2018-05-17 18:35:53 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-15 17:56:36 +03:00
|
|
|
"net/http"
|
2018-05-17 18:35:53 +03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-08-15 17:56:36 +03:00
|
|
|
"regexp"
|
2018-05-17 18:35:53 +03:00
|
|
|
"syscall"
|
|
|
|
|
2018-05-31 15:03:57 +03:00
|
|
|
"github.com/getsentry/raven-go"
|
2018-08-14 17:49:01 +03:00
|
|
|
"github.com/gin-contrib/multitemplate"
|
|
|
|
"github.com/gin-gonic/gin"
|
2018-05-17 18:35:53 +03:00
|
|
|
_ "github.com/golang-migrate/migrate/database/postgres"
|
|
|
|
_ "github.com/golang-migrate/migrate/source/file"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
parser.AddCommand("run",
|
|
|
|
"Run mg-telegram",
|
|
|
|
"Run mg-telegram.",
|
|
|
|
&RunCommand{},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCommand struct
|
|
|
|
type RunCommand struct{}
|
|
|
|
|
|
|
|
// Execute command
|
|
|
|
func (x *RunCommand) Execute(args []string) error {
|
2018-05-31 15:03:57 +03:00
|
|
|
config = LoadConfig(options.Config)
|
|
|
|
orm = NewDb(config)
|
|
|
|
logger = newLogger()
|
|
|
|
|
2018-05-17 18:35:53 +03:00
|
|
|
go start()
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c)
|
|
|
|
for sig := range c {
|
|
|
|
switch sig {
|
|
|
|
case os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM:
|
|
|
|
orm.DB.Close()
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
2018-08-14 17:49:01 +03:00
|
|
|
routing := setup()
|
|
|
|
routing.Run(config.HTTPServer.Listen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup() *gin.Engine {
|
|
|
|
loadTranslateFile()
|
2018-08-15 17:56:36 +03:00
|
|
|
setValidation()
|
2018-08-14 17:49:01 +03:00
|
|
|
|
|
|
|
if config.Debug == false {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
if config.Debug {
|
|
|
|
r.Use(gin.Logger())
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Static("/static", "./static")
|
|
|
|
r.HTMLRender = createHTMLRender()
|
|
|
|
|
|
|
|
r.Use(func(c *gin.Context) {
|
|
|
|
setLocale(c.GetHeader("Accept-Language"))
|
|
|
|
})
|
|
|
|
|
|
|
|
errorHandlers := []ErrorHandlerFunc{
|
|
|
|
PanicLogger(),
|
|
|
|
ErrorResponseHandler(),
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:13:01 +03:00
|
|
|
sentry, _ := raven.New(config.SentryDSN)
|
2018-08-14 17:49:01 +03:00
|
|
|
if sentry != nil {
|
2018-08-16 18:13:01 +03:00
|
|
|
errorHandlers = append(errorHandlers, ErrorCaptureHandler(sentry, true))
|
2018-08-14 17:49:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Use(ErrorHandler(errorHandlers...))
|
|
|
|
|
2018-08-15 17:56:36 +03:00
|
|
|
r.GET("/", checkAccountForRequest(), connectHandler)
|
2018-08-14 17:49:01 +03:00
|
|
|
r.GET("/settings/:uid", settingsHandler)
|
2018-08-15 17:56:36 +03:00
|
|
|
r.POST("/save/", checkConnectionForRequest(), saveHandler)
|
|
|
|
r.POST("/create/", checkConnectionForRequest(), createHandler)
|
|
|
|
r.POST("/add-bot/", checkBotForRequest(), addBotHandler)
|
|
|
|
r.POST("/delete-bot/", checkBotForRequest(), deleteBotHandler)
|
2018-08-14 17:49:01 +03:00
|
|
|
r.POST("/actions/activity", activityHandler)
|
|
|
|
r.POST("/telegram/:token", telegramWebhookHandler)
|
|
|
|
r.POST("/webhook/", mgWebhookHandler)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHTMLRender() multitemplate.Renderer {
|
|
|
|
r := multitemplate.NewRenderer()
|
|
|
|
r.AddFromFiles("home", "templates/layout.html", "templates/home.html")
|
|
|
|
r.AddFromFiles("form", "templates/layout.html", "templates/form.html")
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-08-15 17:56:36 +03:00
|
|
|
func checkAccountForRequest() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
rx := regexp.MustCompile(`/+$`)
|
|
|
|
ra := rx.ReplaceAllString(c.Query("account"), ``)
|
|
|
|
p := Connection{
|
|
|
|
APIURL: ra,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Set("account", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkBotForRequest() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
var b Bot
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&b); err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Token == "" {
|
2018-08-16 18:13:01 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("no_bot_token")})
|
2018-08-15 17:56:36 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Set("bot", b)
|
2018-08-14 17:49:01 +03:00
|
|
|
}
|
2018-08-15 17:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkConnectionForRequest() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
var conn Connection
|
|
|
|
|
2018-08-16 18:13:01 +03:00
|
|
|
if err := c.ShouldBindJSON(&conn); err != nil {
|
2018-08-15 17:56:36 +03:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": getLocalizedMessage("incorrect_url_key")})
|
|
|
|
return
|
2018-08-14 17:49:01 +03:00
|
|
|
}
|
2018-08-15 17:56:36 +03:00
|
|
|
|
|
|
|
c.Set("connection", conn)
|
2018-08-14 17:49:01 +03:00
|
|
|
}
|
2018-05-17 18:35:53 +03:00
|
|
|
}
|