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
|
|
|
|
2021-07-21 23:12:40 +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"
|
2020-10-14 19:46:20 +00:00
|
|
|
"github.com/kofalt/go-memoize"
|
2020-10-12 14:12:09 +00:00
|
|
|
|
|
|
|
"subscribe-bot/config"
|
2021-07-21 23:12:40 +00:00
|
|
|
"subscribe-bot/db"
|
2020-10-14 19:46:20 +00:00
|
|
|
"subscribe-bot/osuapi"
|
2020-10-12 14:12:09 +00:00
|
|
|
)
|
|
|
|
|
2020-10-14 19:08:38 +00:00
|
|
|
const (
|
|
|
|
USER_KEY = "user"
|
|
|
|
)
|
|
|
|
|
2020-10-14 19:46:20 +00:00
|
|
|
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
|
2021-07-21 23:12:40 +00:00
|
|
|
db *db.Db
|
2020-10-14 21:14:57 +00:00
|
|
|
version string
|
2020-10-14 20:58:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 23:12:40 +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,
|
|
|
|
}
|
|
|
|
|
2021-07-21 23:12:40 +00:00
|
|
|
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 {
|
2020-10-14 19:11:07 +00:00
|
|
|
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
|
|
|
|
},
|
2021-07-21 23:12:40 +00:00
|
|
|
"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)
|
2020-10-14 21:49:59 +00:00
|
|
|
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()
|
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),
|
2020-10-14 19:46:20 +00:00
|
|
|
"Beatmapsets": beatmapSets,
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-21 23:12:40 +00:00
|
|
|
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
|
|
|
}
|