DB & Route Organization

This commit is contained in:
2021-01-09 19:44:02 -05:00
parent 04924ead5c
commit 96b0c888ed
21 changed files with 430 additions and 192 deletions

9
routes/albums.go Normal file
View File

@@ -0,0 +1,9 @@
package routes
import (
"net/http"
)
func albumsHandler(w http.ResponseWriter, r *http.Request) {
}

13
routes/auth.go Normal file
View File

@@ -0,0 +1,13 @@
package routes
import (
"net/http"
)
func loginHandler(w http.ResponseWriter, r *http.Request) {
}
func logoutHandler(w http.ResponseWriter, r *http.Request) {
}

9
routes/info.go Normal file
View File

@@ -0,0 +1,9 @@
package routes
import (
"net/http"
)
func infoHandler(w http.ResponseWriter, r *http.Request) {
}

9
routes/media_items.go Normal file
View File

@@ -0,0 +1,9 @@
package routes
import (
"net/http"
)
func mediaItemsHandler(w http.ResponseWriter, r *http.Request) {
}

View File

@@ -19,17 +19,17 @@ func MultipleMiddleware(h http.Handler, m ...Middleware) http.Handler {
return wrapped
}
func authMiddleware(h 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)
}
})
}
// func authMiddleware(h 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)
// }
// })
// }
func logMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@@ -2,84 +2,90 @@ package routes
import (
"net/http"
"fmt"
// "reichard.io/imagini/internal/db"
"github.com/tus/tusd/pkg/filestore"
tusd "github.com/tus/tusd/pkg/handler"
)
func RegisterRoutes() {
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)))
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)
}
// func tagsHandler(w http.ResponseWriter, r *http.Request) {
// query := r.URL.Query()
// filters, present := query["filters"]
// Examples:
// [POST] /Login { user: <USER_OR_EMAIL>, password: <PASSWORD> }
// [POST] /Logout
// [GET] /MediaItems
// 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",
// }
// composer := tusd.NewStoreComposer()
// store.UseIn(composer)
//
// handler, err := tusd.NewHandler(tusd.Config{
// BasePath: "/uploads/",
// StoreComposer: composer,
// NotifyCompleteUploads: true,
// })
//
// if err != nil {
// panic(fmt.Errorf("Unable to create handler: %s", err))
// }
//
// go func() {
// for {
// event := <-handler.CompleteUploads
// fmt.Printf("Upload %s finished\n", event.Upload.ID)
// }
// }()
//
// // return func(w http.ResponseWriter, r *http.Request) {
// // http.StripPrefix("/Uploads/", handler).ServeHTTP(w, r)
// // };
//
// return http.StripPrefix("/Uploads/", handler)
// }
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",
}
composer := tusd.NewStoreComposer()
store.UseIn(composer)
handler, err := tusd.NewHandler(tusd.Config{
BasePath: "/uploads/",
StoreComposer: composer,
NotifyCompleteUploads: true,
})
if err != nil {
panic(fmt.Errorf("Unable to create handler: %s", err))
}
go func() {
for {
event := <-handler.CompleteUploads
fmt.Printf("Upload %s finished\n", event.Upload.ID)
}
}()
// 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
//
@@ -108,9 +114,3 @@ func uploadsHandler() http.Handler {
// img = imaging.Fit(img, 240, 160, imaging.Lanczos)
// err = imaging.Save(img, "thumbnail.jpg")
// }

9
routes/tags.go Normal file
View File

@@ -0,0 +1,9 @@
package routes
import (
"net/http"
)
func tagsHandler(w http.ResponseWriter, r *http.Request) {
}

9
routes/upload.go Normal file
View File

@@ -0,0 +1,9 @@
package routes
import (
"net/http"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
}

View File

@@ -5,6 +5,9 @@ import (
)
func usersHandler(w http.ResponseWriter, r *http.Request) {
// TODO:
// - Get current UserID
}
func meHandler(w http.ResponseWriter, r *http.Request) {
}