main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package main
  5. import (
  6. "apiote.xyz/p/szczanieckiej/config"
  7. "apiote.xyz/p/szczanieckiej/server"
  8. "apiote.xyz/p/szczanieckiej/traffic"
  9. "context"
  10. "embed"
  11. "fmt"
  12. "log"
  13. "log/slog"
  14. "os"
  15. "os/signal"
  16. "syscall"
  17. )
  18. //go:embed translations
  19. var feedTranslations embed.FS
  20. func main() {
  21. logLevel := new(slog.LevelVar)
  22. logLevel.Set(slog.LevelDebug)
  23. logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))
  24. if len(os.Args) == 1 {
  25. fmt.Println("szczanieckiej [-c <config>] <command> ")
  26. os.Exit(1)
  27. }
  28. configFilePath := "/etc/szczanieckiej.toml"
  29. command := os.Args[1]
  30. if os.Args[1] == "-c" {
  31. configFilePath = os.Args[2]
  32. command = os.Args[3]
  33. }
  34. cfg, err := config.Read(configFilePath)
  35. if err != nil {
  36. logger.LogAttrs(context.Background(), slog.LevelError, "error reading config file", slog.Attr{Key: "error", Value: slog.StringValue(err.Error())})
  37. os.Exit(1)
  38. }
  39. err = logLevel.UnmarshalText([]byte(cfg.LogLevel))
  40. if err != nil {
  41. logger.LogAttrs(context.Background(), slog.LevelError, "error setting log level ("+cfg.LogLevel+") from config", slog.Attr{Key: "error", Value: slog.StringValue(err.Error())})
  42. os.Exit(1)
  43. }
  44. t := traffic.Traffic{}
  45. traffic.EnableFeeds(cfg, &t)
  46. switch command {
  47. case "convert":
  48. err := traffic.Prepare(cfg, t, -1, feedTranslations)
  49. if err != nil {
  50. log.Println(err)
  51. os.Exit(1)
  52. }
  53. case "serve":
  54. logger.LogAttrs(context.Background(), slog.LevelWarn-1, "The wheels on the bus go round and round")
  55. c := make(chan os.Signal, 1)
  56. d := make(chan bool)
  57. i := make(chan bool, 1)
  58. signal.Notify(c, os.Interrupt, syscall.SIGUSR1)
  59. go traffic.Initialise(c, d, i, cfg, &t)
  60. c <- syscall.SIGUSR1
  61. srv := server.RoutePreinit(cfg)
  62. <-i
  63. if err := srv.Shutdown(context.Background()); err != nil {
  64. panic(err)
  65. }
  66. srv = server.Route(cfg, &t, logger)
  67. <-d
  68. if err := srv.Shutdown(context.Background()); err != nil {
  69. panic(err)
  70. }
  71. logger.LogAttrs(context.Background(), slog.LevelWarn-1, "Nothing arrived")
  72. }
  73. }