2020-10-12 14:12:09 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2020-10-14 19:08:38 +00:00
|
|
|
"encoding/json"
|
2020-10-12 14:12:09 +00:00
|
|
|
"fmt"
|
2020-10-14 19:08:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"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"
|
|
|
|
|
|
|
|
"subscribe-bot/config"
|
|
|
|
)
|
|
|
|
|
2020-10-14 19:08:38 +00:00
|
|
|
const (
|
|
|
|
USER_KEY = "user"
|
|
|
|
)
|
|
|
|
|
2020-10-12 14:12:09 +00:00
|
|
|
func RunWeb(config *config.Config) {
|
2020-10-14 19:08:38 +00:00
|
|
|
hc := http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2020-10-14 19:11:07 +00:00
|
|
|
if !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)))
|
|
|
|
r.Use(sessions.Sessions("mysession", sessions.NewCookieStore([]byte(config.Web.SessionSecret))))
|
|
|
|
|
|
|
|
r.HTMLRender = ginview.New(goview.Config{
|
|
|
|
Root: "web/templates",
|
|
|
|
DisableCache: config.Debug,
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/login", func(c *gin.Context) {
|
|
|
|
url := url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "osu.ppy.sh",
|
|
|
|
Path: "/oauth/authorize",
|
|
|
|
}
|
|
|
|
q := url.Query()
|
|
|
|
q.Set("client_id", config.Oauth.ClientId)
|
|
|
|
q.Set("redirect_uri", config.Web.ServedAt+"/login/callback")
|
|
|
|
q.Set("response_type", "code")
|
|
|
|
q.Set("scope", "identify public")
|
|
|
|
q.Set("state", "urmom")
|
|
|
|
url.RawQuery = q.Encode()
|
|
|
|
fmt.Println("redirecting to", url.String())
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, url.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/login/callback", func(c *gin.Context) {
|
|
|
|
receivedCode := c.Query("code")
|
|
|
|
|
|
|
|
bodyQuery := url.Values{}
|
|
|
|
bodyQuery.Set("client_id", config.Oauth.ClientId)
|
|
|
|
bodyQuery.Set("client_secret", config.Oauth.ClientSecret)
|
|
|
|
bodyQuery.Set("code", receivedCode)
|
|
|
|
bodyQuery.Set("grant_type", "authorization_code")
|
|
|
|
bodyQuery.Set("redirect_uri", config.Web.ServedAt+"/login/callback")
|
|
|
|
body := strings.NewReader(bodyQuery.Encode())
|
|
|
|
resp, _ := hc.Post("https://osu.ppy.sh/oauth/token", "application/x-www-form-urlencoded", body)
|
|
|
|
respBody, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
type OsuToken struct {
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|
|
|
|
var token OsuToken
|
|
|
|
_ = json.Unmarshal(respBody, &token)
|
|
|
|
fmt.Println("TOKEN", token)
|
|
|
|
|
|
|
|
session := sessions.Default(c)
|
|
|
|
session.Set("access_token", token.AccessToken)
|
|
|
|
session.Save()
|
|
|
|
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/")
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repos, _ := getRepos(config)
|
|
|
|
|
|
|
|
// render with master
|
|
|
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
|
|
|
"LoggedIn": loggedIn,
|
|
|
|
"Repos": repos,
|
2020-10-12 14:12:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", config.Web.Host, config.Web.Port)
|
|
|
|
r.Run(addr)
|
|
|
|
}
|
2020-10-14 19:08:38 +00:00
|
|
|
|
|
|
|
func getRepos(config *config.Config) (repos []string, err error) {
|
|
|
|
repos = make([]string, 0)
|
|
|
|
reposDir := config.Repos
|
|
|
|
users, err := ioutil.ReadDir(reposDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range users {
|
|
|
|
userDir := path.Join(reposDir, user.Name())
|
|
|
|
var maps []os.FileInfo
|
|
|
|
maps, err = ioutil.ReadDir(userDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mapId := range maps {
|
|
|
|
mapDir := path.Join(userDir, mapId.Name())
|
|
|
|
fmt.Println(mapDir)
|
|
|
|
repos = append(repos, user.Name()+"/"+mapId.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|