aboutsummaryrefslogtreecommitdiff
path: root/cmd/monfront/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/monfront/main.go')
-rw-r--r--cmd/monfront/main.go53
1 files changed, 52 insertions, 1 deletions
diff --git a/cmd/monfront/main.go b/cmd/monfront/main.go
index a318e75..19be6e0 100644
--- a/cmd/monfront/main.go
+++ b/cmd/monfront/main.go
@@ -6,8 +6,10 @@ import (
"flag"
"fmt"
"html/template"
+ "io"
"io/ioutil"
"log"
+ "log/slog"
"net"
"net/http"
"os"
@@ -49,6 +51,11 @@ type (
Mode string `toml:"mode"`
List []string `toml:"list"`
}
+ Log struct {
+ Format string `toml:"format"`
+ Level string `toml:"level"`
+ Output string `toml:"output"`
+ }
}
MapEntry struct {
@@ -99,6 +106,8 @@ func main() {
log.Fatalf("could not parse config: %s", err)
}
+ logger := parseLogger(config)
+
db, err := sql.Open("postgres", config.DB)
if err != nil {
log.Fatalf("could not open database connection: %s", err)
@@ -168,7 +177,7 @@ func main() {
l = tls.NewListener(l, tlsConf)
}
- s := newServer(l, db, tmpl, auth, autho)
+ s := newServer(l, db, logger, tmpl, auth, autho)
s.Handle("/", showChecks)
s.Handle("/create", showCreate)
s.Handle("/check", showCheck)
@@ -179,6 +188,48 @@ func main() {
log.Fatalf("http server stopped: %s", s.ListenAndServe())
}
+func parseLogger(config Config) *slog.Logger {
+ var output io.Writer
+ switch config.Log.Output {
+ case "", "stderr":
+ output = os.Stderr
+ case "stdout":
+ output = os.Stdout
+ default:
+ var err error
+ output, err = os.OpenFile(config.Log.Output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0640)
+ if err != nil {
+ log.Fatalf("could not open log file handler: %s", err)
+ }
+ }
+
+ var level slog.Level
+ switch config.Log.Level {
+ case "debug":
+ level = slog.LevelDebug
+ case "", "info":
+ level = slog.LevelInfo
+ case "warn":
+ level = slog.LevelWarn
+ case "error":
+ level = slog.LevelError
+ default:
+ log.Fatalf("unknown log level '%s', only 'debug', 'info', 'warn' and 'error' are supported", config.Log.Level)
+ }
+
+ var handler slog.Handler
+ switch config.Log.Format {
+ case "", "text":
+ handler = slog.NewTextHandler(output, &slog.HandlerOptions{Level: level})
+ case "json":
+ handler = slog.NewJSONHandler(output, &slog.HandlerOptions{Level: level})
+ default:
+ log.Fatalf("unknown log format '%s', only 'text' and 'json' are supported", config.Log.Format)
+ }
+
+ return slog.New(handler)
+}
+
func checkAction(con *Context) {
if con.r.Method != "POST" {
con.w.WriteHeader(http.StatusMethodNotAllowed)