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.

79 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/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(effectenforcer.NewService(resolver))
  40. bus.Join(nanoleaf.NewService())
  41. bus.Join(hue.NewService())
  42. bus.Join(tradfri.NewService())
  43. bus.Join(uistate.NewService())
  44. bus.Join(script.NewService(resolver, variables))
  45. bus.Join(database)
  46. bus.Join(httpAPI)
  47. bus.Join(variables)
  48. bus.RunEvent(events.Started{})
  49. exitSignal := make(chan os.Signal)
  50. signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
  51. sig := <-exitSignal
  52. log.Println("Received signal", sig)
  53. }
  54. func env(key string) string {
  55. value := os.Getenv(key)
  56. if value == "" {
  57. log.Fatalln("Expected env:", key)
  58. }
  59. return value
  60. }
  61. func envInt(key string) int {
  62. value, err := strconv.Atoi(os.Getenv(key))
  63. if err != nil {
  64. log.Fatalln("Expected numeric env:", key)
  65. }
  66. return value
  67. }