-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (152 loc) · 4.39 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
*/
package main
import (
"context"
"embed"
"flag"
"io/fs"
"log"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"time"
"github.com/fingon/lixie/cm"
"github.com/fingon/lixie/data"
)
// This is set from Makefile, which is .. ugly, and somewhat awkward
var ldBuildTimestamp = "not set"
var boot = time.Now()
// Note: While we don't have any static, double comment = static/ will be empty
// //go:embed all:static
var embedContent embed.FS
type mainConfig struct {
RSSort int `cm:"rss"`
}
func mainHandler(st State) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
config := mainConfig{RSSort: rsHits.ID()}
err := cm.Run(r, w, &config)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
err = MainPage(st, config).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), 400)
}
})
}
func newMux(st State) http.Handler {
mux := http.NewServeMux()
// Configure the routes
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Other options: StatusMovedPermanently, StatusFound
// http.Redirect(w, r, "/rule/edit", http.StatusSeeOther)
// })
mux.HandleFunc("/", http.NotFound)
mux.Handle("/{$}", mainHandler(st))
mux.Handle(topLevelLog.PathMatcher(), logListHandler(st))
mux.Handle(topLevelLog.Path+"/{hash}/ham", logClassifyHandler(st, true))
mux.Handle(topLevelLog.Path+"/{hash}/spam", logClassifyHandler(st, false))
mux.Handle(topLevelLogRule.PathMatcher(), logRuleListHandler(st))
mux.Handle(topLevelLogRule.Path+"/edit", logRuleEditHandler(st))
mux.Handle(topLevelLogRule.Path+"/{id}/delete", logRuleDeleteSpecificHandler(st))
mux.Handle(topLevelLogRule.Path+"/{id}/edit", logRuleEditSpecificHandler(st))
mux.Handle("/version", versionHandler(st))
// Static content
staticFS, err := fs.Sub(embedContent, "static")
if err != nil {
log.Panic(err)
}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
return mux
}
// Most of this function is based on
// https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years/
//
// These might be also useful at some point
//
// getenv func(string) string,
// stdin io.Reader,
// stdout, stderr io.Writer,
func run(
ctx context.Context,
args []string,
) error {
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
// CLI
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
address := flags.String("address", "127.0.0.1", "Address to listen at")
lokiServer := flags.String("loki-server", "https://fw.fingon.iki.fi:3100", "Address of the Loki server")
lokiSelector := flags.String("loki-selector", "{host=~\".+\"}", "Selector to use when querying logs from Loki")
arrayFile := flags.String("log-source-file", "", "Log file source")
dbPath := flags.String("db", "db.json", "Database to use")
dev := flags.Bool("dev", false, "Enable development mode")
port := flags.Int("port", 8080, "Port number to listen at")
if err := flags.Parse(args[1:]); err != nil {
return err
}
var source data.LogSource
if *arrayFile != "" {
arr := data.ArraySource{}
err := data.UnmarshalJSONFromPath(&arr, *arrayFile)
if err != nil {
return err
}
source = &arr
} else {
source = &data.LokiSource{Server: *lokiServer, Selector: *lokiSelector}
}
db := data.Database{Source: source, Path: *dbPath}
err := db.Load()
if err != nil {
return err
}
state := State{DB: &db, BuildTimestamp: ldBuildTimestamp}
if *dev {
state.RefreshIntervalMs = 1000
}
mux := newMux(state)
// Start the actual server
httpServer := &http.Server{
Addr: net.JoinHostPort(*address, strconv.Itoa(*port)),
Handler: mux,
}
go func() {
slog.Info("Listening", "addr", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error("Error listening", "err", err)
}
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
slog.Error("error shutting down http server", "err", err)
}
}()
wg.Wait()
return nil
}
func main() {
ctx := context.Background()
err := run(ctx, os.Args)
if err != nil {
slog.Error("run resulted in error", "err", err)
os.Exit(1)
}
}