2018-08-14 09:25:34 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2018-09-18 15:54:36 +00:00
|
|
|
"fmt"
|
2018-09-02 01:15:27 +00:00
|
|
|
"log"
|
|
|
|
|
2018-09-19 12:23:39 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/config"
|
2018-09-02 01:15:27 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/context"
|
2019-02-20 03:37:26 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/module/utils"
|
2018-09-02 01:15:27 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/router/api"
|
2018-09-15 15:09:41 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/router/google"
|
|
|
|
"git.trj.tw/golang/mtfosbot/router/line"
|
2018-09-21 13:37:33 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/router/private"
|
2018-09-18 15:54:36 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/router/rimg"
|
2018-09-16 15:39:15 +00:00
|
|
|
"git.trj.tw/golang/mtfosbot/router/twitch"
|
2018-10-12 08:15:47 +00:00
|
|
|
"github.com/DeanThompson/ginpprof"
|
2018-08-14 09:25:34 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
2018-09-02 01:15:27 +00:00
|
|
|
"github.com/gin-gonic/contrib/sessions"
|
2018-08-14 09:25:34 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewServ - create new gin server
|
|
|
|
func NewServ() *gin.Engine {
|
|
|
|
r := gin.New()
|
2018-09-02 01:15:27 +00:00
|
|
|
|
2018-09-19 12:23:39 +00:00
|
|
|
conf := config.GetConf()
|
|
|
|
|
|
|
|
redisStr := fmt.Sprintf("%s:%d", conf.Redis.Host, conf.Redis.Port)
|
|
|
|
|
|
|
|
store, err := sessions.NewRedisStore(10, "tcp", redisStr, "", []byte("seckey"))
|
2018-09-02 01:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 09:25:34 +00:00
|
|
|
// access log
|
|
|
|
r.Use(gin.Logger())
|
|
|
|
// error catch
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
// enable cors
|
2018-10-14 13:56:57 +00:00
|
|
|
corsConfig := cors.DefaultConfig()
|
|
|
|
corsConfig.AllowCredentials = true
|
2018-10-14 14:13:04 +00:00
|
|
|
corsConfig.AllowOrigins = []string{
|
|
|
|
"http://localhost:8080",
|
|
|
|
"http://localhost:8081",
|
|
|
|
"https://bot.trj.tw",
|
|
|
|
}
|
2018-10-14 13:56:57 +00:00
|
|
|
r.Use(cors.New(corsConfig))
|
2018-09-02 01:15:27 +00:00
|
|
|
// session
|
2018-09-02 03:24:40 +00:00
|
|
|
r.Use(sessions.Sessions("ginsess", store))
|
2018-09-02 01:15:27 +00:00
|
|
|
|
2018-08-14 09:25:34 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoutes - set routes
|
|
|
|
func SetRoutes(r *gin.Engine) {
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"message": "ok",
|
|
|
|
})
|
|
|
|
})
|
2018-09-02 01:15:27 +00:00
|
|
|
|
2018-09-18 15:54:36 +00:00
|
|
|
imageProcGroup := r.Group("/image")
|
|
|
|
{
|
|
|
|
imageProcGroup.GET("/origin/:imgname", context.PatchCtx(rimg.GetOriginImage))
|
2018-09-19 09:26:36 +00:00
|
|
|
imageProcGroup.GET("/thumbnail/:imgname", context.PatchCtx(rimg.GetThumbnailImage))
|
2018-10-02 14:20:34 +00:00
|
|
|
imageProcGroup.GET("/line_log_image/:imgname", context.PatchCtx(rimg.GetLineLogImage))
|
2018-09-18 15:54:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 01:15:27 +00:00
|
|
|
apiGroup := r.Group("/api")
|
|
|
|
{
|
2019-02-20 03:37:26 +00:00
|
|
|
apiGroup.GET("/memory", func(c *gin.Context) {
|
|
|
|
mem := utils.GetMemoryUsage()
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"runtime": utils.ToMap(mem),
|
|
|
|
})
|
|
|
|
})
|
2018-09-02 01:15:27 +00:00
|
|
|
apiGroup.POST("/login", context.PatchCtx(api.UserLogin))
|
2018-09-16 15:39:15 +00:00
|
|
|
apiGroup.POST("/logout", context.PatchCtx(api.UserLogout))
|
2018-10-14 13:16:58 +00:00
|
|
|
apiGroup.GET("/line/logs", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineMessageLog))
|
|
|
|
apiGroup.GET("/line/groups", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetLineList))
|
2018-12-10 07:13:06 +00:00
|
|
|
apiGroup.GET("/line/cmds", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetCommandList))
|
2018-12-10 08:11:13 +00:00
|
|
|
apiGroup.POST("/line/cmd", context.PatchCtx(api.CheckSession), context.PatchCtx(api.AddLineGroupCommand))
|
2018-12-11 02:17:47 +00:00
|
|
|
apiGroup.DELETE("/line/cmd/:cmd", context.PatchCtx(api.CheckSession), context.PatchCtx(api.DeleteLineGroupCommand))
|
|
|
|
apiGroup.PUT("/line/cmd/:cmd", context.PatchCtx(api.CheckSession), context.PatchCtx(api.EditLineGroupCommand))
|
2018-09-20 17:14:08 +00:00
|
|
|
apiGroup.GET("/session", context.PatchCtx(api.CheckSession), context.PatchCtx(api.GetSessionData))
|
2018-09-18 15:54:36 +00:00
|
|
|
apiGroup.GET("/twitch/channel/:chid/opay/bar", context.PatchCtx(api.GetDonateBarStatus))
|
2018-09-02 01:15:27 +00:00
|
|
|
}
|
2018-09-21 13:37:33 +00:00
|
|
|
|
|
|
|
privateAPIGroup := apiGroup.Group("/private", context.PatchCtx(private.VerifyKey))
|
|
|
|
{
|
|
|
|
privateAPIGroup.GET("/pages", context.PatchCtx(private.GetFacebookPageIDs))
|
|
|
|
privateAPIGroup.POST("/pageposts", context.PatchCtx(private.UpdateFacebookPagePost))
|
|
|
|
}
|
|
|
|
|
2018-09-17 16:35:48 +00:00
|
|
|
apiTwitchGroup := apiGroup.Group("/twitch", context.PatchCtx(api.CheckSession))
|
|
|
|
{
|
|
|
|
apiTwitchGroup.GET("/channels", context.PatchCtx(api.GetChannels), context.PatchCtx(api.GetChannelList))
|
|
|
|
twitchChannelGroup := apiTwitchGroup.Group("/channel/:chid", context.PatchCtx(api.GetChannels))
|
|
|
|
{
|
|
|
|
twitchChannelGroup.GET("/", context.PatchCtx(api.GetChannelData))
|
|
|
|
twitchChannelGroup.PUT("/botjoin", context.PatchCtx(api.BotJoinChannel))
|
|
|
|
twitchChannelGroup.PUT("/opay", context.PatchCtx(api.OpayIDChange))
|
|
|
|
twitchChannelGroup.GET("/opay/setting", context.PatchCtx(api.GetDonateSetting))
|
|
|
|
twitchChannelGroup.PUT("/opay/setting", context.PatchCtx(api.UpdateDonateSetting))
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 15:09:41 +00:00
|
|
|
|
2018-09-20 14:42:22 +00:00
|
|
|
r.POST("/line", context.PatchCtx(line.GetRawBody), context.PatchCtx(line.VerifyLine), context.PatchCtx(line.GetLineMessage))
|
2018-09-15 15:09:41 +00:00
|
|
|
lineApis := r.Group("/line")
|
|
|
|
{
|
|
|
|
lineApis.POST("/", context.PatchCtx(line.GetRawBody), context.PatchCtx(line.VerifyLine), context.PatchCtx(line.GetLineMessage))
|
|
|
|
}
|
|
|
|
|
|
|
|
googleApis := r.Group("/google")
|
|
|
|
{
|
|
|
|
googleApis.GET("/youtube/webhook", context.PatchCtx(google.VerifyWebhook))
|
|
|
|
googleApis.POST("/youtube/webhook", context.PatchCtx(google.GetNotifyWebhook))
|
|
|
|
}
|
2018-09-16 15:39:15 +00:00
|
|
|
|
|
|
|
twitchApis := r.Group("/twitch")
|
|
|
|
{
|
|
|
|
twitchApis.GET("/login", context.PatchCtx(twitch.OAuthLogin))
|
|
|
|
twitchApis.GET("/oauth", context.PatchCtx(twitch.OAuthProc))
|
|
|
|
}
|
2018-10-11 03:19:55 +00:00
|
|
|
|
|
|
|
// set pprof router
|
2018-10-12 08:15:47 +00:00
|
|
|
ginpprof.Wrap(r)
|
2018-08-14 09:25:34 +00:00
|
|
|
}
|