99 lines
2.3 KiB
Go
99 lines
2.3 KiB
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"github.com/foolin/goview"
|
|
"github.com/foolin/goview/supports/ginview"
|
|
"github.com/gin-contrib/static"
|
|
"github.com/gin-gonic/contrib/sessions"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/kofalt/go-memoize"
|
|
|
|
"subscribe-bot/config"
|
|
"subscribe-bot/db"
|
|
"subscribe-bot/osuapi"
|
|
)
|
|
|
|
const (
|
|
USER_KEY = "user"
|
|
)
|
|
|
|
var (
|
|
cache = memoize.NewMemoizer(90*time.Second, 10*time.Minute)
|
|
)
|
|
|
|
type Web struct {
|
|
config *config.Config
|
|
api *osuapi.Osuapi
|
|
hc *http.Client
|
|
db *db.Db
|
|
version string
|
|
}
|
|
|
|
func RunWeb(config *config.Config, api *osuapi.Osuapi, db *db.Db, version string) {
|
|
hc := &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
|
|
web := Web{config, api, hc, db, version}
|
|
web.Run()
|
|
}
|
|
|
|
func (web *Web) Run() {
|
|
if !web.config.Debug {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
r := gin.Default()
|
|
r.Use(gin.Recovery())
|
|
r.Use(static.Serve("/static", static.LocalFile("web/static", false)))
|
|
r.Use(sessions.Sessions("mysession", sessions.NewCookieStore([]byte(web.config.Web.SessionSecret))))
|
|
|
|
r.HTMLRender = ginview.New(goview.Config{
|
|
Root: "web/templates",
|
|
Master: "master.html",
|
|
DisableCache: web.config.Debug,
|
|
Funcs: template.FuncMap{
|
|
"GitCommit": func() string {
|
|
return web.version
|
|
},
|
|
"humanize": humanize.Time,
|
|
},
|
|
})
|
|
|
|
r.GET("/logout", web.logout)
|
|
r.GET("/login", web.login)
|
|
r.GET("/login/callback", web.loginCallback)
|
|
|
|
r.GET("/map/:userId", web.errorWrap(web.mapperIndex))
|
|
r.GET("/map/:userId/:mapId/versions", web.errorWrap(web.mapVersions))
|
|
r.GET("/map/:userId/:mapId/patch/:hash", web.errorWrap(web.mapPatch))
|
|
r.GET("/map/:userId/:mapId/zip/:hash", web.errorWrap(web.mapZip))
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
beatmapSets := web.listRepos()
|
|
stats := web.db.GetStats()
|
|
web.render(c, http.StatusOK, "index.html", gin.H{
|
|
"Beatmapsets": beatmapSets,
|
|
"TotalMaps": stats.TotalMaps,
|
|
"TotalUsers": stats.TotalUsers,
|
|
})
|
|
})
|
|
|
|
addr := fmt.Sprintf("%s:%d", web.config.Web.Host, web.config.Web.Port)
|
|
r.Run(addr)
|
|
}
|
|
|
|
func (web *Web) listRepos() []db.Beatmapset {
|
|
beatmapSets := make([]db.Beatmapset, 0)
|
|
web.db.IterTrackedBeatmapsets(10, func(beatmapset db.Beatmapset) error {
|
|
beatmapSets = append(beatmapSets, beatmapset)
|
|
return nil
|
|
})
|
|
return beatmapSets
|
|
}
|