2023-09-18 23:57:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-11-03 23:38:31 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"reichard.io/bbank/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UTCFormatter struct {
|
|
|
|
log.Formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UTCFormatter) Format(e *log.Entry) ([]byte, error) {
|
|
|
|
e.Time = e.Time.UTC()
|
|
|
|
return u.Formatter.Format(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetFormatter(UTCFormatter{&log.TextFormatter{FullTimestamp: true}})
|
|
|
|
|
|
|
|
app := &cli.App{
|
2023-11-03 23:38:31 +00:00
|
|
|
Name: "Book Manager",
|
2023-09-18 23:57:18 +00:00
|
|
|
Usage: "A self hosted e-book progress tracker.",
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "serve",
|
|
|
|
Aliases: []string{"s"},
|
2023-11-03 23:38:31 +00:00
|
|
|
Usage: "Start Book Manager web server.",
|
2023-09-18 23:57:18 +00:00
|
|
|
Action: cmdServer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdServer(ctx *cli.Context) error {
|
2023-11-03 23:38:31 +00:00
|
|
|
log.Info("Starting Book Manager Server")
|
|
|
|
|
|
|
|
// Create Channel
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
done := make(chan struct{})
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
// Start Server
|
2023-09-18 23:57:18 +00:00
|
|
|
server := server.NewServer()
|
2023-11-03 23:38:31 +00:00
|
|
|
server.StartServer(&wg, done)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
// Wait & Close
|
|
|
|
<-interrupt
|
|
|
|
server.StopServer(&wg, done)
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
// Stop Server
|
2023-09-18 23:57:18 +00:00
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|