Compare commits

...

3 Commits

Author SHA1 Message Date
daru
fd0f8bc8e2 DiscordId + PATCH register + nil fixes 2022-07-17 03:19:14 +02:00
daru
0417ceb1f9 Can provide Discord ID on register 2022-07-16 22:03:58 +02:00
daru
aec582ff8e Keep appointmets longer 2022-07-16 21:37:29 +02:00
7 changed files with 103 additions and 20 deletions

View File

@@ -16,13 +16,15 @@ Hanami's universeller Serien Organizer
| - | - | - | - | - | - | - |
| GET | /api/auth/{user} | Test für Authentifizierung | (status code) | {user} = MAL username | X-HUSO-AUTH | |
| POST | /api/register | Registrieren | RegisterData JSON | | | RegisterData JSON |
| DELETE | /api/register | User löschen | RegisterData JSON | | | RegisterData JSON |
| PATCH | /api/register | Registrierung bearbeiten | RegisterData JSON | | X-HUSO-AUTH | RegisterData JSON |
| DELETE | /api/register | User löschen | RegisterData JSON | | X-HUSO-AUTH | RegisterData JSON |
_RegisterData_
```json
{
"username": "TESTUSERNAME",
"malId": 42069,
"discordId": 111111111111111111,
"secret": "1ef539ed34435873fc964c2c20da84f8793ae3731a154cffb41039f2f061cabe97dea18cfffa51c58f3f564f8e7f0cd0a98d7ba3dddb7301c0e7549626ea43af",
"sauce": "31f3637d8d05ddca7deee89453e3b68a9b74860facd8d5a6768e5ca842571595c8d31e43ce0e996f893578d0bd2b61f3f3820ec03fbb30407e31a2603c887b1"
}

4
go.mod
View File

@@ -16,12 +16,12 @@ require (
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/klauspost/compress v1.15.7 // indirect
github.com/klauspost/compress v1.15.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/xujiajun/mmap-go v1.0.1 // indirect
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

8
go.sum
View File

@@ -22,6 +22,8 @@ github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.7 h1:7cgTQxJCU/vy+oP/E3B9RGbQTgbiVzIJWIKOLoAsPok=
github.com/klauspost/compress v1.15.7/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.15.8 h1:JahtItbkWjf2jzm/T+qgMxkP9EMHsqEUA6vCMGmXvhA=
github.com/klauspost/compress v1.15.8/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -65,8 +67,10 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8=
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@@ -125,7 +125,11 @@ func main() {
} else {
mmDb, err = sql.Open("mysql", conns)
if err != nil || mmDb == nil {
color.Errorln(err.Error())
if err != nil {
color.Errorln(err.Error())
} else {
color.Errorln("No MovieManager DB connection")
}
} else {
mmDb.SetConnMaxLifetime(time.Minute * 3)
mmDb.SetMaxOpenConns(10)

View File

@@ -68,9 +68,10 @@ type WatchUser struct {
}
type UserData struct {
Username string `json:"username"`
MalID int64 `json:"malId"`
Secret string `json:"secret"`
Username string `json:"username"`
MalID int64 `json:"malId"`
DiscordID int64 `json:"discordId"`
Secret string `json:"secret"`
}
type User struct {
@@ -86,10 +87,11 @@ type User struct {
}
type RegisterData struct {
Username string `json:"username"`
MalID int64 `json:"malId"`
Secret string `json:"secret"`
Sauce string `json:"sauce"`
Username string `json:"username"`
MalID int64 `json:"malId"`
DiscordID int64 `json:"discordId"`
Secret string `json:"secret"`
Sauce string `json:"sauce"`
}
type AnimeDetailMal struct {

83
ober.go
View File

@@ -34,6 +34,7 @@ func RunWebserv() {
r.POST("/api/chat/{id}/{user}", Headers(ChatPost))
r.POST("/api/register", Headers(Register))
r.POST("/api/watch/{user}", Headers(WatchPost))
r.PATCH("/api/register", Headers(RegisterUpdate))
r.DELETE("/api/appointment/{user}", Headers(AppointmentDelete))
r.DELETE("/api/register", Headers(UnRegister))
r.DELETE("/api/watch/{user}", Headers(WatchDelete))
@@ -436,9 +437,10 @@ func Register(ctx *fasthttp.RequestCtx) {
}
// REGISTER
user := UserData{
Username: register.Username,
MalID: register.MalID,
Secret: register.Secret,
Username: register.Username,
MalID: register.MalID,
Secret: register.Secret,
DiscordID: register.DiscordID,
}
err = SaveUser(&user)
if err != nil {
@@ -451,6 +453,65 @@ func Register(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
}
func RegisterUpdate(ctx *fasthttp.RequestCtx) {
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
if auth == nil || string(auth) == "" || !strings.Contains(string(ctx.Request.Header.ContentType()), "application/json") {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
body := ctx.PostBody()
var regUpdate RegisterData
err := json.Unmarshal(body, &regUpdate)
if err != nil {
ctx.WriteString(err.Error())
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
if regUpdate.MalID == 0 || regUpdate.Username == "" || regUpdate.Sauce == "" {
ctx.WriteString("Sprich JSON du Hurensohn")
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
legit, _ := GheddoAuth(regUpdate.Username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
}
calcSauce := Sauce(regUpdate.MalID, regUpdate.Username)
if calcSauce != strings.ToLower(regUpdate.Sauce) {
ctx.WriteString("Möge die Sauce mit dir sein")
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
// check user exists
user, err := ReadUser(regUpdate.Username)
if err != nil {
ctx.WriteString("Dich gibts hier nicht wtf")
ctx.SetStatusCode(fasthttp.StatusNotFound)
return
}
if regUpdate.MalID != user.MalID {
ctx.WriteString("MAL id ändern is nich")
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
if regUpdate.Secret != "" {
user.Secret = regUpdate.Secret
}
user.DiscordID = regUpdate.DiscordID
err = SaveUser(user)
if err != nil {
addErrorToCtx(ctx, err)
return
}
ctx.SetBody(body)
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetStatusCode(fasthttp.StatusOK)
}
func AppointmentPost(ctx *fasthttp.RequestCtx) {
processUpdateAppointmentReq(ctx, true)
}
@@ -510,7 +571,8 @@ func AppointmentDelete(ctx *fasthttp.RequestCtx) {
}
func UnRegister(ctx *fasthttp.RequestCtx) {
if !strings.Contains(string(ctx.Request.Header.ContentType()), "application/json") {
auth := ctx.Request.Header.Peek("X-HUSO-AUTH")
if auth == nil || string(auth) == "" || !strings.Contains(string(ctx.Request.Header.ContentType()), "application/json") {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
@@ -527,6 +589,11 @@ func UnRegister(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
legit, _ := GheddoAuth(register.Username, string(auth))
if !legit {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
return
}
calcSauce := Sauce(register.MalID, register.Username)
if calcSauce != strings.ToLower(register.Sauce) {
ctx.WriteString("Möge die Sauce mit dir sein")
@@ -640,7 +707,9 @@ func processUpdateReq(ctx *fasthttp.RequestCtx, update bool) {
var animes []AnimeUser
err := json.Unmarshal(body, &animes)
if err != nil || len(animes) == 0 {
ctx.WriteString(err.Error())
if err != nil {
ctx.WriteString(err.Error())
}
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
@@ -715,7 +784,9 @@ func processUpdateAppointmentReq(ctx *fasthttp.RequestCtx, update bool) {
var appoints []Appointment
err := json.Unmarshal(body, &appoints)
if err != nil || len(appoints) == 0 {
ctx.WriteString(err.Error())
if err != nil {
ctx.WriteString(err.Error())
}
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}

View File

@@ -28,7 +28,7 @@ func Arbeit() {
} else {
cleared := 0
for _, a := range appoints {
if a.Time.Before(time.Now()) {
if a.Time.Before(time.Now().Add(time.Hour * 22)) {
// appointment expired
keyBytes := Int64AndDateToBytes(a.Anime, a.Time)
err = DbDelete(bucketAppoint, string(keyBytes))