subscribe-bot/web/web.go

114 lines
2.5 KiB
Go
Raw Normal View History

2020-10-12 14:12:09 +00:00
package web
import (
"fmt"
2020-10-14 21:14:57 +00:00
"html/template"
2020-10-14 19:08:38 +00:00
"net/http"
"time"
2020-10-12 14:12:09 +00:00
"github.com/dustin/go-humanize"
2020-10-14 19:08:38 +00:00
"github.com/foolin/goview"
"github.com/foolin/goview/supports/ginview"
"github.com/gin-contrib/static"
"github.com/gin-gonic/contrib/sessions"
2020-10-12 14:12:09 +00:00
"github.com/gin-gonic/gin"
"github.com/kofalt/go-memoize"
2020-10-12 14:12:09 +00:00
"subscribe-bot/config"
"subscribe-bot/db"
"subscribe-bot/osuapi"
2020-10-12 14:12:09 +00:00
)
2020-10-14 19:08:38 +00:00
const (
USER_KEY = "user"
)
var (
cache = memoize.NewMemoizer(90*time.Second, 10*time.Minute)
)
2020-10-14 20:58:01 +00:00
type Web struct {
2020-10-14 21:14:57 +00:00
config *config.Config
api *osuapi.Osuapi
hc *http.Client
db *db.Db
2020-10-14 21:14:57 +00:00
version string
2020-10-14 20:58:01 +00:00
}
func RunWeb(config *config.Config, api *osuapi.Osuapi, db *db.Db, version string) {
2020-10-14 20:58:01 +00:00
hc := &http.Client{
2020-10-14 19:08:38 +00:00
Timeout: 10 * time.Second,
}
web := Web{config, api, hc, db, version}
2020-10-14 20:58:01 +00:00
web.Run()
}
func (web *Web) Run() {
if !web.config.Debug {
gin.SetMode(gin.ReleaseMode)
}
2020-10-12 14:12:09 +00:00
r := gin.Default()
2020-10-14 19:08:38 +00:00
r.Use(gin.Recovery())
r.Use(static.Serve("/static", static.LocalFile("web/static", false)))
2020-10-14 20:58:01 +00:00
r.Use(sessions.Sessions("mysession", sessions.NewCookieStore([]byte(web.config.Web.SessionSecret))))
2020-10-14 19:08:38 +00:00
r.HTMLRender = ginview.New(goview.Config{
Root: "web/templates",
2020-10-14 20:58:01 +00:00
Master: "master.html",
DisableCache: web.config.Debug,
2020-10-14 21:14:57 +00:00
Funcs: template.FuncMap{
"GitCommit": func() string {
return web.version
},
"humanize": humanize.Time,
2020-10-14 21:14:57 +00:00
},
2020-10-14 19:08:38 +00:00
})
2020-10-14 20:58:01 +00:00
r.GET("/logout", web.logout)
r.GET("/login", web.login)
r.GET("/login/callback", web.loginCallback)
2020-10-14 19:08:38 +00:00
2020-10-14 20:58:01 +00:00
r.GET("/map/:userId/:mapId/versions", web.mapVersions)
r.GET("/map/:userId/:mapId/patch/:hash", web.mapPatch)
r.GET("/map/:userId/:mapId/zip/:hash", web.mapZip)
2020-10-14 19:08:38 +00:00
r.GET("/", func(c *gin.Context) {
2020-10-14 20:58:01 +00:00
beatmapSets := web.listRepos()
2021-07-22 08:11:48 +00:00
stats := web.db.GetStats()
2020-10-14 19:08:38 +00:00
c.HTML(http.StatusOK, "index.html", gin.H{
2020-10-14 20:58:01 +00:00
"LoggedIn": isLoggedIn(c),
"Beatmapsets": beatmapSets,
2021-07-22 08:11:48 +00:00
"TotalMaps": stats.TotalMaps,
"TotalUsers": stats.TotalUsers,
2020-10-12 14:12:09 +00:00
})
})
2020-10-14 20:58:01 +00:00
addr := fmt.Sprintf("%s:%d", web.config.Web.Host, web.config.Web.Port)
2020-10-12 14:12:09 +00:00
r.Run(addr)
}
2020-10-14 19:08:38 +00:00
2020-10-14 20:58:01 +00:00
func isLoggedIn(c *gin.Context) bool {
session := sessions.Default(c)
var accessToken string
loggedIn := false
accessTokenI := session.Get("access_token")
if accessTokenI != nil {
accessToken = accessTokenI.(string)
if len(accessToken) > 0 {
loggedIn = true
}
}
return loggedIn
}
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
2020-10-14 19:08:38 +00:00
}