-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
210 lines (173 loc) · 4.55 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
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
package main
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"time"
"github.com/andybalholm/brotli"
"github.com/diamondburned/smolboard/frontend/frontserver"
"github.com/diamondburned/smolboard/server"
"github.com/diamondburned/smolboard/server/http/upload"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/http2"
toml "github.com/pelletier/go-toml"
)
var (
configGlob = "./config*.toml"
noFrontend = false
)
func stderrlnf(f string, v ...interface{}) {
fmt.Fprintf(os.Stderr, f+"\n", v...)
}
type Config struct {
SocketPath string `toml:"socketPath"`
SocketPerm string `toml:"socketPerm"`
frontserver.FrontConfig
server.Config
}
func NewConfig() Config {
return Config{
FrontConfig: frontserver.NewConfig(),
Config: server.NewConfig(),
}
}
func init() {
pflag.StringVarP(
&configGlob, "config", "c", configGlob,
"Path to config file with glob support for fallback",
)
pflag.BoolVarP(
&noFrontend, "no-frontend", "n", noFrontend,
"Disable the default frontend at root",
)
pflag.Usage = func() {
stderrlnf("Usage: %s [subcommand] [flags...]", filepath.Base(os.Args[0]))
stderrlnf("Subcommands:")
stderrlnf(" create-owner Initialize a new owner user once")
stderrlnf(" serve Run the HTTP server")
stderrlnf("Flags:")
pflag.PrintDefaults()
}
}
func main() {
pflag.Parse()
// Read all globs.
d, err := filepath.Glob(configGlob)
if err != nil {
log.Fatalln("Failed to glob:", err)
}
if len(d) == 0 {
log.Fatalln("Glob returns no matches.")
}
var cfg = NewConfig()
for _, path := range d {
f, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln("Failed to read globbed config file:", err)
}
t, err := toml.LoadBytes(f)
if err != nil {
log.Fatalln("Failed to load TOML:")
}
// Workaround: Unmarshal really wants a non-nil MaxSize block, else it
// will panic. We have to manually insert this if it's not there.
if !t.Has("MaxSize") {
t.SetPath([]string{"MaxSize"}, upload.MaxSize{})
}
if err := t.Unmarshal(&cfg); err != nil {
log.Fatalln("Failed to unmarshal from TOML:", err)
}
}
switch pflag.Arg(0) {
case "create-owner":
fmt.Print("Enter your password: ")
p, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalln("Failed to read password:", err)
}
fmt.Println()
if err := server.CreateOwner(cfg.Config, p); err != nil {
log.Fatalln(err)
}
case "serve":
fallthrough
default:
a, err := server.New(cfg.Config)
if err != nil {
log.Fatalln("Failed to create instance:", err)
}
c := middleware.NewCompressor(5)
c.SetEncoder("br", func(w io.Writer, level int) io.Writer {
return brotli.NewWriterLevel(w, level)
})
mux := chi.NewMux()
mux.Use(c.Handler)
mux.Mount("/api/v1", a)
if !noFrontend {
f, err := frontserver.New(cfg.SocketPath, cfg.FrontConfig)
if err != nil {
log.Fatalln("Failed to create frontend:", err)
}
mux.Mount("/", f)
}
// Ensure that the socket is cleaned up because we're not gracefully
// handling closes.
if err := os.Remove(cfg.SocketPath); err != nil {
if !errors.Is(err, os.ErrNotExist) {
log.Fatalln("Failed to clean up old socket:", err)
}
}
l, err := net.Listen("unix", cfg.SocketPath)
if err != nil {
log.Fatalln("Failed to listen to Unix socket:", err)
}
if cfg.SocketPerm != "" {
o, err := strconv.ParseUint(cfg.SocketPerm, 8, 32)
if err != nil {
log.Fatalln("Failed to parse socket perm in octet:", err)
}
if err := os.Chmod(cfg.SocketPath, os.FileMode(o)); err != nil {
log.Fatalln("Failed to chmod socket:", err)
}
}
var server = http.Server{
Handler: mux,
}
// Explicitly set up HTTP/2.
err = http2.ConfigureServer(&server, &http2.Server{
MaxHandlers: 10240,
MaxConcurrentStreams: 4096,
})
if err != nil {
log.Fatalln("Failed to configure HTTP/2 server:", err)
}
log.Println("Starting HTTP/2 listener at socket", l.Addr())
go func() {
if err := server.Serve(l); err != nil {
log.Fatalln("Failed to start:", err)
}
}()
// Handle SIGINT and gracefully close the server.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
<-sig
// Give the server a 10 seconds timeout for shutting down.
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalln("Failed to gracefully close the server:", err)
}
}
}