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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
package server
import (
"context"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"log/slog"
"net"
"net/http"
"os"
"path"
"path/filepath"
"slices"
"strings"
"time"
cfg "searchix/internal/config"
"searchix/internal/options"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/osdevisnot/sorvor/pkg/livereload"
"github.com/pkg/errors"
"github.com/shengyanli1982/law"
)
var config *cfg.Config
var (
CommitSHA string
ShortSHA string
)
type Config struct {
Production bool `conf:"default:false"`
InDevServer bool `conf:"default:false"`
LiveReload bool `conf:"default:false,flag:live"`
Root string `conf:"default:website"`
ListenAddress string `conf:"default:localhost"`
Port string `conf:"default:3000,short:p"`
BaseURL cfg.URL `conf:"default:http://localhost:3000,short:b"`
}
type HTTPError struct {
Error error
Message string
Code int
}
type Server struct {
*http.Server
}
const jsSnippet = template.HTML(livereload.JsSnippet) // #nosec G203
type TemplateData struct {
LiveReload template.HTML
Query string
}
type OptionResultData struct {
TemplateData
Query string
Results options.NixOptions
}
type TemplateCollection map[string]*template.Template
func applyDevModeOverrides(config *cfg.Config) {
config.CSP.ScriptSrc = slices.Insert(config.CSP.ScriptSrc, 0, "'unsafe-inline'")
config.CSP.ConnectSrc = slices.Insert(config.CSP.ConnectSrc, 0, "'self'")
}
var templateFuncs = template.FuncMap{
"HTML": func(input string) template.HTML {
return template.HTML(input) // #nosec G203
},
}
func loadTemplate(filename string) (*template.Template, error) {
text, err := os.ReadFile(filename)
if err != nil {
return nil, errors.WithMessage(err, "could not read template")
}
name, _ := strings.CutSuffix(path.Base(filename), ".gotmpl")
tpl := template.New(name)
tpl.Funcs(templateFuncs)
_, err = tpl.Parse(string(text))
if err != nil {
return nil, errors.WithMessage(err, "could not parse template")
}
return tpl, nil
}
func loadTemplates() (TemplateCollection, error) {
templateDir := path.Join("frontend", "templates")
templates := make(TemplateCollection, 0)
index, err := loadTemplate(path.Join(templateDir, "index.gotmpl"))
if err != nil {
return nil, err
}
templates["index"] = index
templatePaths, err := filepath.Glob(path.Join(templateDir, "blocks", "*.gotmpl"))
if err != nil {
return nil, errors.WithMessage(err, "could not glob block templates")
}
for _, fullname := range templatePaths {
tpl, err := loadTemplate(fullname)
if err != nil {
return nil, err
}
_, err = tpl.AddParseTree("index", index.Tree)
if err != nil {
return nil, errors.WithMessage(err, "could not add index template")
}
templates[tpl.Name()] = tpl
}
return templates, nil
}
func New(runtimeConfig *Config) (*Server, error) {
var err error
config, err = cfg.GetConfig()
if err != nil {
return nil, errors.WithMessage(err, "error parsing configuration file")
}
env := "development"
if runtimeConfig.Production {
env = "production"
}
err = sentry.Init(sentry.ClientOptions{
EnableTracing: true,
TracesSampleRate: 1.0,
Dsn: os.Getenv("SENTRY_DSN"),
Release: CommitSHA,
Environment: env,
})
if err != nil {
return nil, errors.WithMessage(err, "could not set up sentry")
}
defer sentry.Flush(2 * time.Second)
sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
templates, err := loadTemplates()
if err != nil {
log.Panicf("could not load templates: %v", err)
}
top := http.NewServeMux()
mux := http.NewServeMux()
indexData := TemplateData{
LiveReload: jsSnippet,
}
mux.HandleFunc("/{$}", func(w http.ResponseWriter, _ *http.Request) {
err := templates["index"].Execute(w, indexData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
var nixosOptions = options.NixOptions{}
jsonFile, err := os.ReadFile(path.Join("data", "test.json"))
if err != nil {
slog.Error(fmt.Sprintf("error reading json file: %v", err))
}
err = json.Unmarshal(jsonFile, &nixosOptions)
if err != nil {
slog.Error(fmt.Sprintf("error parsing json file: %v", err))
}
mux.HandleFunc("/options/results", func(w http.ResponseWriter, r *http.Request) {
tdata := OptionResultData{
TemplateData: indexData,
Query: r.URL.Query().Get("query"),
Results: nixosOptions,
}
var err error
if r.Header.Get("Fetch") == "true" {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
err = templates["options"].ExecuteTemplate(w, "results", tdata)
} else {
err = templates["options"].ExecuteTemplate(w, "index", tdata)
}
if err != nil {
slog.Error(fmt.Sprintf("template error: %v", err))
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("frontend/static"))))
if runtimeConfig.LiveReload {
applyDevModeOverrides(config)
liveReload := livereload.New()
liveReload.Start()
top.Handle("/livereload", liveReload)
fw, err := NewFileWatcher()
if err != nil {
return nil, errors.WithMessage(err, "could not create file watcher")
}
err = fw.AddRecursive(path.Join("frontend"))
if err != nil {
return nil, errors.WithMessage(err, "could not add directory to file watcher")
}
go fw.Start(func(filename string) {
slog.Debug(fmt.Sprintf("got filename %s", filename))
if path.Ext(filename) == ".gotmpl" {
templates, err = loadTemplates()
if err != nil {
slog.Error(fmt.Sprintf("could not reload templates: %v", err))
}
}
liveReload.Reload()
})
}
var logWriter io.Writer
if runtimeConfig.Production {
logWriter = law.NewWriteAsyncer(os.Stdout, nil)
} else {
logWriter = os.Stdout
}
top.Handle("/",
AddHeadersMiddleware(
sentryHandler.Handle(
wrapHandlerWithLogging(mux, wrappedHandlerOptions{
defaultHostname: runtimeConfig.BaseURL.Hostname(),
logger: logWriter,
}),
),
config,
),
)
// no logging, no sentry
top.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, runtimeConfig.Port)
return &Server{
&http.Server{
Addr: listenAddress,
Handler: top,
ReadHeaderTimeout: 20 * time.Second,
},
}, nil
}
func (s *Server) Start() error {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
return errors.WithMessage(err, "could not start server")
}
return nil
}
func (s *Server) Stop() chan struct{} {
slog.Debug("stop called")
idleConnsClosed := make(chan struct{})
go func() {
slog.Debug("shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := s.Server.Shutdown(ctx)
slog.Debug("server shut down")
if err != nil {
// Error from closing listeners, or context timeout:
log.Printf("HTTP server Shutdown: %v", err)
}
close(idleConnsClosed)
}()
return idleConnsClosed
}
|