main.go 3.5 KB

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