Skip to content

Commit

Permalink
Merge pull request #269 from simonmittag/268
Browse files Browse the repository at this point in the history
268
  • Loading branch information
simonmittag authored Jul 13, 2022
2 parents 341c6e9 + f5afbc4 commit 58e2d0f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
40 changes: 40 additions & 0 deletions cmd/j8a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package main
import (
"flag"
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/simonmittag/j8a"
"os"
"os/signal"
"syscall"
"time"
)

func main() {
go waitForSignal()

//for Bootstrap
defer recovery()

cfgFile := flag.String("c", j8a.DefaultConfigFile, "config file location")
flag.Usage = func() {
fmt.Printf(`j8a[%s] "Achuta! j8a [ dʒʌbbʌ ] is a TLS reverse proxy server for JSON APIs written in golang."`, j8a.Version)
Expand All @@ -31,3 +42,32 @@ func isFlagPassed(name string) bool {
})
return found
}

func recovery() {
if r := recover(); r != nil {
pid := os.Getpid()
log.WithLevel(zerolog.FatalLevel).
Int("pid", pid).
Msg("exiting...")
os.Exit(-1)
}
}

func waitForSignal() {
defer recovery()
sig := interruptChannel()
for {
select {
case <-sig:
panic("os signal")
default:
time.Sleep(time.Second * 1)
}
}
}

func interruptChannel() chan os.Signal {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT)
return sigs
}
9 changes: 4 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func (config Config) panic(msg string) {
if len(msg) == 0 {
msg = "error loading config."
}
msg = msg + " exiting..."
log.WithLevel(zerolog.FatalLevel).Msg(msg)
panic(msg)
}
Expand All @@ -86,7 +85,7 @@ func (config Config) readYmlFile(file string) *Config {
f, err := os.Open(file)
defer f.Close()
if err != nil {
msg := fmt.Sprintf("unable to load config from %s, exiting...", file)
msg := fmt.Sprintf("unable to load config from %s", file)
log.Fatal().Msg(msg)
panic(msg)
}
Expand Down Expand Up @@ -224,15 +223,15 @@ func (config Config) validateAcmeConfig() *Config {
if !acmeProvider {
config.panic("ACME provider must be specified in ACME config")
}

if !acmeDomain {
config.panic("ACME domain must be specified in ACME config")
}

if !acmeEmail {
config.panic("ACME email must be specified in ACME config")
}

// ACME domain checks
for _, domain := range config.Connection.Downstream.Tls.Acme.Domains {
if !govalidator.IsDNSName(domain) {
Expand All @@ -255,7 +254,7 @@ func (config Config) validateAcmeConfig() *Config {
config.panic(fmt.Sprintf("ACME domain validation does not support domains ending with '.', was %s", domain))
}
}

// ACME provider checks
if _, supported := acmeProviders[config.Connection.Downstream.Tls.Acme.Provider]; !supported {
config.panic(fmt.Sprintf("ACME provider not supported: %s", config.Connection.Downstream.Tls.Acme.Provider))
Expand Down
9 changes: 9 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func initServerID() {
log.Logger = log.With().Str("srvId", ID).Logger()
}

//ServerID is a unique identifier made up as md5 of hostname and version.
//initServerId creates a unique ID for the server log.
func initPID() {
pid := os.Getpid()
log.Info().Int("pid", pid).Msg("pid determined")
log.Logger = log.With().Int("pid", pid).Logger()
}

func getHost() string {
host := os.Getenv("HOSTNAME")
if len(host) == 0 {
Expand Down Expand Up @@ -93,5 +101,6 @@ func initLogger() {

initServerID()
initTime()
initPID()
log.Info().Msgf("setting global log level to %s", logLevel)
}
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (rt *Runtime) startListening() {

select {
case sig := <-err:
log.Fatal().Err(sig).Msg("... j8a exiting")
log.Fatal().Err(sig).Msg(sig.Error())
panic(sig.Error())
}
}
Expand Down

0 comments on commit 58e2d0f

Please sign in to comment.