mtfosbot/router/routes/routes.go

133 lines
4.7 KiB
Go
Raw Normal View History

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"
"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"
"git.trj.tw/golang/mtfosbot/router/twitch"
"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")
{
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))
apiGroup.POST("/logout", context.PatchCtx(api.UserLogout))
2019-03-29 14:19:39 +00:00
apiGroup.POST("/line/push", context.PatchCtx(api.CheckSession), context.PatchCtx(api.PushLineMessage))
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))
2019-05-22 14:25:09 +00:00
privateAPIGroup.GET("/ig", context.PatchCtx(private.GetInstagramIDs))
privateAPIGroup.POST("/igposts", context.PatchCtx(private.UpdateInstagramPost))
2018-09-21 13:37:33 +00:00
}
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))
}
twitchApis := r.Group("/twitch")
{
twitchApis.GET("/login", context.PatchCtx(twitch.OAuthLogin))
twitchApis.GET("/oauth", context.PatchCtx(twitch.OAuthProc))
2021-06-21 14:18:13 +00:00
twitchApis.POST("/send", context.PatchCtx(twitch.SendToChannel))
}
2018-10-11 03:19:55 +00:00
// set pprof router
ginpprof.Wrap(r)
2018-08-14 09:25:34 +00:00
}