main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "os"
  14. "os/signal"
  15. "syscall"
  16. )
  17. //go:embed translations
  18. var feedTranslations embed.FS
  19. func main() {
  20. if len(os.Args) == 1 {
  21. fmt.Println("szczanieckiej [-c <config>] <command> ")
  22. os.Exit(1)
  23. }
  24. configFilePath := "/etc/szczanieckiej.toml"
  25. command := os.Args[1]
  26. if os.Args[1] == "-c" {
  27. configFilePath = os.Args[2]
  28. command = os.Args[3]
  29. }
  30. cfg, err := config.Read(configFilePath)
  31. if err != nil {
  32. log.Printf("Error while reading config file, %v\n", err)
  33. os.Exit(1)
  34. }
  35. t := traffic.Traffic{}
  36. traffic.EnableFeeds(cfg, &t)
  37. switch command {
  38. case "convert":
  39. err := traffic.Prepare(cfg, t, -1, feedTranslations)
  40. if err != nil {
  41. log.Println(err)
  42. os.Exit(1)
  43. }
  44. case "serve":
  45. fmt.Println("The wheels on the bus go round and round")
  46. c := make(chan os.Signal, 1)
  47. d := make(chan bool)
  48. i := make(chan bool, 1)
  49. signal.Notify(c, os.Interrupt, syscall.SIGUSR1)
  50. go traffic.Initialise(c, d, i, cfg, &t)
  51. c <- syscall.SIGUSR1
  52. srv := server.RoutePreinit(cfg)
  53. <-i
  54. if err := srv.Shutdown(context.Background()); err != nil {
  55. panic(err)
  56. }
  57. srv = server.Route(cfg, &t)
  58. <-d
  59. if err := srv.Shutdown(context.Background()); err != nil {
  60. panic(err)
  61. }
  62. fmt.Println("\nNothing Arrived")
  63. }
  64. }