forked from mirrors/homebox
end-to-end testing setup
This commit is contained in:
parent
b4eb7d8ddc
commit
ad4c8c9ab4
41 changed files with 544 additions and 313 deletions
|
@ -1,17 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/hay-kot/content/backend/app/api/base"
|
||||
_ "github.com/hay-kot/content/backend/app/api/docs"
|
||||
v1 "github.com/hay-kot/content/backend/app/api/v1"
|
||||
"github.com/hay-kot/content/backend/internal/repo"
|
||||
"github.com/rs/zerolog/log"
|
||||
httpSwagger "github.com/swaggo/http-swagger" // http-swagger middleware
|
||||
)
|
||||
|
||||
//go:embed all:public/*
|
||||
var public embed.FS
|
||||
|
||||
const prefix = "/api"
|
||||
|
||||
// registerRoutes registers all the routes for the API
|
||||
|
@ -22,54 +32,54 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
|
|||
// =========================================================================
|
||||
// Base Routes
|
||||
|
||||
DumpEmbedContents()
|
||||
|
||||
r.Get("/swagger/*", httpSwagger.Handler(
|
||||
httpSwagger.URL(fmt.Sprintf("%s://%s/swagger/doc.json", a.conf.Swagger.Scheme, a.conf.Swagger.Host)),
|
||||
))
|
||||
|
||||
// Server Favicon
|
||||
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "static/favicon.ico")
|
||||
})
|
||||
|
||||
baseHandler := base.NewBaseController(a.server)
|
||||
r.Get(prefix+"/status", baseHandler.HandleBase(func() bool { return true }, "v1"))
|
||||
|
||||
// =========================================================================
|
||||
// API Version 1
|
||||
|
||||
v1Base := v1.BaseUrlFunc(prefix)
|
||||
v1Ctrl := v1.NewControllerV1(a.services)
|
||||
{
|
||||
v1Handlers := v1.NewControllerV1(a.services)
|
||||
r.Post(v1Base("/users/register"), v1Handlers.HandleUserRegistration())
|
||||
r.Post(v1Base("/users/login"), v1Handlers.HandleAuthLogin())
|
||||
r.Get(v1Base("/status"), v1Ctrl.HandleBase(func() bool { return true }, "v1"))
|
||||
|
||||
r.Post(v1Base("/users/register"), v1Ctrl.HandleUserRegistration())
|
||||
r.Post(v1Base("/users/login"), v1Ctrl.HandleAuthLogin())
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(a.mwAuthToken)
|
||||
r.Get(v1Base("/users/self"), v1Handlers.HandleUserSelf())
|
||||
r.Put(v1Base("/users/self"), v1Handlers.HandleUserUpdate())
|
||||
r.Put(v1Base("/users/self/password"), v1Handlers.HandleUserUpdatePassword())
|
||||
r.Post(v1Base("/users/logout"), v1Handlers.HandleAuthLogout())
|
||||
r.Get(v1Base("/users/refresh"), v1Handlers.HandleAuthRefresh())
|
||||
r.Get(v1Base("/users/self"), v1Ctrl.HandleUserSelf())
|
||||
r.Put(v1Base("/users/self"), v1Ctrl.HandleUserSelfUpdate())
|
||||
r.Delete(v1Base("/users/self"), v1Ctrl.HandleUserSelfDelete())
|
||||
r.Put(v1Base("/users/self/password"), v1Ctrl.HandleUserUpdatePassword())
|
||||
r.Post(v1Base("/users/logout"), v1Ctrl.HandleAuthLogout())
|
||||
r.Get(v1Base("/users/refresh"), v1Ctrl.HandleAuthRefresh())
|
||||
|
||||
r.Get(v1Base("/locations"), v1Handlers.HandleLocationGetAll())
|
||||
r.Post(v1Base("/locations"), v1Handlers.HandleLocationCreate())
|
||||
r.Get(v1Base("/locations/{id}"), v1Handlers.HandleLocationGet())
|
||||
r.Put(v1Base("/locations/{id}"), v1Handlers.HandleLocationUpdate())
|
||||
r.Delete(v1Base("/locations/{id}"), v1Handlers.HandleLocationDelete())
|
||||
r.Get(v1Base("/locations"), v1Ctrl.HandleLocationGetAll())
|
||||
r.Post(v1Base("/locations"), v1Ctrl.HandleLocationCreate())
|
||||
r.Get(v1Base("/locations/{id}"), v1Ctrl.HandleLocationGet())
|
||||
r.Put(v1Base("/locations/{id}"), v1Ctrl.HandleLocationUpdate())
|
||||
r.Delete(v1Base("/locations/{id}"), v1Ctrl.HandleLocationDelete())
|
||||
|
||||
r.Get(v1Base("/labels"), v1Handlers.HandleLabelsGetAll())
|
||||
r.Post(v1Base("/labels"), v1Handlers.HandleLabelsCreate())
|
||||
r.Get(v1Base("/labels/{id}"), v1Handlers.HandleLabelGet())
|
||||
r.Put(v1Base("/labels/{id}"), v1Handlers.HandleLabelUpdate())
|
||||
r.Delete(v1Base("/labels/{id}"), v1Handlers.HandleLabelDelete())
|
||||
r.Get(v1Base("/labels"), v1Ctrl.HandleLabelsGetAll())
|
||||
r.Post(v1Base("/labels"), v1Ctrl.HandleLabelsCreate())
|
||||
r.Get(v1Base("/labels/{id}"), v1Ctrl.HandleLabelGet())
|
||||
r.Put(v1Base("/labels/{id}"), v1Ctrl.HandleLabelUpdate())
|
||||
r.Delete(v1Base("/labels/{id}"), v1Ctrl.HandleLabelDelete())
|
||||
|
||||
r.Get(v1Base("/items"), v1Handlers.HandleItemsGetAll())
|
||||
r.Post(v1Base("/items"), v1Handlers.HandleItemsCreate())
|
||||
r.Get(v1Base("/items/{id}"), v1Handlers.HandleItemGet())
|
||||
r.Put(v1Base("/items/{id}"), v1Handlers.HandleItemUpdate())
|
||||
r.Delete(v1Base("/items/{id}"), v1Handlers.HandleItemDelete())
|
||||
r.Get(v1Base("/items"), v1Ctrl.HandleItemsGetAll())
|
||||
r.Post(v1Base("/items"), v1Ctrl.HandleItemsCreate())
|
||||
r.Get(v1Base("/items/{id}"), v1Ctrl.HandleItemGet())
|
||||
r.Put(v1Base("/items/{id}"), v1Ctrl.HandleItemUpdate())
|
||||
r.Delete(v1Base("/items/{id}"), v1Ctrl.HandleItemDelete())
|
||||
})
|
||||
}
|
||||
|
||||
r.NotFound(NotFoundHandler)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
|
@ -78,7 +88,7 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
|
|||
func (a *app) LogRoutes(r *chi.Mux) {
|
||||
desiredSpaces := 10
|
||||
|
||||
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
||||
walkFunc := func(method string, route string, handler http.Handler, middleware ...func(http.Handler) http.Handler) error {
|
||||
text := "[" + method + "]"
|
||||
|
||||
for len(text) < desiredSpaces {
|
||||
|
@ -93,3 +103,56 @@ func (a *app) LogRoutes(r *chi.Mux) {
|
|||
fmt.Printf("Logging err: %s\n", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
var ErrDir = errors.New("path is dir")
|
||||
|
||||
func init() {
|
||||
mime.AddExtensionType(".js", "application/javascript")
|
||||
mime.AddExtensionType(".mjs", "application/javascript")
|
||||
}
|
||||
|
||||
func tryRead(fs embed.FS, prefix, requestedPath string, w http.ResponseWriter) error {
|
||||
f, err := fs.Open(path.Join(prefix, requestedPath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
stat, _ := f.Stat()
|
||||
if stat.IsDir() {
|
||||
return ErrDir
|
||||
}
|
||||
|
||||
contentType := mime.TypeByExtension(filepath.Ext(requestedPath))
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
_, err = io.Copy(w, f)
|
||||
return err
|
||||
}
|
||||
|
||||
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
|
||||
err := tryRead(public, "public", r.URL.Path, w)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
log.Debug().
|
||||
Str("path", r.URL.Path).
|
||||
Msg("served from embed not found - serving index.html")
|
||||
err = tryRead(public, "public", "index.html", w)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func DumpEmbedContents() {
|
||||
// recursively prints all contents in the embed.FS
|
||||
err := fs.WalkDir(public, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(path)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue