mirror of
https://forgejo.merr.is/annika/isl-api.git
synced 2025-12-11 15:03:16 -05:00
Added JWT Auth
Wrote my own JWT auth middleware, since I could not get the go-chi middleware to accept a JWKS instead of a certificate.
This commit is contained in:
parent
ac18b94a86
commit
b5ea01729b
12 changed files with 336 additions and 132 deletions
167
controllers/PowerItemController.go
Normal file
167
controllers/PowerItemController.go
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"forgejo.merr.is/annika/isl-api/entities"
|
||||
"forgejo.merr.is/annika/isl-api/services"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PowerItemController struct {
|
||||
powerItemService *services.PowerItemService
|
||||
}
|
||||
|
||||
func NewPowerItemController(powerItemService *services.PowerItemService) *PowerItemController {
|
||||
controller := &PowerItemController{
|
||||
powerItemService: powerItemService,
|
||||
}
|
||||
return controller
|
||||
}
|
||||
|
||||
func (p *PowerItemController) Add(w http.ResponseWriter, r *http.Request) {
|
||||
var newItem entities.PowerItem
|
||||
err := json.NewDecoder(r.Body).Decode(&newItem)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := p.powerItemService.Add(newItem)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
returnValue, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprint(w, string(returnValue))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) AddMultiple(w http.ResponseWriter, r *http.Request) {
|
||||
var itemType int32 = 3
|
||||
var newItems map[string]entities.PowerItem
|
||||
err := json.NewDecoder(r.Body).Decode(&newItems)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
added, errors := p.powerItemService.AddMultipile(newItems, itemType)
|
||||
if len(errors) != 0 {
|
||||
http.Error(w, "Could not add item", http.StatusInternalServerError)
|
||||
for _, err := range errors {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
returnValue, err := json.Marshal(added)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprint(w, string(returnValue))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) GetAll(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := p.powerItemService.GetAll()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) GetAllAsMap(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := p.powerItemService.GetAll()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
resultMap := make(map[string]entities.PowerItem)
|
||||
for _, curItem := range items {
|
||||
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", curItem.ID.Bytes[0:4], curItem.ID.Bytes[4:6], curItem.ID.Bytes[6:8], curItem.ID.Bytes[8:10], curItem.ID.Bytes[10:16])
|
||||
resultMap[uuid] = curItem
|
||||
}
|
||||
|
||||
data, err := json.Marshal(resultMap)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) GetAllByType(w http.ResponseWriter, r *http.Request) {
|
||||
typeCode, err := strconv.Atoi(chi.URLParam(r, "type"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := p.powerItemService.GetAllByType(typeCode)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) GetAllByTypeAsMap(w http.ResponseWriter, r *http.Request) {
|
||||
typeCode, err := strconv.Atoi(chi.URLParam(r, "type"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
items, err := p.powerItemService.GetAllByType(typeCode)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
resultMap := make(map[string]entities.PowerItem)
|
||||
for _, curItem := range items {
|
||||
uuid := fmt.Sprintf("%x-%x-%x-%x-%x", curItem.ID.Bytes[0:4], curItem.ID.Bytes[4:6], curItem.ID.Bytes[6:8], curItem.ID.Bytes[8:10], curItem.ID.Bytes[10:16])
|
||||
resultMap[uuid] = curItem
|
||||
}
|
||||
|
||||
data, err := json.Marshal(resultMap)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue