WIP
This commit is contained in:
@@ -4,6 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func albumsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) albumsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,70 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"time"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"reichard.io/imagini/internal/auth"
|
||||
"reichard.io/imagini/internal/models"
|
||||
// log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
JSONError(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Decode into Struct
|
||||
var creds models.APICredentials
|
||||
err := json.NewDecoder(r.Body).Decode(&creds)
|
||||
if err != nil {
|
||||
JSONError(w, "Invalid parameters.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate
|
||||
if creds.User == "" || creds.Password == "" {
|
||||
JSONError(w, "Invalid parameters.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Is user already logged in? If so refresh token, if different user, kill session and log in new user?
|
||||
|
||||
// Do login
|
||||
resp := auth.AuthenticateUser(ctx.DB, creds)
|
||||
if resp == true {
|
||||
// Return Success
|
||||
cookie := http.Cookie{
|
||||
Name: "Token",
|
||||
Value: "testToken",
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
JSONSuccess(w, "Login success.", http.StatusOK)
|
||||
}else {
|
||||
// Return Failure
|
||||
JSONError(w, "Invalid credentials.", http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) logoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Do logout
|
||||
|
||||
// TODO: Clear Session Server Side
|
||||
|
||||
// Tell Client to Expire Token
|
||||
cookie := &http.Cookie{
|
||||
Name: "Token",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
HttpOnly: true,
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func infoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) infoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func mediaItemsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) mediaItemsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
104
routes/routes.go
104
routes/routes.go
@@ -1,60 +1,82 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reichard.io/imagini/internal/context"
|
||||
)
|
||||
|
||||
func RegisterRoutes() {
|
||||
http.HandleFunc("/MediaItems", mediaItemsHandler)
|
||||
http.HandleFunc("/Upload", uploadHandler)
|
||||
http.HandleFunc("/Albums", albumsHandler)
|
||||
http.HandleFunc("/Logout", logoutHandler)
|
||||
http.HandleFunc("/Login", loginHandler)
|
||||
http.HandleFunc("/Users", usersHandler)
|
||||
http.HandleFunc("/Tags", tagsHandler)
|
||||
http.HandleFunc("/Info", infoHandler)
|
||||
http.HandleFunc("/Me", meHandler)
|
||||
type ImaginiContext struct {
|
||||
*context.ImaginiContext
|
||||
}
|
||||
|
||||
// Examples:
|
||||
// [POST] /Login { user: <USER_OR_EMAIL>, password: <PASSWORD> }
|
||||
// [POST] /Logout
|
||||
// [GET] /MediaItems
|
||||
func RegisterRoutes(cctx *context.ImaginiContext) {
|
||||
ctx := &ImaginiContext{cctx}
|
||||
http.HandleFunc("/MediaItems", ctx.mediaItemsHandler)
|
||||
http.HandleFunc("/Upload", ctx.uploadHandler)
|
||||
http.HandleFunc("/Albums", ctx.albumsHandler)
|
||||
http.HandleFunc("/Logout", ctx.logoutHandler)
|
||||
http.HandleFunc("/Login", ctx.loginHandler)
|
||||
http.HandleFunc("/Users", ctx.usersHandler)
|
||||
http.HandleFunc("/Tags", ctx.tagsHandler)
|
||||
http.HandleFunc("/Info", ctx.infoHandler)
|
||||
http.HandleFunc("/Me", ctx.meHandler)
|
||||
}
|
||||
|
||||
// commonMiddleware := []Middleware{
|
||||
// logMiddleware,
|
||||
// authMiddleware,
|
||||
// }
|
||||
// http.Handle("/Users", MultipleMiddleware(usersHandler, commonMiddleware...))
|
||||
// http.Handle("/Uploads/", MultipleMiddleware(uploadsHandler, commonMiddleware...))
|
||||
// https://stackoverflow.com/a/59764037
|
||||
func JSONError(w http.ResponseWriter, err string, code int) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"error": err})
|
||||
}
|
||||
|
||||
// // http.HandleFunc("/uploads/", uploadsHandler())
|
||||
// http.Handle("/Uploads/", func(next http.Handler) http.Handler {
|
||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// _, ok := ValidateUserToken(r)
|
||||
func JSONSuccess(w http.ResponseWriter, msg string, code int) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"success": msg})
|
||||
}
|
||||
|
||||
// if ok {
|
||||
// next.ServeHTTP(w, r)
|
||||
// } else {
|
||||
// w.WriteHeader(http.StatusUnauthorized)
|
||||
// }
|
||||
// })
|
||||
// }(http.StripPrefix("/Uploads/", tusHandler)))
|
||||
// METHOD:
|
||||
// switch r.Method {
|
||||
// case http.MethodGet:
|
||||
// // Serve the resource.
|
||||
// case http.MethodPost:
|
||||
// // Create a new record.
|
||||
// case http.MethodPut:
|
||||
// // Update an existing record.
|
||||
// case http.MethodDelete:
|
||||
// // Remove the record.
|
||||
// default:
|
||||
// // Give an error message.
|
||||
// }
|
||||
|
||||
|
||||
// commonMiddleware := []Middleware{
|
||||
// logMiddleware,
|
||||
// authMiddleware,
|
||||
// }
|
||||
// http.Handle("/Users", MultipleMiddleware(usersHandler, commonMiddleware...))
|
||||
// http.Handle("/Uploads/", MultipleMiddleware(uploadsHandler, commonMiddleware...))
|
||||
|
||||
// // http.HandleFunc("/uploads/", uploadsHandler())
|
||||
// http.Handle("/Uploads/", func(next http.Handler) http.Handler {
|
||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// _, ok := ValidateUserToken(r)
|
||||
|
||||
// if ok {
|
||||
// next.ServeHTTP(w, r)
|
||||
// } else {
|
||||
// w.WriteHeader(http.StatusUnauthorized)
|
||||
// }
|
||||
// })
|
||||
// }(http.StripPrefix("/Uploads/", tusHandler)))
|
||||
|
||||
// Filter Example:
|
||||
// query := r.URL.Query()
|
||||
// filters, present := query["filters"]
|
||||
|
||||
// HTTP Errors
|
||||
// if r.Method != "GET" {
|
||||
// http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
// if r.URL.Path != "/hello" {
|
||||
// http.Error(w, "404 not found.", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// func uploadsHandler() http.Handler {
|
||||
// store := filestore.FileStore{
|
||||
// Path: "./Uploads",
|
||||
|
||||
@@ -4,6 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func tagsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) tagsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
@@ -2,12 +2,38 @@ package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func usersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (ctx *ImaginiContext) usersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost {
|
||||
// CREATE
|
||||
} else if r.Method == http.MethodPut {
|
||||
// UPDATE / REPLACE
|
||||
} else if r.Method == http.MethodPatch {
|
||||
// UPDATE / MODIFY
|
||||
} else if r.Method == http.MethodDelete {
|
||||
// DELETE
|
||||
} else if r.Method == http.MethodGet {
|
||||
// GET
|
||||
} else {
|
||||
JSONError(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func meHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (ctx *ImaginiContext) meHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
JSONError(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Get Authenticated User & Return Object
|
||||
authCookie, err := r.Cookie("Token")
|
||||
if err != nil {
|
||||
log.Error("[routes] ", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("[routes] INFO: ", authCookie)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user