GoBlog/main.go

229 lines
5.6 KiB
Go
Raw Normal View History

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"
"net"
"net/http"
netpprof "net/http/pprof"
2020-08-01 15:49:46 +00:00
"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
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 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
2021-07-30 13:43:13 +00:00
// Init regular garbage collection
initGC()
app := &goBlog{
httpClient: newHttpClient(),
2021-07-30 13:43:13 +00:00
}
// Initialize config
2021-12-14 16:38:36 +00:00
if err = app.loadConfigFile(*configfile); err != nil {
app.logErrAndQuit("Failed to load config file:", err.Error())
return
}
if err = app.initConfig(); err != nil {
app.logErrAndQuit("Failed to init config:", err.Error())
2021-05-07 14:14:15 +00:00
return
}
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
health := app.healthcheckExitCode()
2021-06-19 21:54:07 +00:00
app.shutdown.ShutdownAndWait()
2021-05-07 14:14:15 +00:00
os.Exit(health)
2021-04-11 14:08:29 +00:00
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(err.Error())
2021-02-28 07:57:15 +00:00
return
}
log.Println("TOTP-Secret:", key.Secret())
2021-06-19 21:54:07 +00:00
app.shutdown.ShutdownAndWait()
return
2021-02-28 07:57:15 +00:00
}
// 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: defaultIfEmpty(pprofCfg.Address, "localhost:0"),
Handler: pprofHandler,
}
listener, err := net.Listen("tcp", pprofServer.Addr)
if err != nil {
log.Fatalln("Failed to start pprof server:", err.Error())
return
}
log.Println("Pprof server listening on", listener.Addr().String())
// Start server
if err := pprofServer.Serve(listener); err != nil {
log.Fatalln("Failed to start pprof server:", err.Error())
return
}
}()
}
// Execute pre-start hooks
app.preStartHooks()
// Initialize database
2021-07-03 10:11:57 +00:00
if err = app.initDatabase(true); err != nil {
app.logErrAndQuit("Failed to init database:", err.Error())
2020-08-01 15:49:46 +00:00
return
2020-07-28 19:17:07 +00:00
}
// Link check tool after init of markdown
if len(os.Args) >= 2 && os.Args[1] == "check" {
2021-07-30 13:43:13 +00:00
app.initMarkdown()
app.checkAllExternalLinks()
2021-06-19 21:54:07 +00:00
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.Error())
return
}
app.shutdown.ShutdownAndWait()
return
}
2021-07-30 13:43:13 +00:00
// Initialize components
2021-09-02 12:49:10 +00:00
app.initComponents(true)
2021-07-30 13:43:13 +00:00
// Start cron hooks
app.startHourlyHooks()
// Start the server
err = app.startServer()
if err != nil {
app.logErrAndQuit("Failed to start server(s):", err.Error())
return
}
// Wait till everything is shutdown
app.shutdown.Wait()
}
2021-09-02 12:49:10 +00:00
func (app *goBlog) initComponents(logging bool) {
2021-07-30 13:43:13 +00:00
var err error
// Log start
2021-09-02 12:49:10 +00:00
if logging {
log.Println("Initialize components...")
}
2021-07-30 13:43:13 +00:00
app.initMarkdown()
if err = app.initTemplateAssets(); err != nil { // Needs minify
app.logErrAndQuit("Failed to init template assets:", err.Error())
return
}
if err = app.initTemplateStrings(); err != nil {
app.logErrAndQuit("Failed to init template translations:", err.Error())
return
}
if err = app.initCache(); err != nil {
app.logErrAndQuit("Failed to init HTTP cache:", err.Error())
2020-10-15 18:54:43 +00:00
return
}
if err = app.initRegexRedirects(); err != nil {
app.logErrAndQuit("Failed to init redirects:", err.Error())
2020-11-26 16:40:40 +00:00
return
}
if err = app.initHTTPLog(); err != nil {
app.logErrAndQuit("Failed to init HTTP logging:", err.Error())
2020-11-17 21:10:13 +00:00
return
}
if err = app.initActivityPub(); err != nil {
app.logErrAndQuit("Failed to init ActivityPub:", err.Error())
return
}
app.initWebmention()
app.initTelegram()
app.initBlogStats()
app.initTTS()
app.initSessions()
2021-11-23 14:23:01 +00:00
app.initIndieAuth()
2021-12-11 18:43:40 +00:00
app.startPostsScheduler()
app.initPostsDeleter()
2022-01-24 08:43:06 +00:00
app.initIndexNow()
2021-07-30 13:43:13 +00:00
// Log finish
2021-09-02 12:49:10 +00:00
if logging {
log.Println("Initialized components")
}
2021-05-07 14:14:15 +00:00
}
2020-08-01 15:49:46 +00:00
2022-03-16 07:28:03 +00:00
func (a *goBlog) logErrAndQuit(v ...any) {
2021-05-07 14:14:15 +00:00
log.Println(v...)
2021-06-19 21:54:07 +00:00
a.shutdown.ShutdownAndWait()
2021-05-07 14:14:15 +00:00
os.Exit(1)
2020-07-28 19:17:07 +00:00
}