-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (55 loc) · 1.7 KB
/
Copy pathmain.go
File metadata and controls
66 lines (55 loc) · 1.7 KB
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
66
package main
import (
"log"
"os"
"path/filepath"
"github.com/winnerproxy/winnerproxy/config"
"github.com/winnerproxy/winnerproxy/internal/cache"
"github.com/winnerproxy/winnerproxy/internal/handler"
"github.com/winnerproxy/winnerproxy/internal/router"
)
const configFileName = "config.yml"
func main() {
cfgPath, err := configPath()
if err != nil {
log.Fatalf("locate executable: %v", err)
}
preStart(cfgPath)
cfg := config.Load(cfgPath)
log.Printf("config loaded: %s", cfgPath)
c := cache.New(cfg.Cache.Size)
log.Printf("freecache initialized: %d bytes", cfg.Cache.Size)
h := handler.New(c)
engine := router.New(h)
log.Printf("WinnerProxy listening on %s", cfg.Server.Addr)
if err := engine.Run(cfg.Server.Addr); err != nil {
log.Fatalf("server stopped: %v", err)
}
}
// preStart runs initialization steps that must complete before the
// server begins accepting connections. It ensures a config.yml exists
// next to the executable, creating a default one if missing.
func preStart(cfgPath string) {
log.Printf("running pre-start process: %s", cfgPath)
if _, err := os.Stat(cfgPath); err == nil {
log.Printf("config already present: %s", cfgPath)
return
} else if !os.IsNotExist(err) {
log.Printf("stat config: %v", err)
return
}
if err := os.WriteFile(cfgPath, config.DefaultYAML(), 0o644); err != nil {
log.Printf("create config: %v", err)
return
}
log.Printf("created default config: %s", cfgPath)
}
// configPath returns the absolute path of config.yml located in the
// same directory as the running executable.
func configPath() (string, error) {
exe, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Join(filepath.Dir(exe), configFileName), nil
}