subscribe-bot/web/web.go

144 lines
2.9 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
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
2020-10-14 19:56:56 +00:00
"sync"
2020-10-14 19:08:38 +00:00
"time"
2020-10-12 14:12:09 +00:00
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/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
version string
2020-10-14 20:58:01 +00:00
}
2020-10-14 21:14:57 +00:00
func RunWeb(config *config.Config, api *osuapi.Osuapi, 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,
}
2020-10-14 21:14:57 +00:00
web := Web{config, api, hc, 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
},
},
2020-10-14 19:08:38 +00:00
})
2020-10-14 20:58:01 +00:00
r.GET("/logout", web.logout)
2020-10-14 20:09:03 +00:00
2020-10-14 20:58:01 +00:00
r.GET("/login", web.login)
2020-10-14 20:09:03 +00:00
2020-10-14 20:58:01 +00:00
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)
2020-10-14 19:08:38 +00:00
2020-10-14 20:58:01 +00:00
r.GET("/map/:userId/:mapId/patch/:hash", web.mapPatch)
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),
"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
}
func (web *Web) listRepos() []osuapi.Beatmapset {
expensive := func() (interface{}, error) {
2020-10-14 19:56:56 +00:00
repos := make([]int, 0)
2020-10-14 20:58:01 +00:00
reposDir := web.config.Repos
users, _ := ioutil.ReadDir(reposDir)
2020-10-14 19:08:38 +00:00
for _, user := range users {
userDir := path.Join(reposDir, user.Name())
var maps []os.FileInfo
maps, _ = ioutil.ReadDir(userDir)
2020-10-14 19:08:38 +00:00
for _, mapId := range maps {
mapDir := path.Join(userDir, mapId.Name())
fmt.Println(mapDir)
id, _ := strconv.Atoi(mapId.Name())
2020-10-14 19:56:56 +00:00
repos = append(repos, id)
}
2020-10-14 19:08:38 +00:00
}
2020-10-14 19:56:56 +00:00
beatmapSets := make([]osuapi.Beatmapset, len(repos))
var wg sync.WaitGroup
for i, repo := range repos {
wg.Add(1)
go func(i int, repo int) {
2020-10-14 20:58:01 +00:00
bs, _ := web.api.GetBeatmapSet(repo)
2020-10-14 19:56:56 +00:00
beatmapSets[i] = bs
wg.Done()
}(i, repo)
}
wg.Wait()
return beatmapSets, nil
2020-10-14 19:08:38 +00:00
}
result, _, _ := cache.Memoize("key1", expensive)
return result.([]osuapi.Beatmapset)
2020-10-14 19:08:38 +00:00
}