github.com/retailcrm/mg-bot-helper@v0.0.0-20201229112329-a17255681a84/src/run.go (about) 1 package main 2 3 import ( 4 "net/http" 5 "os" 6 "os/signal" 7 "syscall" 8 9 "github.com/getsentry/raven-go" 10 "github.com/gin-contrib/multitemplate" 11 "github.com/gin-gonic/gin" 12 _ "github.com/golang-migrate/migrate/database/postgres" 13 _ "github.com/golang-migrate/migrate/source/file" 14 ) 15 16 func init() { 17 parser.AddCommand("run", 18 "Run bot", 19 "Run bot.", 20 &RunCommand{}, 21 ) 22 } 23 24 var ( 25 sentry *raven.Client 26 wm = NewWorkersManager() 27 ) 28 29 // RunCommand struct 30 type RunCommand struct{} 31 32 // Execute command 33 func (x *RunCommand) Execute(args []string) error { 34 config = LoadConfig(options.Config) 35 orm = NewDb(config) 36 logger = newLogger() 37 38 go start() 39 40 c := make(chan os.Signal, 1) 41 signal.Notify(c) 42 for sig := range c { 43 switch sig { 44 case os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM: 45 orm.DB.Close() 46 return nil 47 default: 48 } 49 } 50 51 return nil 52 } 53 54 func start() { 55 router := setup() 56 startWS() 57 router.Run(config.HTTPServer.Listen) 58 } 59 60 func setup() *gin.Engine { 61 loadTranslateFile() 62 setValidation() 63 64 if config.Debug == false { 65 gin.SetMode(gin.ReleaseMode) 66 } 67 68 r := gin.New() 69 r.Use(gin.Recovery()) 70 if config.Debug { 71 r.Use(gin.Logger()) 72 } 73 74 r.HTMLRender = createHTMLRender() 75 76 r.Static("/static", "./static") 77 78 r.Use(func(c *gin.Context) { 79 setLocale(c.GetHeader("Accept-Language")) 80 }) 81 82 errorHandlers := []ErrorHandlerFunc{ 83 PanicLogger(), 84 ErrorResponseHandler(), 85 } 86 87 sentry, _ = raven.New(config.SentryDSN) 88 if sentry != nil { 89 errorHandlers = append(errorHandlers, ErrorCaptureHandler(sentry, true)) 90 } 91 92 r.Use(ErrorHandler(errorHandlers...)) 93 94 r.GET("/", checkAccountForRequest(), connectHandler) 95 r.Any("/settings/:uid", settingsHandler) 96 r.POST("/save/", checkConnectionForRequest(), saveHandler) 97 r.POST("/create/", checkConnectionForRequest(), createHandler) 98 r.POST("/bot-settings/", botSettingsHandler) 99 r.POST("/actions/activity", activityHandler) 100 101 return r 102 } 103 104 func createHTMLRender() multitemplate.Renderer { 105 r := multitemplate.NewRenderer() 106 r.AddFromFiles("home", "templates/layout.html", "templates/home.html") 107 r.AddFromFiles("form", "templates/layout.html", "templates/form.html") 108 return r 109 } 110 111 func checkAccountForRequest() gin.HandlerFunc { 112 return func(c *gin.Context) { 113 ra := rx.ReplaceAllString(c.Query("account"), ``) 114 p := Connection{ 115 APIURL: ra, 116 } 117 118 c.Set("account", p) 119 } 120 } 121 122 func checkConnectionForRequest() gin.HandlerFunc { 123 return func(c *gin.Context) { 124 var conn Connection 125 126 if err := c.ShouldBindJSON(&conn); err != nil { 127 c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_url_key")}) 128 return 129 } 130 conn.NormalizeApiUrl() 131 132 c.Set("connection", conn) 133 } 134 } 135 136 func startWS() { 137 res := getActiveConnection() 138 if len(res) > 0 { 139 for _, v := range res { 140 wm.setWorker(v) 141 } 142 } 143 }