basic login page

This commit is contained in:
Daniel Mason 2021-03-26 21:06:28 +13:00
parent 7e4be938ee
commit 3555d2b768
10 changed files with 448 additions and 49 deletions

View file

@ -10,6 +10,7 @@ import (
"path/filepath"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
// spaHandler - Handles Single Page Applications (React)
@ -32,6 +33,10 @@ var standardLimiter = NewIPRateLimiter(5, 5)
// List of Reverse proxies
var ReverseProxies []string
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
// HandleRequests - Boot HTTP!
func HandleRequests() {
// Create a new router
@ -57,8 +62,15 @@ func HandleRequests() {
spa := spaHandler{staticPath: "web/build", indexPath: "index.html"}
r.PathPrefix("/").Handler(spa)
c := cors.New(cors.Options{
// Grrrr CORS
AllowedOrigins: []string{"*"},
AllowCredentials: true,
})
handler := c.Handler(r)
// Serve it up!
log.Fatal(http.ListenAndServe(":42069", r))
log.Fatal(http.ListenAndServe(":42069", handler))
}
// MIDDLEWARE
@ -82,6 +94,16 @@ func throwBadReq(w http.ResponseWriter, m string) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
// throwUnauthorized - Throws a 403
func throwOkMessage(w http.ResponseWriter, m string) {
jr := jsonResponse{
Err: m,
}
js, _ := json.Marshal(&jr)
err := errors.New(string(js))
http.Error(w, err.Error(), http.StatusOK)
}
// generateJsonMessage - Generates a message:str response
func generateJsonMessage(m string) []byte {
jr := jsonResponse{
@ -139,7 +161,7 @@ func handleRegister(w http.ResponseWriter, r *http.Request) {
ip := getUserIp(r)
err = createUser(&regReq, ip)
if err != nil {
throwBadReq(w, err.Error())
throwOkMessage(w, err.Error())
return
}