main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. "github.com/gorilla/handlers"
  10. "github.com/jinzhu/gorm"
  11. "github.com/robfig/cron"
  12. "gogs.carducci-dante.gov.it/karmen/core/config"
  13. "gogs.carducci-dante.gov.it/karmen/core/cron/sync"
  14. "gogs.carducci-dante.gov.it/karmen/core/generator"
  15. "gogs.carducci-dante.gov.it/karmen/core/generator/generators/department"
  16. "gogs.carducci-dante.gov.it/karmen/core/generator/generators/list"
  17. karmen_handlers "gogs.carducci-dante.gov.it/karmen/core/handlers"
  18. "gogs.carducci-dante.gov.it/karmen/core/orm"
  19. )
  20. const (
  21. MaxNumRetries = 20
  22. RetryTimeInterval = 5
  23. )
  24. func main() {
  25. var (
  26. db *gorm.DB
  27. err error
  28. )
  29. log.Println("Loading config file...")
  30. err = config.ReadFile("config/config.yaml", config.Config)
  31. if err != nil {
  32. panic(err)
  33. }
  34. log.Println("Starting karmen and waiting for the DB...")
  35. count := MaxNumRetries
  36. wait := true
  37. for wait && count > 0 {
  38. db, err = orm.New(fmt.Sprintf("%s?%s", config.Config.Orm.Connection, config.Config.Orm.Options))
  39. if err != nil {
  40. count--
  41. log.Println(err)
  42. log.Printf("Remaining retries: %d", count)
  43. time.Sleep(time.Second * RetryTimeInterval)
  44. continue
  45. }
  46. wait = false
  47. }
  48. orm.Use(db)
  49. if config.Config.Orm.AutoMigrate {
  50. log.Print("Automigrating...")
  51. orm.AutoMigrate()
  52. }
  53. log.Println("Initialize ListGenerator")
  54. generator.Generators = make(map[string]generator.Generator)
  55. generator.Generators["list"] = list.NewListGenerator(config.Config)
  56. log.Println("Initialize DepartmentGenerator")
  57. generator.Generators = make(map[string]generator.Generator)
  58. generator.Generators["department"] = department.NewDepartmentGenerator(&generator.Config{ConfigT: *config.Config})
  59. files, err := filepath.Glob("generator/generators/*")
  60. log.Println("Found the following generators...")
  61. gTypes := make([]*orm.GeneratorType, 0)
  62. for _, g := range files {
  63. gTypes = append(gTypes, &orm.GeneratorType{Name: filepath.Base(g)})
  64. }
  65. log.Println("Update generator_type table...")
  66. for _, gt := range gTypes {
  67. var gTypes []orm.GeneratorType
  68. if err := orm.DB().Find(&gTypes).Error; err != nil {
  69. panic(err)
  70. }
  71. if len(gTypes) == 0 {
  72. err := orm.DB().Create(gt).Error
  73. if err != nil {
  74. panic(err)
  75. }
  76. }
  77. }
  78. log.Println("Starting cron jobs...")
  79. c := cron.New()
  80. syncJob := sync.NewSyncJob(config.Config)
  81. if config.Config.Sync.Schedule == "" {
  82. config.Config.Sync.Schedule = "@every 10m"
  83. }
  84. log.Printf("Adding LDAP Sync Job with schedule set at %s", config.Config.Sync.Schedule)
  85. log.Printf("Sync will run with SAFE mode set to %v", config.Config.Sync.SafeRun)
  86. log.Printf("Sync will run with SEND_MAIL set to %v", config.Config.Sync.SendMail)
  87. log.Printf("Sync will run with SCHEDULE set to %v", config.Config.Sync.Schedule)
  88. c.AddJob(config.Config.Sync.Schedule, syncJob)
  89. c.Start()
  90. log.Println("karmen is listening to port 3000...")
  91. if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, karmen_handlers.Handlers())); err != nil {
  92. panic(err)
  93. }
  94. }