91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"justguestbook/internal/configuration"
|
|
"justguestbook/internal/database/sqlite"
|
|
"justguestbook/internal/handlers"
|
|
"justguestbook/pkg/server"
|
|
"log"
|
|
"net/netip"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
var version string
|
|
|
|
var configFilePath *string = flag.String("conf", "/etc/justguestbook.yaml", "path to a configuration file")
|
|
var showVersion *bool = flag.Bool("v", false, "show version")
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
if *showVersion {
|
|
fmt.Println("justguestbookd ver.", version, "\nCopyright (c) 2022 Alexander \"Arav\" Andreev <me@arav.top>")
|
|
return
|
|
}
|
|
|
|
log.SetFlags(0)
|
|
|
|
config, err := configuration.LoadConfiguration(*configFilePath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
if len(config.Owner.Password) == 0 {
|
|
log.Fatalln("empty password is not allowed")
|
|
}
|
|
|
|
db, err := sqlite.New(config.DBPath)
|
|
if err != nil {
|
|
log.Fatalln("failed to init DB:", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
hand := handlers.New(config.Owner.Name, config.Owner.Password, config.AnonymousPosterName, config.PageSize, db, config.CaptchaAddr)
|
|
|
|
srv := server.NewHttpServer()
|
|
|
|
srv.GET("/", hand.Entries)
|
|
srv.POST("/", hand.New)
|
|
srv.PUT("/:entry", hand.Update)
|
|
srv.DELETE("/:entry", hand.Delete)
|
|
srv.POST("/:entry/reply", hand.Reply)
|
|
srv.PUT("/:entry/reply", hand.Update)
|
|
srv.DELETE("/:entry/reply", hand.Delete)
|
|
|
|
var network string
|
|
if !strings.ContainsRune(config.ListenOn, ':') {
|
|
network = "unix"
|
|
defer os.Remove(config.ListenOn)
|
|
} else {
|
|
ap, err := netip.ParseAddrPort(config.ListenOn)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
if ap.Addr().Is4() {
|
|
network = "tcp4"
|
|
} else if ap.Addr().Is6() {
|
|
network = "tcp6"
|
|
}
|
|
}
|
|
|
|
if err := srv.Start(network, config.ListenOn); err != nil {
|
|
log.Fatalln("failed to start a server:", err)
|
|
}
|
|
|
|
doneSignal := make(chan os.Signal, 1)
|
|
signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-doneSignal
|
|
|
|
if err := srv.Stop(); err != nil {
|
|
log.Fatalln("failed to properly shutdown a server:", err)
|
|
}
|
|
|
|
}
|