-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsmasq-manager.go
114 lines (94 loc) · 2.77 KB
/
dnsmasq-manager.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/monitor"
"github.com/gringolito/dnsmasq-manager/api"
"github.com/gringolito/dnsmasq-manager/api/handler"
"github.com/gringolito/dnsmasq-manager/config"
"github.com/gringolito/dnsmasq-manager/pkg/host"
"golang.org/x/exp/slog"
)
const (
AppName = "dnsmasq Manager"
AppVersion = "v0.1.0"
)
// These properties are variables because release builds will change their values
var (
BuildMode = "Development"
// This file path will be changed to a proper absolute path on the OS directory tree
OpenApiSpecFile = "api/spec/openapi.yaml"
)
func setupLogger(cfg *config.Config) *slog.Logger {
// Defaults to slog.LevelInfo
logLevel := map[string]slog.Level{
config.LogLevelError: slog.LevelError,
config.LogLevelWarning: slog.LevelWarn,
config.LogLevelInfo: slog.LevelInfo,
config.LogLevelDebug: slog.LevelDebug,
}
options := &slog.HandlerOptions{
AddSource: cfg.Log.Source,
Level: logLevel[cfg.Log.Level],
}
var output io.Writer = os.Stdout
if cfg.Log.File != "" {
logFile, err := os.OpenFile(cfg.Log.File, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
if err != nil {
log.Fatal("Failed to create log output file", err)
}
output = logFile
}
var handler slog.Handler
if cfg.Log.Format == config.LogFormatPlainText {
handler = slog.NewTextHandler(output, options)
} else {
handler = slog.NewJSONHandler(output, options)
}
handler = handler.WithAttrs([]slog.Attr{
slog.Group("app",
slog.String("name", AppName),
slog.String("version", AppVersion),
),
})
logger := slog.New(handler)
slog.SetDefault(logger)
return logger
}
func addStaticHostApi(router api.Router, cfg *config.Config) {
hostRepository := host.NewRepository(cfg.Host.Static.File)
hostService := host.NewService(hostRepository)
handler.RouteStaticHosts(router, hostService)
}
func main() {
configName := "test"
cfg, err := config.Init(configName)
if err != nil {
log.Fatal(err)
}
logger := setupLogger(cfg)
logger.Info("Starting app", slog.String("config", configName))
app := fiber.New(fiber.Config{
CaseSensitive: true,
EnablePrintRoutes: true,
AppName: fmt.Sprintf("%s %s (%s build)", AppName, AppVersion, BuildMode),
})
middleware, err := api.NewMiddleware(logger, cfg)
if err != nil {
logger.Error(err.Error(), slog.String("config", configName))
os.Exit(1)
}
router := api.NewRouter(app, middleware)
router.AddSwaggerUIRoute(OpenApiSpecFile)
router.AddMetricsRoute(monitor.Config{
Title: fmt.Sprintf("%s Monitor", AppName),
})
addStaticHostApi(router, cfg)
if err := app.Listen(fmt.Sprintf(":%d", cfg.Server.Port)); err != nil {
logger.Error(err.Error(), slog.Int("listeningPort", cfg.Server.Port))
os.Exit(1)
}
}