-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
260 lines (236 loc) · 6.47 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
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
package main
import (
"cmp"
"flag"
"fmt"
"net"
"net/http"
netpprof "net/http/pprof"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/pquerna/otp/totp"
)
func main() {
var err error
// Command line flags
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")
configfile := flag.String("config", "", "use a specific config file")
// Init app and logger
app := &goBlog{
httpClient: newHttpClient(),
}
app.initLog()
// Init CPU and memory profiling
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
app.fatal("could not create CPU profile", "err", err)
return
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
app.fatal("could not start CPU profile", "err", err)
return
}
defer pprof.StopCPUProfile()
}
if *memprofile != "" {
defer func() {
f, err := os.Create(*memprofile)
if err != nil {
app.fatal("could not create memory profile", "err", err)
return
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
app.fatal("could not write memory profile", "err", err)
return
}
}()
}
// Initialize config
if err = app.loadConfigFile(*configfile); err != nil {
app.logErrAndQuit("Failed to load config file", "err", err)
return
}
if err = app.initConfig(false); err != nil {
app.logErrAndQuit("Failed to init config", "err", err)
return
}
// Healthcheck tool
if len(os.Args) >= 2 && os.Args[1] == "healthcheck" {
// Connect to public address + "/ping" and exit with 0 when successful
health := app.healthcheckExitCode()
app.shutdown.ShutdownAndWait()
os.Exit(health)
return
}
// Tool to generate TOTP secret
if len(os.Args) >= 2 && os.Args[1] == "totp-secret" {
key, err := totp.Generate(totp.GenerateOpts{
Issuer: app.cfg.Server.PublicAddress,
AccountName: app.cfg.User.Nick,
})
if err != nil {
app.logErrAndQuit("Failed to generate TOTP secret", "err", err)
return
}
fmt.Println("TOTP-Secret:", key.Secret())
app.shutdown.ShutdownAndWait()
return
}
// Initialize plugins
if err = app.initPlugins(); err != nil {
app.logErrAndQuit("Failed to init plugins", "err", err)
return
}
// Start pprof server
if pprofCfg := app.cfg.Pprof; pprofCfg != nil && pprofCfg.Enabled {
go func() {
// Build handler
pprofHandler := http.NewServeMux()
pprofHandler.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
http.Redirect(rw, r, "/debug/pprof/", http.StatusFound)
})
pprofHandler.HandleFunc("/debug/pprof/", netpprof.Index)
pprofHandler.HandleFunc("/debug/pprof/{action}", netpprof.Index)
pprofHandler.HandleFunc("/debug/pprof/cmdline", netpprof.Cmdline)
pprofHandler.HandleFunc("/debug/pprof/profile", netpprof.Profile)
pprofHandler.HandleFunc("/debug/pprof/symbol", netpprof.Symbol)
pprofHandler.HandleFunc("/debug/pprof/trace", netpprof.Trace)
// Build server and listener
pprofServer := &http.Server{
Addr: cmp.Or(pprofCfg.Address, "localhost:0"),
Handler: pprofHandler,
ReadHeaderTimeout: 1 * time.Minute,
}
listener, err := net.Listen("tcp", pprofServer.Addr)
if err != nil {
app.fatal("Failed to start pprof server", "err", err)
return
}
app.info("Pprof server listening", "addr", listener.Addr().String())
// Start server
if err := pprofServer.Serve(listener); err != nil {
app.fatal("Failed to start pprof server", "err", err)
return
}
}()
}
// Execute pre-start hooks
app.preStartHooks()
// Link check tool after init of markdown
if len(os.Args) >= 2 && os.Args[1] == "check" {
app.initMarkdown()
err = app.initTemplateStrings()
if err != nil {
app.logErrAndQuit("Failed to start check", "err", err)
}
err = app.checkAllExternalLinks()
if err != nil {
app.logErrAndQuit("Failed to start check", "err", err)
}
app.shutdown.ShutdownAndWait()
return
}
// Markdown export
if len(os.Args) >= 2 && os.Args[1] == "export" {
var dir string
if len(os.Args) >= 3 {
dir = os.Args[2]
}
err = app.exportMarkdownFiles(dir)
if err != nil {
app.logErrAndQuit("Failed to export markdown files", "err", err)
return
}
app.shutdown.ShutdownAndWait()
return
}
// ActivityPub refetch followers
if len(os.Args) >= 2 && os.Args[1] == "activitypub" {
if !app.apEnabled() {
app.logErrAndQuit("ActivityPub not enabled")
return
}
if err = app.initActivityPubBase(); err != nil {
app.logErrAndQuit("Failed to init ActivityPub base", "err", err)
return
}
if len(os.Args) >= 4 && os.Args[2] == "refetch-followers" {
blog := os.Args[3]
if err = app.apRefetchFollowers(blog); err != nil {
app.logErrAndQuit("Failed to refetch ActivityPub followers", "blog", blog, "err", err)
return
}
app.shutdown.ShutdownAndWait()
return
}
}
// Initialize components
app.initComponents()
// Start cron hooks
app.startHourlyHooks()
// Start the server
err = app.startServer()
if err != nil {
app.logErrAndQuit("Failed to start server(s)", "err", err)
return
}
// Wait till everything is shutdown
app.shutdown.Wait()
}
func (app *goBlog) initComponents() {
var err error
app.info("Initialize components...")
app.initMarkdown()
if err = app.initTemplateAssets(); err != nil { // Needs minify
app.logErrAndQuit("Failed to init template assets", "err", err)
return
}
if err = app.initTemplateStrings(); err != nil {
app.logErrAndQuit("Failed to init template translations", "err", err)
return
}
if err = app.initCache(); err != nil {
app.logErrAndQuit("Failed to init HTTP cache", "err", err)
return
}
if err = app.initRegexRedirects(); err != nil {
app.logErrAndQuit("Failed to init redirects", "err", err)
return
}
if err = app.initHTTPLog(); err != nil {
app.logErrAndQuit("Failed to init HTTP logging", "err", err)
return
}
if err = app.initActivityPub(); err != nil {
app.logErrAndQuit("Failed to init ActivityPub", "err", err)
return
}
if err = app.initWebAuthn(); err != nil {
app.logErrAndQuit("Failed to init WebAuthn", "err", err)
return
}
app.initWebmention()
app.initTelegram()
app.initAtproto()
app.initBlogStats()
app.initTTS()
app.initSessions()
app.initIndieAuth()
app.startPostsScheduler()
app.initPostsDeleter()
app.initIndexNow()
app.info("Initialized components")
}
func (a *goBlog) logErrAndQuit(msg string, args ...any) {
a.error(msg, args...)
a.shutdown.ShutdownAndWait()
os.Exit(1)
}