WIP
This commit is contained in:
@@ -6,31 +6,32 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type Middleware func(http.HandlerFunc) http.HandlerFunc
|
||||
type Middleware func(http.Handler) http.Handler
|
||||
|
||||
func MultipleMiddleware(h http.HandlerFunc, m ...Middleware) http.HandlerFunc {
|
||||
func MultipleMiddleware(h http.Handler, m ...Middleware) http.Handler {
|
||||
if len(m) < 1 {
|
||||
return h
|
||||
}
|
||||
|
||||
wrapped := h
|
||||
|
||||
for i := len(m) - 1; i >= 0; i-- {
|
||||
wrapped = m[i](wrapped)
|
||||
}
|
||||
|
||||
return wrapped
|
||||
}
|
||||
|
||||
func authMiddleware(h http.HandlerFunc) http.HandlerFunc {
|
||||
func authMiddleware(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.Println(r.Method, r.URL)
|
||||
h.ServeHTTP(w, r)
|
||||
_, ok := ValidateUserToken(r)
|
||||
|
||||
if ok {
|
||||
next.ServeHTTP(w, r)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func logMiddleware(h http.HandlerFunc) http.HandlerFunc {
|
||||
func logMiddleware(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.Println(r.Method, r.URL)
|
||||
|
||||
@@ -3,27 +3,55 @@ package routes
|
||||
import (
|
||||
"net/http"
|
||||
"fmt"
|
||||
"reichard.io/imagini/internal/db"
|
||||
// "reichard.io/imagini/internal/db"
|
||||
"github.com/tus/tusd/pkg/filestore"
|
||||
tusd "github.com/tus/tusd/pkg/handler"
|
||||
)
|
||||
|
||||
func RegisterRoutes() {
|
||||
http.HandleFunc("/Users", usersHandler)
|
||||
http.HandleFunc("/Tags", tagsHandler)
|
||||
commonMiddleware := []Middleware{
|
||||
logMiddleware,
|
||||
authMiddleware,
|
||||
}
|
||||
http.Handle("/Users", MultipleMiddleware(usersHandler, commonMiddleware...))
|
||||
http.Handle("/Uploads/", MultipleMiddleware(uploadsHandler, commonMiddleware...))
|
||||
|
||||
// Uploads Handler
|
||||
http.Handle("/uploads/", uploadsHandler())
|
||||
// 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)))
|
||||
}
|
||||
|
||||
func tagsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
filters, present := query["filters"]
|
||||
// func tagsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// query := r.URL.Query()
|
||||
// filters, present := query["filters"]
|
||||
// }
|
||||
|
||||
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/hello" {
|
||||
http.Error(w, "404 not found.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "Hello!")
|
||||
}
|
||||
|
||||
func uploadsHandler() http.Handler {
|
||||
store := filestore.FileStore{
|
||||
Path: "./uploads",
|
||||
Path: "./Uploads",
|
||||
}
|
||||
composer := tusd.NewStoreComposer()
|
||||
store.UseIn(composer)
|
||||
@@ -45,12 +73,16 @@ func uploadsHandler() http.Handler {
|
||||
}
|
||||
}()
|
||||
|
||||
return http.StripPrefix("/uploads/", handler)
|
||||
// return func(w http.ResponseWriter, r *http.Request) {
|
||||
// http.StripPrefix("/Uploads/", handler).ServeHTTP(w, r)
|
||||
// };
|
||||
|
||||
return http.StripPrefix("/Uploads/", handler)
|
||||
}
|
||||
|
||||
func processMedia() {
|
||||
var mi db.MediaItem
|
||||
|
||||
// func processMedia() {
|
||||
// var mi db.MediaItem
|
||||
//
|
||||
// TODO:
|
||||
// - Derive Magic -> mediaType
|
||||
// - Create Thumbnail
|
||||
@@ -75,23 +107,10 @@ func processMedia() {
|
||||
//
|
||||
// img = imaging.Fit(img, 240, 160, imaging.Lanczos)
|
||||
// err = imaging.Save(img, "thumbnail.jpg")
|
||||
}
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/hello" {
|
||||
http.Error(w, "404 not found.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, "Method is not supported.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "Hello!")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user