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.

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