main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. generator.Generators = make(map[string]generator.Generator)
  54. log.Println("Initialize ListGenerator")
  55. generator.Generators["list"] = list.NewListGenerator(config.Config)
  56. log.Println("Initialize DepartmentGenerator")
  57. generator.Generators["department"] = department.NewDepartmentGenerator(&generator.Config{ConfigT: *config.Config})
  58. files, err := filepath.Glob("generator/generators/*")
  59. log.Println("Found the following generators...", files)
  60. gTypes := make([]*orm.GeneratorType, 0)
  61. for _, g := range files {
  62. gTypes = append(gTypes, &orm.GeneratorType{Name: filepath.Base(g)})
  63. }
  64. log.Println("Update generator_types table...")
  65. for _, gt := range gTypes {
  66. var gType []orm.GeneratorType
  67. if err := orm.DB().Where("name = ?", gt.Name).Find(&gType).Error; err != nil {
  68. panic(err)
  69. }
  70. if len(gType) == 0 {
  71. err := orm.DB().Debug().Create(gt).Error
  72. if err != nil {
  73. panic(err)
  74. }
  75. }
  76. }
  77. if err := orm.DB().Find(&gTypes).Error; err != nil {
  78. panic(err)
  79. }
  80. log.Println("Currently registered generators are:")
  81. for _, g := range gTypes {
  82. log.Println(g.Name)
  83. }
  84. log.Println("Starting cron jobs...")
  85. c := cron.New()
  86. syncJob := sync.NewSyncJob(config.Config)
  87. if config.Config.Sync.Schedule == "" {
  88. config.Config.Sync.Schedule = "@every 10m"
  89. }
  90. log.Printf("Adding LDAP Sync Job with schedule set at %s", config.Config.Sync.Schedule)
  91. log.Printf("Sync will run with SAFE mode set to %v", config.Config.Sync.SafeRun)
  92. log.Printf("Sync will run with SEND_MAIL set to %v", config.Config.Sync.SendMail)
  93. log.Printf("Sync will run with SCHEDULE set to %v", config.Config.Sync.Schedule)
  94. c.AddJob(config.Config.Sync.Schedule, syncJob)
  95. c.Start()
  96. log.Println("karmen is listening to port 3000...")
  97. if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, karmen_handlers.Handlers())); err != nil {
  98. panic(err)
  99. }
  100. }