-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
206 lines (191 loc) · 5.44 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
package main
import (
"fmt"
"io"
"os"
"runtime"
"runtime/pprof"
"github.com/pquerna/otp/totp"
"github.com/spf13/cobra"
"go.goblog.app/app/pkgs/utils"
)
func main() {
rootCmd := &cobra.Command{
Use: "GoBlog",
Short: "Main application, without any command, the app gets started.",
Run: func(cmd *cobra.Command, args []string) {
app := initializeApp(cmd)
if err := app.initPlugins(); err != nil {
app.logErrAndQuit("Failed to init plugins", "err", err)
return
}
app.preStartHooks()
initializeComponents(app)
app.startHourlyHooks()
app.startPprofServer()
if err := app.startServer(); err != nil {
app.logErrAndQuit("Failed to start server(s)", "err", err)
}
app.shutdown.Wait()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cpuprofile, _ := cmd.Flags().GetString("cpuprofile"); cpuprofile != "" {
r, w := io.Pipe()
go func() {
_ = w.CloseWithError(pprof.StartCPUProfile(w))
}()
go func() {
_ = r.CloseWithError(utils.SaveToFile(r, cpuprofile))
}()
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
pprof.StopCPUProfile()
if memprofile, _ := cmd.Flags().GetString("memprofile"); memprofile != "" {
runtime.GC()
r, w := io.Pipe()
go func() {
_ = w.CloseWithError(pprof.WriteHeapProfile(w))
}()
_ = r.CloseWithError(utils.SaveToFile(r, memprofile))
}
},
}
// Add flags
rootCmd.PersistentFlags().String("cpuprofile", "", "write CPU profile to file")
rootCmd.PersistentFlags().String("memprofile", "", "write memory profile to file")
rootCmd.PersistentFlags().String("config", "", "use a specific config file")
// Healthcheck command
rootCmd.AddCommand(&cobra.Command{
Use: "healthcheck",
Short: "Perform health check",
Run: func(cmd *cobra.Command, args []string) {
app := initializeApp(cmd)
health := app.healthcheckExitCode()
app.shutdown.ShutdownAndWait()
os.Exit(health)
},
})
// TOTP secret generation command
rootCmd.AddCommand(&cobra.Command{
Use: "totp-secret",
Short: "Generate TOTP secret",
Run: func(cmd *cobra.Command, args []string) {
app := initializeApp(cmd)
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()
},
})
// Link check tool
rootCmd.AddCommand(&cobra.Command{
Use: "check",
Short: "Check all external links",
Run: func(cmd *cobra.Command, args []string) {
app := initializeApp(cmd)
if err := app.initTemplateStrings(); err != nil {
app.logErrAndQuit("Failed to start check", "err", err)
}
if err := app.checkAllExternalLinks(); err != nil {
app.logErrAndQuit("Failed to check links", "err", err)
}
app.shutdown.ShutdownAndWait()
},
})
// Markdown export command
rootCmd.AddCommand(&cobra.Command{
Use: "export [directory]",
Short: "Export markdown files",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
app := initializeApp(cmd)
var dir string
if len(args) > 0 {
dir = args[0]
}
if err := app.exportMarkdownFiles(dir); err != nil {
app.logErrAndQuit("Failed to export markdown files", "err", err)
}
app.shutdown.ShutdownAndWait()
},
})
// ActivityPub refetch followers
activityPubCmd := &cobra.Command{
Use: "activitypub",
Short: "ActivityPub related tasks",
}
activityPubCmd.AddCommand(&cobra.Command{
Use: "refetch-followers blog",
Short: "Refetch ActivityPub followers",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
app := initializeApp(cmd)
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
}
blog := args[0]
if err := app.apRefetchFollowers(blog); err != nil {
app.logErrAndQuit("Failed to refetch ActivityPub followers", "blog", blog, "err", err)
}
app.shutdown.ShutdownAndWait()
},
})
rootCmd.AddCommand(activityPubCmd)
// Execute the root command
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initializeApp(cmd *cobra.Command) *goBlog {
app := &goBlog{
httpClient: newHttpClient(),
}
configfile, _ := cmd.Flags().GetString("config")
if err := app.loadConfigFile(configfile); err != nil {
app.logErrAndQuit("Failed to load config file", "err", err)
return nil
}
if err := app.initConfig(false); err != nil {
app.logErrAndQuit("Failed to init config", "err", err)
return nil
}
return app
}
func initializeComponents(app *goBlog) {
app.info("Initialize components...")
for _, f := range []func() error{
app.initTemplateAssets, app.initTemplateStrings, app.initRegexRedirects,
app.initHTTPLog, app.initActivityPub, app.initWebAuthn,
} {
if err := f(); err != nil {
app.logErrAndQuit("Failed to initialize", "err", err)
return
}
}
for _, f := range []func(){
app.initWebmention, app.initTelegram, app.initAtproto, app.initBlogStats,
app.initTTS, app.initSessions, app.startPostsScheduler, app.initPostsDeleter,
app.initIndexNow,
} {
f()
}
app.info("Initialized components")
}
func (a *goBlog) logErrAndQuit(msg string, args ...any) {
a.error(msg, args...)
a.shutdown.ShutdownAndWait()
os.Exit(1)
}