main.go 3.6 KB

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