main.go 3.5 KB

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