Add go backend
This commit is contained in:
37
api/controllers/session.go
Normal file
37
api/controllers/session.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"magmise/models"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/wader/gormstore/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
// key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
|
||||
key = []byte("super-secret-key")
|
||||
store = gormstore.NewOptions(models.DB(), gormstore.Options{}, []byte(os.Getenv("HASHKEY")), []byte("BLOCKKEY"))
|
||||
)
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := store.Get(r, "session")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Authentication goes here
|
||||
// ...
|
||||
// Set user as authenticated
|
||||
session.Values["user"] = "test"
|
||||
session.Values["authenticated"] = true
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
func Logout(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := store.Get(r, "session")
|
||||
|
||||
// Revoke users authentication
|
||||
session.Values["authenticated"] = false
|
||||
session.Save(r, w)
|
||||
}
|
||||
188
api/controllers/user.go
Normal file
188
api/controllers/user.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"magmise/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
}
|
||||
|
||||
// "/user/:username"
|
||||
func (u User) Get(w http.ResponseWriter, r *http.Request) {
|
||||
username := chi.URLParam(r, "username")
|
||||
if username == "" {
|
||||
http.Error(w, "Missing user id", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
user := models.User{Username: username}
|
||||
if user.Read() != nil {
|
||||
http.Error(w, "Not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(user); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
// "/user"
|
||||
func (User) Create(w http.ResponseWriter, r *http.Request) {
|
||||
var user models.User
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if user.Create() != nil {
|
||||
http.Error(w, "failed to create user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// // /users/application/:status/
|
||||
// func (t User) Getusers(w http.ResponseWriter, r *http.Request) {
|
||||
// status := vestigo.Param(r, "status")
|
||||
// offset, err := strconv.ParseInt(vestigo.Param(r, "offset"), 0, 32)
|
||||
// if err != nil {
|
||||
// offset = 0
|
||||
// }
|
||||
|
||||
// limit, err := strconv.ParseInt(vestigo.Param(r, "limit"), 0, 32)
|
||||
// if err != nil {
|
||||
// limit = 100
|
||||
// }
|
||||
|
||||
// var user models.User
|
||||
// switch status {
|
||||
// case "open", "closed":
|
||||
// user = models.User{Status: status}
|
||||
// case "all":
|
||||
// user = models.User{}
|
||||
// default:
|
||||
// http.Error(w, "Missing status", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// users, err := user.ReadAll(int(offset), int(limit))
|
||||
// if err != nil {
|
||||
// http.Error(w, "Not found", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
// if err := json.NewEncoder(w).Encode(users); err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// w.Header().Add("Content-Type", "application/json")
|
||||
// }
|
||||
|
||||
// // "/user/application/:userid"
|
||||
// func (t User) Updateuser(w http.ResponseWriter, r *http.Request) {
|
||||
// userid := vestigo.Param(r, "userid")
|
||||
// if userid == "" {
|
||||
// http.Error(w, "Missing user or discord id", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
// log.Println(r.Body)
|
||||
// var user models.User
|
||||
// if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// log.Println(user)
|
||||
// if err := user.Update(); err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// // "/user/application/:userid"
|
||||
// func (t User) Deleteuser(w http.ResponseWriter, r *http.Request) {
|
||||
// userid := vestigo.Param(r, "userid")
|
||||
// if userid == "" {
|
||||
// http.Error(w, "Missing user or discord id", http.StatusNotFound)
|
||||
// }
|
||||
// user := models.User{userId: userid}
|
||||
|
||||
// if user.Delete() != nil {
|
||||
// http.Error(w, "failed to delete user", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// // /user/:discordid/users/application/:status/
|
||||
// func (t User) GetUserusers(w http.ResponseWriter, r *http.Request) {
|
||||
// discordid, _ := strconv.ParseInt(vestigo.Param(r, "discordid"), 0, 64)
|
||||
// status := vestigo.Param(r, "status")
|
||||
// offset, err := strconv.ParseInt(vestigo.Param(r, "offset"), 0, 32)
|
||||
// if err != nil {
|
||||
// offset = 0
|
||||
// }
|
||||
|
||||
// limit, err := strconv.ParseInt(vestigo.Param(r, "limit"), 0, 32)
|
||||
// if err != nil {
|
||||
// limit = 100
|
||||
// }
|
||||
|
||||
// log.Println(discordid, status)
|
||||
// var user models.User
|
||||
// switch status {
|
||||
// case "open", "closed":
|
||||
// user = models.User{DiscordId: discordid, Status: status}
|
||||
// case "all":
|
||||
// user = models.User{DiscordId: discordid}
|
||||
// default:
|
||||
// http.Error(w, "Missing status", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// users, err := user.ReadAll(int(offset), int(limit))
|
||||
// if err != nil {
|
||||
// http.Error(w, "Not found", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
// if err := json.NewEncoder(w).Encode(users); err != nil {
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// w.Header().Add("Content-Type", "application/json")
|
||||
// }
|
||||
|
||||
// // "/answer/application/:userid/:modalid"
|
||||
|
||||
// func (t User) AddAnswer(w http.ResponseWriter, r *http.Request) {
|
||||
// userid := vestigo.Param(r, "userid")
|
||||
// modalid := vestigo.Param(r, "modalid")
|
||||
// if userid == "" {
|
||||
// http.Error(w, "Missing user or discord id", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
// b, err := ioutil.ReadAll(r.Body)
|
||||
// if err != nil {
|
||||
// http.Error(w, "Missing body", http.StatusNotFound)
|
||||
// }
|
||||
// user := models.User{userId: userid}
|
||||
// if user.Read() != nil {
|
||||
// http.Error(w, "Failed to read user", http.StatusInternalServerError)
|
||||
// }
|
||||
// for _, answer := range user.Answers {
|
||||
// if answer.ModalId == modalid {
|
||||
// answer.Response = string(b)
|
||||
// if answer.Update() != nil {
|
||||
// http.Error(w, "Failed to update answer", http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// answer := models.Answer{ModalId: modalid, Response: string(b)}
|
||||
// user.Answers = append(user.Answers, answer)
|
||||
// if user.Update() != nil {
|
||||
// http.Error(w, "Failed to update user", http.StatusInternalServerError)
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user