Skip to content

Commit

Permalink
src: handlers: router: added cors wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pandadiestro committed Nov 14, 2024
1 parent dfd0f72 commit 475949e
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 44 deletions.
1 change: 0 additions & 1 deletion stiller-backend/internal/dbutils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type StillerFile struct {

type StillerUser struct {
Id int `json:"id"`
AvatarId int `json:"avatarid"`
TierId int `json:"tierid"`
Displayname string `json:"displayname"`
Username string `json:"username"`
Expand Down
4 changes: 4 additions & 0 deletions stiller-backend/internal/handlers/fileretrieve/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
)

func Nethandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if handleutils.CORS(w, r) {
return
}

user_token := r.Header.Get("token")
user_tk, token_decode_err := jwtutils.Decode(user_token)
if handleutils.RequestLog(token_decode_err, "", http.StatusUnauthorized, &w) {
Expand Down
23 changes: 23 additions & 0 deletions stiller-backend/internal/handlers/handleutils/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package handleutils

import "net/http"

func CORS(w http.ResponseWriter, r *http.Request) bool {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Credentials", "true")
w.Header().Add("Access-Control-Expose-Headers", "*")

if r.Method == http.MethodOptions {
w.Header().Add("Access-Control-Allow-Methods", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Max-Age", "86400")

w.WriteHeader(http.StatusOK)
return true
}

return false
}



34 changes: 20 additions & 14 deletions stiller-backend/internal/handlers/newuser/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package newuser

import (
"errors"
"log"
"net/http"
"os"
"stiller"
Expand Down Expand Up @@ -36,31 +37,29 @@ func newuserScalarFn(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, err
}

new_user := dbutils.StillerUser{
AvatarId: args[0].Int(),
TierId: args[1].Int(),
Username: args[2].Text(),
TierId: args[0].Int(),
Username: args[1].Text(),
Displayname: args[2].Text(),
Mail: args[3].Text(),
Bpasswd: string(bcrypt_pwd),
}

query := `
insert into
user (avatar, tier, displayname, username, mail, bpasswd)
user (tier, displayname, username, mail, bpasswd)
values
(?1, ?2, ?3, ?4, ?5, ?6)
(?1, ?2, ?3, ?4, ?5)
returning
id;`

new_id := int(-1)
sqlitex.ExecuteTransient(db_conn, query, &sqlitex.ExecOptions{
exec_err := sqlitex.ExecuteTransient(db_conn, query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
new_id = stmt.ColumnInt(0)
return nil
},

Args: []any{
new_user.AvatarId,
new_user.TierId,
new_user.Username,
new_user.Displayname,
Expand All @@ -69,6 +68,10 @@ func newuserScalarFn(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, err
},
})

if exec_err != nil {
return sqlite.Value{}, exec_err
}

if new_id == -1 {
return sqlite.Value{}, ErrInQuery
}
Expand All @@ -77,6 +80,10 @@ func newuserScalarFn(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, err
}

func Nethandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if handleutils.CORS(w, r) {
return
}

type ReqPayload struct {
AvatarId int `json:"avatar_id"`
TierId int `json:"tier_id"`
Expand All @@ -85,10 +92,6 @@ func Nethandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
Passwd string `json:"pwd"`
}

type ResPayload struct {
NewId int `json:"newid"`
}

rpayload := ReqPayload{}
unmarshal_err := jsonexp.UnmarshalRead(r.Body, &rpayload, jsonexp.DefaultOptionsV2())
if handleutils.RequestLog(unmarshal_err, "", http.StatusBadRequest, &w) {
Expand All @@ -112,13 +115,12 @@ func Nethandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return
}

query := `select newuser(?1, ?2, ?3, ?4, ?5);`

new_tk := jwtutils.Token{
UserId: -1,
}

sqlitex.ExecuteTransient(new_dbconn, query, &sqlitex.ExecOptions{
query := `select newuser(?1, ?2, ?3, ?4, ?5);`
exec_err := sqlitex.ExecuteTransient(new_dbconn, query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
new_tk.UserId = stmt.ColumnInt(0)
return nil
Expand All @@ -133,6 +135,10 @@ func Nethandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
},
})

if handleutils.RequestLog(exec_err, "", http.StatusInternalServerError, &w) {
return
}

if new_tk.UserId == -1 {
handleutils.GenericLog(nil, "no new user was created")
w.WriteHeader(http.StatusInternalServerError)
Expand Down
4 changes: 4 additions & 0 deletions stiller-backend/internal/handlers/patchfile/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
)

func NetHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if handleutils.CORS(w, r) {
return
}

type ReqPayload struct {
Id int `json:"id"`
Title string `json:"title"`
Expand Down
4 changes: 4 additions & 0 deletions stiller-backend/internal/handlers/upload/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func pushNewFile(fileptr *dbutils.StillerFile) (int, error) {
}

func NetHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if handleutils.CORS(w, r) {
return
}

type ResPayload struct {
Id int `json:"id"`
}
Expand Down
4 changes: 4 additions & 0 deletions stiller-backend/internal/handlers/userlogin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (


func NetHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if handleutils.CORS(w, r) {
return
}

type ReqPayload struct {
Username string `json:"username"`
Pwd string `json:"pwd"`
Expand Down
4 changes: 4 additions & 0 deletions stiller-backend/internal/handlers/userverify/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (


func NetHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if handleutils.CORS(w, r) {
return
}

username := params.ByName("username")
if len(username) == 0 {
w.WriteHeader(http.StatusNotFound)
Expand Down
91 changes: 62 additions & 29 deletions stiller-backend/internal/router/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"net/http"
"stiller/internal/handlers/fileretrieve"
"stiller/internal/handlers/newuser"
"stiller/internal/handlers/patchfile"
Expand All @@ -11,38 +12,70 @@ import (
"github.com/julienschmidt/httprouter"
)

type individualHandler struct {
path string
method string
handlefunc httprouter.Handle
}

var routes = [...]individualHandler{
{
path: "/file/upload",
method: http.MethodPost,
handlefunc: upload.NetHandler,
},
{
path: "/file/update",
method: http.MethodPatch,
handlefunc: patchfile.NetHandler,
},
{
path: "/file/retrieveall",
method: http.MethodGet,
handlefunc: fileretrieve.Nethandler,
},
{
path: "/auth/newuser",
method: http.MethodPost,
handlefunc: newuser.Nethandler,
},
{
path: "/auth/checkuser/:username",
method: http.MethodGet,
handlefunc: userverify.NetHandler,
},
{
path: "/auth/login",
method: http.MethodPost,
handlefunc: userlogin.NetHandler,
},
}

func routerDigest(router *httprouter.Router, ind *individualHandler) {
var fun func(path string, handle httprouter.Handle)
switch ind.method {
case http.MethodPost:
fun = router.POST
case http.MethodGet:
fun = router.GET
case http.MethodHead:
fun = router.HEAD
case http.MethodPatch:
fun = router.PATCH
case http.MethodPut:
fun = router.PUT
}

router.OPTIONS(ind.path, ind.handlefunc)
fun(ind.path, ind.handlefunc)
}

func NewStillerRouter() *httprouter.Router {
new_router := httprouter.New()

new_router.POST(
"/file/upload",
upload.NetHandler,
)

new_router.PATCH(
"/file/update",
patchfile.NetHandler,
)

new_router.GET(
"/file/retrieveall",
fileretrieve.Nethandler,
)

new_router.POST(
"/auth/newuser",
newuser.Nethandler,
)

new_router.GET(
"/auth/checkuser/:username",
userverify.NetHandler,
)

new_router.POST(
"/auth/login",
userlogin.NetHandler,
)
for index := range routes {
routerDigest(new_router, &routes[index])
}

return new_router
}
Expand Down

0 comments on commit 475949e

Please sign in to comment.