-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
74 lines (68 loc) · 2.09 KB
/
run.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
package main
import (
"context"
"embed"
"flag"
"fmt"
"github.com/SimonSchneider/goslu/config"
"github.com/SimonSchneider/goslu/srvu"
"github.com/SimonSchneider/goslu/templ"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
)
//go:embed static/*
var embeddedFS embed.FS
type Config struct {
Watch bool
Addr string
MaxSecrets uint
MaxSecretSize int64 `config:"u: Maximum size of a secret in bytes"`
}
func parseConfig(args []string, getEnv func(string) string) (Config, error) {
cfg := Config{
Addr: ":8888",
MaxSecrets: 100,
MaxSecretSize: 256 * 1024,
}
return cfg, config.ParseInto(&cfg, flag.NewFlagSet("", flag.ExitOnError), args, getEnv)
}
func Run(ctx context.Context, args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer, getEnv func(string) string, getwd func() (string, error)) error {
cfg, err := parseConfig(args[1:], getEnv)
if err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
logger := srvu.LogToOutput(log.New(stdout, "", log.LstdFlags|log.Lshortfile))
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
pub, tmpl, err := templ.GetPublicAndTemplates(embeddedFS, &templ.Config{
Watch: cfg.Watch,
TmplPatterns: []string{"templates/*.gohtml"},
})
if err != nil {
return fmt.Errorf("failed to get public and templates: %w", err)
}
fmt.Printf("pub: %v, tmpl: %v\n", pub, tmpl.Lookup("secrets.gohtml"))
handler := Handler{
Secrets: NewInMemorySecrets(cfg.MaxSecrets),
Templates: tmpl,
MaxSecretBytes: cfg.MaxSecretSize,
Files: pub,
}
mux := http.NewServeMux()
mux.HandleFunc("POST /secrets", handler.CreateSecret)
mux.HandleFunc("GET /secrets/{id}", handler.GetSecret)
mux.Handle("GET /", http.StripPrefix("/", http.FileServerFS(pub)))
srv := &http.Server{
BaseContext: func(listener net.Listener) context.Context {
return ctx
},
Addr: cfg.Addr,
Handler: srvu.With(mux, srvu.WithCompression(), srvu.WithLogger(logger)),
}
logger.Printf("Listening on: %s", cfg.Addr)
return srvu.RunServerGracefully(ctx, srv, logger)
}