mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-22 00:21:55 +00:00
Add login/logout/register routes
This commit is contained in:
parent
671254223a
commit
28d8d3491a
@ -1,6 +1,7 @@
|
||||
package goscrobble
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@ -19,32 +20,46 @@ type spaHandler struct {
|
||||
// HandleRequests - Boot HTTP!
|
||||
func HandleRequests() {
|
||||
// Create a new router
|
||||
httpRouter := mux.NewRouter().StrictSlash(true)
|
||||
r := mux.NewRouter().StrictSlash(true)
|
||||
|
||||
v1 := r.PathPrefix("/api/v1").Subrouter()
|
||||
// STATIC TOKEN AUTH
|
||||
httpRouter.HandleFunc("/api/v1/ingress/jellyfin", serveEndpoint)
|
||||
// httpRouter.HandleFunc("/api/v1/ingress/jellyfin", serveEndpoint)
|
||||
|
||||
// JWT AUTH?
|
||||
httpRouter.HandleFunc("/api/v1/profile/{id}", serveEndpoint)
|
||||
// JWT SESSION AUTH?
|
||||
// httpRouter.HandleFunc("/api/v1/profile/{id}", serveEndpoint)
|
||||
|
||||
// NO AUTH
|
||||
httpRouter.HandleFunc("/api/v1/login", serveEndpoint)
|
||||
httpRouter.HandleFunc("/api/v1/logout", serveEndpoint)
|
||||
httpRouter.HandleFunc("/api/v1/register", serveEndpoint)
|
||||
v1.HandleFunc("/register", serveEndpoint).Methods("POST")
|
||||
v1.HandleFunc("/login", serveEndpoint).Methods("POST")
|
||||
v1.HandleFunc("/logout", serveEndpoint).Methods("POST")
|
||||
|
||||
// This just prevents it serving frontend over /api
|
||||
r.PathPrefix("/api")
|
||||
|
||||
// SERVE FRONTEND - NO AUTH
|
||||
spa := spaHandler{staticPath: "web/build", indexPath: "index.html"}
|
||||
httpRouter.PathPrefix("/").Handler(spa)
|
||||
r.PathPrefix("/").Handler(spa)
|
||||
|
||||
// Serve it up!
|
||||
log.Fatal(http.ListenAndServe(":42069", httpRouter))
|
||||
log.Fatal(http.ListenAndServe(":42069", r))
|
||||
}
|
||||
|
||||
func serveEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
// Lets trick 'em for now
|
||||
var jsonInput map[string]interface{}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&jsonInput)
|
||||
if err != nil {
|
||||
// If we can't decode. Lets tell them nicely.
|
||||
http.Error(w, "{\"error\":\"Invalid JSON\"}", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Lets trick 'em for now ;) ;)
|
||||
fmt.Fprintf(w, "{}")
|
||||
}
|
||||
|
||||
// ServerHTTP - Frontend server
|
||||
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the absolute path to prevent directory traversal
|
||||
path, err := filepath.Abs(r.URL.Path)
|
||||
|
@ -7,7 +7,7 @@ function App() {
|
||||
<header className="App-header">
|
||||
<img src={logo} className="App-logo" alt="logo" />
|
||||
<p>
|
||||
go-scrobble
|
||||
goscrobble.com
|
||||
</p>
|
||||
<a
|
||||
className="App-link"
|
||||
|
Loading…
Reference in New Issue
Block a user