You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.0 KiB

2 weeks ago
2 weeks ago
  1. package main
  2. import (
  3. lucifer3 "git.aiterp.net/lucifer3/server"
  4. "git.aiterp.net/lucifer3/server/events"
  5. "git.aiterp.net/lucifer3/server/services"
  6. "git.aiterp.net/lucifer3/server/services/effectenforcer"
  7. "git.aiterp.net/lucifer3/server/services/httpapiv1"
  8. "git.aiterp.net/lucifer3/server/services/hue"
  9. "git.aiterp.net/lucifer3/server/services/mill"
  10. "git.aiterp.net/lucifer3/server/services/mysqldb"
  11. "git.aiterp.net/lucifer3/server/services/nanoleaf"
  12. "git.aiterp.net/lucifer3/server/services/script"
  13. "git.aiterp.net/lucifer3/server/services/tradfri"
  14. "git.aiterp.net/lucifer3/server/services/uistate"
  15. "log"
  16. "os"
  17. "os/signal"
  18. "strconv"
  19. "syscall"
  20. )
  21. func main() {
  22. bus := lucifer3.EventBus{}
  23. resolver := services.NewResolver()
  24. variables := script.NewVariables()
  25. database, err := mysqldb.Connect(
  26. env("LUCIFER4_DB_HOST"),
  27. envInt("LUCIFER4_DB_PORT"),
  28. env("LUCIFER4_DB_USER"),
  29. env("LUCIFER4_DB_PASSWORD"),
  30. env("LUCIFER4_DB_SCHEMA"),
  31. )
  32. if err != nil {
  33. log.Fatalln("Database failed", err)
  34. }
  35. httpAPI, err := httpapiv1.New(env("LUCIFER4_HTTP_LISTEN"))
  36. if err != nil {
  37. log.Fatalln("HTTP Listen failed", err)
  38. }
  39. bus.JoinPrivileged(resolver)
  40. bus.Join(services.NewTicker())
  41. bus.Join(effectenforcer.NewService(resolver))
  42. bus.Join(nanoleaf.NewService())
  43. bus.Join(hue.NewService())
  44. bus.Join(tradfri.NewService())
  45. bus.Join(mill.NewService())
  46. bus.Join(uistate.NewService())
  47. bus.Join(script.NewService(resolver, variables))
  48. bus.Join(database)
  49. bus.Join(httpAPI)
  50. bus.Join(variables)
  51. bus.RunEvent(events.Started{})
  52. exitSignal := make(chan os.Signal)
  53. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  54. sig := <-exitSignal
  55. log.Println("Received signal", sig)
  56. }
  57. func env(key string) string {
  58. value := os.Getenv(key)
  59. if value == "" {
  60. log.Fatalln("Expected env:", key)
  61. }
  62. return value
  63. }
  64. func envInt(key string) int {
  65. value, err := strconv.Atoi(os.Getenv(key))
  66. if err != nil {
  67. log.Fatalln("Expected numeric env:", key)
  68. }
  69. return value
  70. }