the shrimplest admin api you've ever seen

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-04-16 22:58:39 +01:00
parent 1cbcece3d2
commit c5a2491627
5 changed files with 266 additions and 12 deletions

20
api/api.go Normal file
View file

@ -0,0 +1,20 @@
package api
import (
"net/http"
"html/template"
"arimelody.me/arimelody.me/api/v1/admin"
)
func Handle(writer http.ResponseWriter, req *http.Request, root *template.Template) int {
code := 404;
if req.URL.Path == "/api/v1/admin/login" {
code = admin.HandleLogin(writer, req, root)
}
if code == 404 {
writer.Write([]byte("404 not found"))
}
return code;
}

43
api/v1/admin/admin.go Normal file
View file

@ -0,0 +1,43 @@
package admin
import (
"fmt"
"html/template"
"io"
"net/http"
)
type (
State struct {
Token string
}
)
func CreateState() *State {
return &State{
Token: "you are the WINRAR!!",
}
}
func HandleLogin(writer http.ResponseWriter, req *http.Request, root *template.Template) int {
if req.Method != "POST" {
return 404;
}
body, err := io.ReadAll(req.Body)
if err != nil {
fmt.Printf("failed to parse request body!\n");
return 500;
}
if string(body) != "super epic mega gaming password" {
return 400;
}
state := CreateState();
writer.WriteHeader(200);
writer.Write([]byte(state.Token))
return 200;
}