main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "time"
  8. "github.com/gorilla/handlers"
  9. "github.com/jinzhu/gorm"
  10. "github.com/robfig/cron"
  11. "gogs.carducci-dante.gov.it/karmen/core/config"
  12. "gogs.carducci-dante.gov.it/karmen/core/cron/sync"
  13. karmen_handlers "gogs.carducci-dante.gov.it/karmen/core/handlers"
  14. "gogs.carducci-dante.gov.it/karmen/core/orm"
  15. )
  16. const (
  17. MaxNumRetries = 20
  18. RetryTimeInterval = 5
  19. )
  20. func main() {
  21. var (
  22. db *gorm.DB
  23. err error
  24. )
  25. log.Println("Loading config file...")
  26. err = config.ReadFile("config/config.yaml", config.Config)
  27. if err != nil {
  28. panic(err)
  29. }
  30. log.Println("Starting karmen and waiting for the DB...")
  31. count := MaxNumRetries
  32. wait := true
  33. for wait && count > 0 {
  34. db, err = orm.New(fmt.Sprintf("%s?%s", config.Config.Orm.Connection, config.Config.Orm.Options))
  35. if err != nil {
  36. count--
  37. log.Println(err)
  38. log.Printf("Remaining retries: %d", count)
  39. time.Sleep(time.Second * RetryTimeInterval)
  40. continue
  41. }
  42. wait = false
  43. }
  44. orm.Use(db)
  45. if config.Config.Orm.AutoMigrate {
  46. log.Print("Automigrating...")
  47. orm.AutoMigrate()
  48. }
  49. log.Println("Starting cron jobs...")
  50. c := cron.New()
  51. syncJob := sync.NewSyncJob(config.Config)
  52. if config.Config.Sync.Schedule == "" {
  53. config.Config.Sync.Schedule = "@every 10m"
  54. }
  55. log.Printf("Adding LDAP Sync Job with schedule set at %s", config.Config.Sync.Schedule)
  56. log.Printf("Sync will run with SAFE mode set to %v", config.Config.Sync.SafeRun)
  57. log.Printf("Sync will run with SEND_MAIL set to %v", config.Config.Sync.SendMail)
  58. log.Printf("Sync will run with SCHEDULE set to %v", config.Config.Sync.Schedule)
  59. c.AddJob(config.Config.Sync.Schedule, syncJob)
  60. c.Start()
  61. log.Println("karmen is listening to port 3000...")
  62. if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, karmen_handlers.Handlers())); err != nil {
  63. panic(err)
  64. }
  65. }