GoBlog/main.go

154 lines
2.8 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"
"os"
"os/signal"
2021-02-24 12:16:33 +00:00
"runtime/pprof"
2021-01-11 16:45:57 +00:00
"syscall"
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-02-24 12:16:33 +00:00
// Init CPU profiling
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
2021-02-28 07:57:15 +00:00
// Initialize config
log.Println("Initialize configuration...")
2020-07-28 19:17:07 +00:00
err := initConfig()
if err != nil {
log.Fatal(err)
}
2021-02-28 07:57:15 +00:00
// Small tools
if len(os.Args) >= 2 {
if os.Args[1] == "totp-secret" {
key, err := totp.Generate(totp.GenerateOpts{
Issuer: appConfig.Server.PublicAddress,
AccountName: appConfig.User.Nick,
})
if err != nil {
log.Fatal(err.Error())
return
}
log.Println("TOTP-Secret:", key.Secret())
return
}
}
2021-03-13 12:17:42 +00:00
// Init regular garbage collection
initGC()
// Execute pre-start hooks
preStartHooks()
// Initialize everything else
log.Println("Initialize database...")
2020-07-28 19:17:07 +00:00
err = initDatabase()
if err != nil {
log.Fatal(err)
2020-08-01 15:49:46 +00:00
return
2020-07-28 19:17:07 +00:00
}
log.Println("Initialize server components...")
2020-08-01 15:49:46 +00:00
initMinify()
initMarkdown()
2020-09-19 10:57:14 +00:00
err = initTemplateAssets() // Needs minify
if err != nil {
log.Fatal(err)
return
}
err = initTemplateStrings()
if err != nil {
log.Fatal(err)
return
}
err = initRendering() // Needs assets
if err != nil {
log.Fatal(err)
return
}
2020-11-20 14:33:20 +00:00
err = initCache()
if err != nil {
log.Fatal(err)
return
}
2020-10-15 19:12:28 +00:00
err = initRegexRedirects()
2020-10-15 18:54:43 +00:00
if err != nil {
log.Fatal(err)
return
}
2020-11-26 16:40:40 +00:00
err = initHTTPLog()
if err != nil {
log.Fatal(err)
return
}
2020-11-17 21:10:13 +00:00
err = initActivityPub()
if err != nil {
log.Fatal(err)
return
}
2020-11-25 11:36:14 +00:00
err = initWebmention()
if err != nil {
log.Fatal(err)
return
}
2020-11-17 21:10:13 +00:00
initTelegram()
2020-08-01 15:49:46 +00:00
// Start cron hooks
startHourlyHooks()
2020-08-01 15:49:46 +00:00
// Prepare graceful shutdown
quit := make(chan os.Signal, 1)
// Start the server
go func() {
log.Println("Starting server...")
2020-08-01 15:49:46 +00:00
err = startServer()
2020-07-28 19:52:56 +00:00
if err != nil {
2020-08-01 15:49:46 +00:00
log.Println("Failed to start server:")
log.Println(err)
2020-07-28 19:52:56 +00:00
}
2020-08-01 15:49:46 +00:00
quit <- os.Interrupt
2020-07-28 19:17:07 +00:00
}()
2020-08-01 15:49:46 +00:00
// Graceful shutdown
2021-01-11 16:45:57 +00:00
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
2020-08-01 15:49:46 +00:00
<-quit
log.Println("Stopping...")
2021-02-24 12:16:33 +00:00
// Write memory profile
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close()
// runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
2020-08-01 15:49:46 +00:00
// Close DB
err = closeDb()
if err != nil {
log.Fatal(err)
2020-08-01 15:49:46 +00:00
return
}
2020-07-28 19:17:07 +00:00
}