main.go 3.4 KB

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