2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
2020-08-01 15:49:46 +00:00
|
|
|
import (
|
2021-02-24 12:16:33 +00:00
|
|
|
"flag"
|
2020-08-01 15:49:46 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-03-19 10:26:45 +00:00
|
|
|
"runtime"
|
2021-02-24 12:16:33 +00:00
|
|
|
"runtime/pprof"
|
2021-02-28 07:57:15 +00:00
|
|
|
|
|
|
|
"github.com/pquerna/otp/totp"
|
2020-08-01 15:49:46 +00:00
|
|
|
)
|
2020-07-28 19:17:07 +00:00
|
|
|
|
2021-02-24 12:16:33 +00:00
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
|
|
|
|
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
|
|
|
|
|
2020-07-28 19:17:07 +00:00
|
|
|
func main() {
|
2021-04-03 13:39:43 +00:00
|
|
|
var err error
|
|
|
|
|
2021-04-11 14:08:29 +00:00
|
|
|
// Init CPU and memory profiling
|
2021-02-24 12:16:33 +00:00
|
|
|
flag.Parse()
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
log.Fatalln("could not create CPU profile: ", err)
|
2021-04-23 16:48:08 +00:00
|
|
|
return
|
2021-02-24 12:16:33 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
log.Fatalln("could not start CPU profile: ", err)
|
2021-04-23 16:48:08 +00:00
|
|
|
return
|
2021-02-24 12:16:33 +00:00
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
2021-04-11 14:08:29 +00:00
|
|
|
if *memprofile != "" {
|
|
|
|
defer func() {
|
|
|
|
f, err := os.Create(*memprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("could not create memory profile: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
runtime.GC()
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
|
|
log.Fatalln("could not write memory profile: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-02-28 07:57:15 +00:00
|
|
|
|
2020-09-20 16:46:25 +00:00
|
|
|
// Initialize config
|
|
|
|
log.Println("Initialize configuration...")
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initConfig(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init config:", err.Error())
|
|
|
|
return
|
2021-04-03 13:39:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 14:08:29 +00:00
|
|
|
// Healthcheck tool
|
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "healthcheck" {
|
|
|
|
// Connect to public address + "/ping" and exit with 0 when successful
|
2021-05-07 14:14:15 +00:00
|
|
|
health := healthcheckExitCode()
|
|
|
|
shutdown()
|
|
|
|
os.Exit(health)
|
2021-04-11 14:08:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tool to generate TOTP secret
|
2021-04-03 13:39:43 +00:00
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "totp-secret" {
|
|
|
|
key, err := totp.Generate(totp.GenerateOpts{
|
|
|
|
Issuer: appConfig.Server.PublicAddress,
|
|
|
|
AccountName: appConfig.User.Nick,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit(err.Error())
|
2021-02-28 07:57:15 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
log.Println("TOTP-Secret:", key.Secret())
|
2021-05-07 14:14:15 +00:00
|
|
|
shutdown()
|
2021-04-03 13:39:43 +00:00
|
|
|
return
|
2021-02-28 07:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 12:17:42 +00:00
|
|
|
// Init regular garbage collection
|
|
|
|
initGC()
|
|
|
|
|
2020-09-20 16:46:25 +00:00
|
|
|
// Execute pre-start hooks
|
|
|
|
preStartHooks()
|
2021-04-03 13:39:43 +00:00
|
|
|
|
|
|
|
// Initialize database and markdown
|
2020-09-20 16:46:25 +00:00
|
|
|
log.Println("Initialize database...")
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initDatabase(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init database:", err.Error())
|
2020-08-01 15:49:46 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2020-09-20 16:46:25 +00:00
|
|
|
log.Println("Initialize server components...")
|
2020-09-20 17:10:01 +00:00
|
|
|
initMarkdown()
|
2021-04-03 13:39:43 +00:00
|
|
|
|
|
|
|
// Link check tool after init of markdown
|
|
|
|
if len(os.Args) >= 2 && os.Args[1] == "check" {
|
|
|
|
checkAllExternalLinks()
|
2021-05-07 14:14:15 +00:00
|
|
|
shutdown()
|
2020-09-19 10:27:07 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
|
|
|
|
// More initializations
|
|
|
|
initMinify()
|
|
|
|
if err = initTemplateAssets(); err != nil { // Needs minify
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init template assets:", err.Error())
|
2020-09-20 17:10:01 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initTemplateStrings(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init template translations:", err.Error())
|
2020-09-20 15:47:54 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initRendering(); err != nil { // Needs assets and minify
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init HTML rendering:", err.Error())
|
2020-11-20 14:33:20 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initCache(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init HTTP cache:", err.Error())
|
2020-10-15 18:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initRegexRedirects(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init redirects:", err.Error())
|
2020-11-26 16:40:40 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initHTTPLog(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init HTTP logging:", err.Error())
|
2020-11-17 21:10:13 +00:00
|
|
|
return
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2021-04-03 13:39:43 +00:00
|
|
|
if err = initActivityPub(); err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to init ActivityPub:", err.Error())
|
2021-04-03 13:39:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-24 08:44:43 +00:00
|
|
|
initWebmention()
|
2020-11-17 21:10:13 +00:00
|
|
|
initTelegram()
|
2021-05-10 15:37:34 +00:00
|
|
|
initBlogStats()
|
2021-05-14 16:24:02 +00:00
|
|
|
initSessions()
|
2020-08-01 15:49:46 +00:00
|
|
|
|
2020-10-18 15:22:24 +00:00
|
|
|
// Start cron hooks
|
|
|
|
startHourlyHooks()
|
|
|
|
|
2020-08-01 15:49:46 +00:00
|
|
|
// Start the server
|
2021-05-07 14:14:15 +00:00
|
|
|
log.Println("Starting server(s)...")
|
2021-04-03 13:39:43 +00:00
|
|
|
err = startServer()
|
|
|
|
if err != nil {
|
2021-05-07 14:14:15 +00:00
|
|
|
logErrAndQuit("Failed to start server(s):", err.Error())
|
2021-04-03 13:39:43 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-01 15:49:46 +00:00
|
|
|
|
2021-04-03 13:39:43 +00:00
|
|
|
// Wait till everything is shutdown
|
|
|
|
waitForShutdown()
|
|
|
|
|
2021-05-07 14:14:15 +00:00
|
|
|
}
|
2020-08-01 15:49:46 +00:00
|
|
|
|
2021-05-07 14:14:15 +00:00
|
|
|
func logErrAndQuit(v ...interface{}) {
|
|
|
|
log.Println(v...)
|
|
|
|
shutdown()
|
|
|
|
os.Exit(1)
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|