mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 17:29:54 +01:00
WatchGet
This commit is contained in:
72
ober.go
72
ober.go
@@ -10,13 +10,18 @@ import (
|
||||
|
||||
"github.com/fasthttp/router"
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/xujiajun/nutsdb"
|
||||
)
|
||||
|
||||
func RunWebserv() {
|
||||
r := router.New()
|
||||
r.GET("/", Start)
|
||||
r.GET("/api/season", Season)
|
||||
r.GET("/api/watch/{user?}", WatchGet)
|
||||
r.POST("/api/register", Register)
|
||||
r.POST("/api/watch/{user}", WatchPost)
|
||||
r.PATCH("/api/watch/{user}", Start)
|
||||
r.DELETE("/api/watch/{user}", Start)
|
||||
log.Fatal(fasthttp.ListenAndServe(":"+strconv.Itoa(*webServerPort), r.Handler))
|
||||
}
|
||||
|
||||
@@ -62,6 +67,58 @@ func Season(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
}
|
||||
|
||||
func WatchGet(ctx *fasthttp.RequestCtx) {
|
||||
usrVal := ctx.UserValue("user")
|
||||
var userId int64
|
||||
if usrVal != nil {
|
||||
// check user exists
|
||||
user, err := ReadUser(fmt.Sprintf("%s", usrVal))
|
||||
if err != nil {
|
||||
ctx.WriteString("Dich gibts nicht")
|
||||
ctx.SetStatusCode(fasthttp.StatusNotFound)
|
||||
return
|
||||
}
|
||||
userId = user.MalID
|
||||
}
|
||||
animes, err := ReadAnimes()
|
||||
if err != nil {
|
||||
// check if no anime were inserted
|
||||
if err != nutsdb.ErrBucketEmpty {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
animes = make([]Anime, 0)
|
||||
}
|
||||
|
||||
// apply single user logic
|
||||
if usrVal != nil {
|
||||
filteredAnimes := make([]Anime, 0)
|
||||
for _, a := range animes {
|
||||
for _, u := range a.Users {
|
||||
if u.MalID == userId {
|
||||
filteredAnimes = append(filteredAnimes, a)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
animes = filteredAnimes
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(animes)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
_, err = ctx.Write(bytes)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetContentType("application/json; charset=utf-8")
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
}
|
||||
|
||||
func Register(ctx *fasthttp.RequestCtx) {
|
||||
if string(ctx.Request.Header.ContentType()) != "application/json" {
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
@@ -75,12 +132,13 @@ func Register(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if register.MalId == 0 || register.Username == "" || register.Secret == "" || register.Sauce == "" {
|
||||
if register.MalID == 0 || register.Username == "" || register.Secret == "" || register.Sauce == "" {
|
||||
ctx.WriteString("Sprich JSON du Hurensohn")
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
calcSauce := fmt.Sprintf("%x", sha512.Sum512([]byte(registerSecret+strconv.Itoa(register.MalId)+register.Username)))
|
||||
|
||||
calcSauce := fmt.Sprintf("%x", sha512.Sum512([]byte(registerSecret+strconv.FormatInt(register.MalID, 10)+register.Username)))
|
||||
if calcSauce != strings.ToLower(register.Sauce) {
|
||||
ctx.WriteString("Möge die Sauce mit dir sein")
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
@@ -100,15 +158,15 @@ func Register(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusExpectationFailed)
|
||||
return
|
||||
}
|
||||
if userData.Data.MalID != register.MalId {
|
||||
ctx.WriteString("Dich gibts net auf MAL")
|
||||
if userData.Data.MalID != register.MalID {
|
||||
ctx.WriteString("Dich gibts nicht auf MAL")
|
||||
ctx.SetStatusCode(fasthttp.StatusExpectationFailed)
|
||||
return
|
||||
}
|
||||
// REGISTER
|
||||
user := UserData{
|
||||
Username: register.Username,
|
||||
MalId: register.MalId,
|
||||
MalID: register.MalID,
|
||||
Secret: register.Secret,
|
||||
}
|
||||
err = SaveUser(&user)
|
||||
@@ -121,6 +179,10 @@ func Register(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
}
|
||||
|
||||
func WatchPost(ctx *fasthttp.RequestCtx) {
|
||||
//
|
||||
}
|
||||
|
||||
func addErrorToCtx(ctx *fasthttp.RequestCtx, err error) {
|
||||
ctx.WriteString(err.Error())
|
||||
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||
|
||||
Reference in New Issue
Block a user