1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- // SPDX-FileCopyrightText: Adam Evyčędo
- //
- // SPDX-License-Identifier: AGPL-3.0-or-later
- package main
- import (
- "apiote.xyz/p/szczanieckiej/config"
- "apiote.xyz/p/szczanieckiej/server"
- "apiote.xyz/p/szczanieckiej/traffic"
- "context"
- "embed"
- "fmt"
- "log"
- "log/slog"
- "os"
- "os/signal"
- "syscall"
- )
- //go:embed translations
- var feedTranslations embed.FS
- func main() {
- logLevel := new(slog.LevelVar)
- logLevel.Set(slog.LevelDebug)
- logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))
- if len(os.Args) == 1 {
- fmt.Println("szczanieckiej [-c <config>] <command> ")
- os.Exit(1)
- }
- configFilePath := "/etc/szczanieckiej.toml"
- command := os.Args[1]
- if os.Args[1] == "-c" {
- configFilePath = os.Args[2]
- command = os.Args[3]
- }
- cfg, err := config.Read(configFilePath)
- if err != nil {
- logger.LogAttrs(context.Background(), slog.LevelError, "error reading config file", slog.Attr{Key: "error", Value: slog.StringValue(err.Error())})
- os.Exit(1)
- }
- err = logLevel.UnmarshalText([]byte(cfg.LogLevel))
- if err != nil {
- logger.LogAttrs(context.Background(), slog.LevelError, "error setting log level ("+cfg.LogLevel+") from config", slog.Attr{Key: "error", Value: slog.StringValue(err.Error())})
- os.Exit(1)
- }
- t := traffic.Traffic{}
- traffic.EnableFeeds(cfg, &t)
- switch command {
- case "convert":
- err := traffic.Prepare(cfg, t, -1, feedTranslations)
- if err != nil {
- log.Println(err)
- os.Exit(1)
- }
- case "serve":
- logger.LogAttrs(context.Background(), slog.LevelWarn-1, "The wheels on the bus go round and round")
- c := make(chan os.Signal, 1)
- d := make(chan bool)
- i := make(chan bool, 1)
- signal.Notify(c, os.Interrupt, syscall.SIGUSR1)
- go traffic.Initialise(c, d, i, cfg, &t)
- c <- syscall.SIGUSR1
- srv := server.RoutePreinit(cfg)
- <-i
- if err := srv.Shutdown(context.Background()); err != nil {
- panic(err)
- }
- srv = server.Route(cfg, &t, logger)
- <-d
- if err := srv.Shutdown(context.Background()); err != nil {
- panic(err)
- }
- logger.LogAttrs(context.Background(), slog.LevelWarn-1, "Nothing arrived")
- }
- }
|