2022-10-19 03:25:43 +04:00
|
|
|
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 {
|
2022-10-21 02:59:34 +04:00
|
|
|
log.Fatalln("failed to init DB:", err)
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2022-10-21 02:59:34 +04:00
|
|
|
hand := handlers.New(config.Owner.Name, config.Owner.Password, config.AnonymousPosterName, config.PageSize, db, config.CaptchaAddr)
|
2022-10-19 03:25:43 +04:00
|
|
|
|
|
|
|
srv := server.NewHttpServer()
|
|
|
|
|
|
|
|
srv.GET("/", hand.Entries)
|
|
|
|
srv.POST("/", hand.New)
|
|
|
|
srv.DELETE("/:entry", hand.Delete)
|
|
|
|
srv.POST("/:entry/reply", hand.Reply)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|