-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (57 loc) · 1.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/DemmyDemon/tjenare/config"
"github.com/DemmyDemon/tjenare/mediator"
"github.com/DemmyDemon/tjenare/redirect"
)
func main() {
log.SetFlags(log.Ltime | log.Ldate)
log.SetOutput(os.Stdout)
if os.Getenv("DEVMODE") != "" {
log.Println("Filename logging enabled: 'Developer mode'")
log.SetFlags(log.Lshortfile | log.Ltime | log.Ldate)
}
var cfg *config.ServerConfig
var err error
switch {
case len(os.Args) > 1:
log.Printf("Loading configuration from command line argument: %s", os.Args[1])
cfg, err = config.Load(os.Args[1])
case os.Getenv("CONFIG") != "":
log.Printf("Loading configuration from CONFIG environment variable: %s", os.Getenv("CONFIG"))
cfg, err = config.Load(os.Getenv("CONFIG"))
default:
log.Printf("Loading hardcoded default configuration from /etc/tjenare.json")
cfg, err = config.Load("/etc/tjenare.json")
}
if err != nil {
log.Fatal(err)
}
if cfg.LogFile != "" {
logfile, err := os.OpenFile(cfg.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
log.Fatalf("Failed to open log file %q: %s\n", cfg.LogFile, err)
}
log.SetOutput(logfile)
log.Println("Log file opened")
defer func() {
// This is unlikely to ever happen, as the OS has likely slain the process if main is returning.
log.Println("Log file closing")
log.SetOutput(os.Stderr)
err := logfile.Close()
if err != nil {
log.Fatalf("Error closing log file: %s", err)
}
}()
}
go redirect.ServeSSLRedirect(cfg)
go mediator.Begin(cfg)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c // Block here until the interrupt comes in
log.Println("Received interrupt")
}