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.

77 lines
1.8 KiB

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