+94
-107
@@ -1,8 +1,7 @@
|
||||
// Modified from https://github.com/arina999999997/elichika/blob/463b37a0d69dbe68fff7112fe87105cfb2e260d6/router/router.go
|
||||
package router
|
||||
|
||||
import (
|
||||
"honoka-chan/internal/handler"
|
||||
"honoka-chan/internal/middleware"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -13,12 +12,79 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type HandlerInfo struct {
|
||||
Method string
|
||||
Path string
|
||||
Handlers []gin.HandlerFunc
|
||||
}
|
||||
type SpecialGroupSetup = func(*gin.RouterGroup)
|
||||
type GroupInfo struct {
|
||||
InitialHandlers []gin.HandlerFunc
|
||||
Handlers map[string]HandlerInfo
|
||||
SpecialSetups []SpecialGroupSetup
|
||||
}
|
||||
|
||||
var (
|
||||
groups = map[string]*GroupInfo{}
|
||||
templates []string
|
||||
)
|
||||
|
||||
func initGroup(g string) {
|
||||
_, exist := groups[g]
|
||||
if exist {
|
||||
return
|
||||
}
|
||||
groups[g] = &GroupInfo{
|
||||
InitialHandlers: []gin.HandlerFunc{},
|
||||
Handlers: map[string]HandlerInfo{},
|
||||
SpecialSetups: []SpecialGroupSetup{},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupInfo) AddInitialHandler(handler gin.HandlerFunc) {
|
||||
g.InitialHandlers = append(g.InitialHandlers, handler)
|
||||
}
|
||||
|
||||
func (g *GroupInfo) AddHandler(method, path string, handlers ...gin.HandlerFunc) {
|
||||
_, exist := g.Handlers[method+path]
|
||||
if exist {
|
||||
panic("Multiple handler for path and method: " + method + " " + path)
|
||||
}
|
||||
g.Handlers[method+path] = HandlerInfo{
|
||||
Method: method,
|
||||
Path: path,
|
||||
Handlers: handlers,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupInfo) AddSpecialSetup(specialGroupSetup SpecialGroupSetup) {
|
||||
g.SpecialSetups = append(g.SpecialSetups, specialGroupSetup)
|
||||
}
|
||||
|
||||
func AddInitialHandler(group string, handler gin.HandlerFunc) {
|
||||
initGroup(group)
|
||||
groups[group].AddInitialHandler(handler)
|
||||
}
|
||||
func AddHandler(group, method, path string, handlers ...gin.HandlerFunc) {
|
||||
initGroup(group)
|
||||
groups[group].AddHandler(method, path, handlers...)
|
||||
}
|
||||
|
||||
func AddSpecialSetup(group string, specialGroupSetup SpecialGroupSetup) {
|
||||
initGroup(group)
|
||||
groups[group].AddSpecialSetup(specialGroupSetup)
|
||||
}
|
||||
|
||||
func AddTemplates(path string) {
|
||||
templates = append(templates, path)
|
||||
}
|
||||
|
||||
func SifRouter(r *gin.Engine) {
|
||||
// Static
|
||||
r.Static("/static", "assets/static")
|
||||
r.Static("/static", "static")
|
||||
|
||||
var files []string
|
||||
_ = filepath.Walk("assets/static/templates", func(path string, info os.FileInfo, err error) error {
|
||||
_ = filepath.Walk("static/templates", func(path string, info os.FileInfo, err error) error {
|
||||
if strings.HasSuffix(path, ".html") {
|
||||
files = append(files, path)
|
||||
}
|
||||
@@ -26,116 +92,37 @@ func SifRouter(r *gin.Engine) {
|
||||
})
|
||||
r.LoadHTMLFiles(files...)
|
||||
|
||||
// favicon
|
||||
r.StaticFile("/favicon.ico", "static/images/favicon.ico")
|
||||
|
||||
// session
|
||||
store := cookie.NewStore([]byte("llsif"))
|
||||
r.Use(sessions.Sessions("llsif", store))
|
||||
|
||||
// /
|
||||
r.Any("/", func(ctx *gin.Context) {
|
||||
ctx.String(http.StatusOK, "Hello, world!")
|
||||
})
|
||||
|
||||
// Private APIs
|
||||
v1 := r.Group("v1")
|
||||
{
|
||||
v1.GET("/basic/getcode", handler.GetCode)
|
||||
v1.POST("/account/active", handler.Active)
|
||||
v1.POST("/account/initialize", handler.Initialize)
|
||||
v1.POST("/account/loginauto", handler.LoginAuto)
|
||||
v1.POST("/account/login", handler.AccountLogin)
|
||||
v1.POST("/account/reportRole", handler.ReportRole)
|
||||
v1.POST("/basic/getcode", handler.GetCode)
|
||||
v1.POST("/basic/getProductList", handler.GetProductList)
|
||||
v1.POST("/basic/handshake", handler.Handshake)
|
||||
v1.POST("/basic/loginarea", handler.LoginArea)
|
||||
v1.POST("/basic/publickey", handler.PublicKey)
|
||||
v1.POST("/guest/status", handler.GuestStatus)
|
||||
}
|
||||
r.GET("/agreement/all", handler.Agreement)
|
||||
r.GET("/integration/appReport/initialize", handler.ReportApp)
|
||||
r.POST("/report/ge/app", handler.ReportLog)
|
||||
// Private APIs
|
||||
|
||||
// Server APIs
|
||||
m := r.Group("main.php").Use(middleware.Common)
|
||||
{
|
||||
m.POST("/album/seriesAll", middleware.ParseMultipartForm, handler.AlbumSeriesAll)
|
||||
m.POST("/announce/checkState", middleware.ParseMultipartForm, handler.AnnounceCheckState)
|
||||
m.POST("/api", middleware.ParseMultipartForm, handler.Api)
|
||||
m.POST("/award/set", handler.AwardSet)
|
||||
m.POST("/background/set", handler.BackgroundSet)
|
||||
m.POST("/download/additional", middleware.ParseMultipartForm, handler.DownloadAdditional)
|
||||
m.POST("/download/batch", middleware.ParseMultipartForm, handler.DownloadBatch)
|
||||
m.POST("/download/event", middleware.ParseMultipartForm, handler.DownloadEvent)
|
||||
m.POST("/download/getUrl", middleware.ParseMultipartForm, handler.DownloadUrl)
|
||||
m.POST("/download/update", middleware.ParseMultipartForm, handler.DownloadUpdate)
|
||||
m.POST("/event/eventList", middleware.ParseMultipartForm, handler.EventList)
|
||||
m.POST("/gdpr/get", middleware.ParseMultipartForm, handler.Gdpr)
|
||||
m.POST("/lbonus/execute", handler.LBonusExecute)
|
||||
m.POST("/live/gameover", handler.GameOver)
|
||||
m.POST("/live/partyList", handler.PartyList)
|
||||
m.POST("/live/play", middleware.ParseMultipartForm, handler.PlayLive)
|
||||
m.POST("/live/preciseScore", middleware.ParseMultipartForm, handler.PlayScore)
|
||||
m.POST("/live/reward", middleware.ParseMultipartForm, handler.PlayReward)
|
||||
m.POST("/login/authkey", middleware.AuthKey, handler.AuthKey)
|
||||
m.POST("/login/login", middleware.ParseMultipartForm, middleware.Login, handler.Login)
|
||||
m.POST("/multiunit/scenarioStartup", handler.MultiUnitStartUp)
|
||||
m.POST("/museum/info", middleware.ParseMultipartForm, handler.MuseumInfo)
|
||||
m.POST("/notice/noticeFriendGreeting", middleware.ParseMultipartForm, handler.NoticeFriendGreeting)
|
||||
m.POST("/notice/noticeFriendVariety", middleware.ParseMultipartForm, handler.NoticeFriendVariety)
|
||||
m.POST("/notice/noticeUserGreetingHistory", handler.NoticeUserGreeting)
|
||||
m.POST("/payment/productList", middleware.ParseMultipartForm, handler.ProductList)
|
||||
m.POST("/personalnotice/get", middleware.ParseMultipartForm, handler.PersonalNotice)
|
||||
m.POST("/profile/profileRegister", handler.ProfileRegister)
|
||||
m.POST("/scenario/reward", handler.ScenarioReward)
|
||||
m.POST("/scenario/startup", handler.ScenarioStartup)
|
||||
m.POST("/subscenario/reward", handler.SubScenarioStartup)
|
||||
m.POST("/subscenario/startup", handler.SubScenarioStartup)
|
||||
m.POST("/tos/tosCheck", middleware.ParseMultipartForm, handler.TosCheck)
|
||||
m.POST("/unit/deck", handler.SetDeck)
|
||||
m.POST("/unit/deckName", handler.SetDeckName)
|
||||
m.POST("/unit/favorite", handler.SetDisplayRank)
|
||||
m.POST("/unit/removableSkillEquipment", handler.RemoveSkillEquip)
|
||||
m.POST("/unit/setDisplayRank", handler.SetDisplayRank)
|
||||
m.POST("/unit/wearAccessory", handler.WearAccessory)
|
||||
m.POST("/user/changeName", handler.ChangeName)
|
||||
m.POST("/user/changeNavi", handler.ChangeNavi)
|
||||
m.POST("/user/setNotificationToken", handler.SetNotificationToken)
|
||||
m.POST("/user/userInfo", middleware.ParseMultipartForm, handler.UserInfo)
|
||||
}
|
||||
r.GET("/webview.php/announce/index", handler.AnnounceIndex)
|
||||
// Server APIs
|
||||
|
||||
// Manga
|
||||
// manga
|
||||
r.GET("/manga", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "common/manga.html", gin.H{})
|
||||
})
|
||||
|
||||
// WebUI
|
||||
w := r.Group("admin").Use(middleware.WebAuth)
|
||||
{
|
||||
w.GET("/index", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "admin/index.html", gin.H{
|
||||
"url": strings.Split(ctx.Request.URL.String(), "?")[0],
|
||||
})
|
||||
})
|
||||
w.GET("/login", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "admin/login.html", gin.H{})
|
||||
})
|
||||
w.POST("/login", handler.WebLogin)
|
||||
w.GET("/logout", handler.WebLogout)
|
||||
w.GET("/card", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "admin/card.html", gin.H{
|
||||
"menu": 1,
|
||||
"url": strings.Split(ctx.Request.URL.String(), "?")[0],
|
||||
})
|
||||
})
|
||||
w.GET("/upload", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "admin/upload.html", gin.H{
|
||||
"menu": 1,
|
||||
"url": strings.Split(ctx.Request.URL.String(), "?")[0],
|
||||
})
|
||||
})
|
||||
w.POST("/upload", handler.Upload)
|
||||
// router
|
||||
for groupPath, groupInfo := range groups {
|
||||
groupApi := r.Group(groupPath, groupInfo.InitialHandlers...)
|
||||
for _, handlerInfo := range groupInfo.Handlers {
|
||||
switch handlerInfo.Method {
|
||||
case "POST":
|
||||
groupApi.POST(handlerInfo.Path, handlerInfo.Handlers...)
|
||||
case "GET":
|
||||
groupApi.GET(handlerInfo.Path, handlerInfo.Handlers...)
|
||||
case "PUT":
|
||||
groupApi.PUT(handlerInfo.Path, handlerInfo.Handlers...)
|
||||
case "DELETE":
|
||||
groupApi.DELETE(handlerInfo.Path, handlerInfo.Handlers...)
|
||||
default:
|
||||
panic("Must be GET, POST, PUT or DELETE")
|
||||
}
|
||||
}
|
||||
for _, specialSetup := range groupInfo.SpecialSetups {
|
||||
specialSetup(groupApi)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user