main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. var (
  25. db *gorm.DB
  26. err error
  27. models = []interface{}{
  28. &orm.Teacher{},
  29. }
  30. )
  31. func main() {
  32. log.Println("Loading config file...")
  33. err = config.ReadFile("config/config.yaml", config.Config)
  34. if err != nil {
  35. panic(err)
  36. }
  37. log.Println("Starting karmen and waiting for the DB...")
  38. count := MaxNumRetries
  39. wait := true
  40. for wait && count > 0 {
  41. db, err = orm.New(fmt.Sprintf("%s?%s", config.Config.Orm.Connection, config.Config.Orm.Options))
  42. if err != nil {
  43. count--
  44. log.Println(err)
  45. log.Printf("Remaining retries: %d", count)
  46. time.Sleep(time.Second * RetryTimeInterval)
  47. continue
  48. }
  49. wait = false
  50. }
  51. orm.Use(db)
  52. if config.Config.Orm.AutoMigrate {
  53. log.Print("Automigrating...")
  54. orm.AutoMigrate()
  55. }
  56. log.Println("Map models <-> handlers")
  57. if err := orm.MapHandlers(models); err != nil {
  58. panic(err)
  59. }
  60. generator.Generators = make(map[string]generator.Generator)
  61. log.Println("Initialize ListGenerator")
  62. generator.Generators["list"] = list.NewListGenerator(config.Config)
  63. log.Println("Initialize DepartmentGenerator")
  64. generator.Generators["department"] = department.NewDepartmentGenerator(&generator.Config{ConfigT: *config.Config})
  65. files, err := filepath.Glob("generator/generators/*")
  66. if err != nil {
  67. panic(err)
  68. }
  69. log.Println("Found the following generators...", files)
  70. gTypes := make([]*orm.GeneratorType, 0)
  71. for _, g := range files {
  72. gTypes = append(gTypes, &orm.GeneratorType{Name: filepath.Base(g)})
  73. }
  74. log.Println("Update generator_types table...")
  75. for _, gt := range gTypes {
  76. var gType []orm.GeneratorType
  77. if err := orm.DB().Where("name = ?", gt.Name).Find(&gType).Error; err != nil {
  78. panic(err)
  79. }
  80. if len(gType) == 0 {
  81. err := orm.DB().Debug().Create(gt).Error
  82. if err != nil {
  83. panic(err)
  84. }
  85. }
  86. }
  87. if err := orm.DB().Find(&gTypes).Error; err != nil {
  88. panic(err)
  89. }
  90. log.Println("Currently registered generators are:")
  91. for _, g := range gTypes {
  92. log.Println(g.Name)
  93. }
  94. log.Println("Starting cron jobs...")
  95. c := cron.New()
  96. syncJob := sync.NewSyncJob(config.Config)
  97. if config.Config.Sync.Schedule == "" {
  98. config.Config.Sync.Schedule = "@every 10m"
  99. }
  100. log.Printf("Adding LDAP Sync Job with schedule set at %s", config.Config.Sync.Schedule)
  101. log.Printf("Sync will run with SAFE mode set to %v", config.Config.Sync.SafeRun)
  102. log.Printf("Sync will run with SEND_MAIL set to %v", config.Config.Sync.SendMail)
  103. log.Printf("Sync will run with SCHEDULE set to %v", config.Config.Sync.Schedule)
  104. c.AddJob(config.Config.Sync.Schedule, syncJob)
  105. c.Start()
  106. log.Println("karmen is listening to port 3000...")
  107. if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, karmen_handlers.Handlers(models))); err != nil {
  108. panic(err)
  109. }
  110. }