diff --git a/.vscode/settings.json b/.vscode/settings.json index 85f49ba..b330533 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,8 @@ "package.json": "package-lock.json, yarn.lock, .eslintrc.js, tsconfig.json, .prettierrc, .editorconfig, pnpm-lock.yaml, postcss.config.js, tailwind.config.js", "docker-compose.yml": "Dockerfile, .dockerignore, docker-compose.dev.yml, docker-compose.yml", "README.md": "LICENSE, SECURITY.md" - } + }, + "cSpell.words": [ + "debughandlers" + ] } diff --git a/Taskfile.yml b/Taskfile.yml index a3dbd81..4462265 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -17,13 +17,13 @@ tasks: deps: - db:generate cmds: - - cd backend/app/api/ && swag fmt - - cd backend/app/api/ && swag init --dir=./,../../internal,../../pkgs + - cd backend/app/api/static && swag fmt --dir=../ + - cd backend/app/api/static && swag init --dir=../,../../../internal,../../../pkgs - | npx swagger-typescript-api \ --no-client \ --modular \ - --path ./backend/app/api/docs/swagger.json \ + --path ./backend/app/api/static/docs/swagger.json \ --output ./frontend/lib/api/types - python3 ./scripts/process-types.py ./frontend/lib/api/types/data-contracts.ts sources: @@ -34,8 +34,8 @@ tasks: generates: - "./frontend/lib/api/types/data-contracts.ts" - "./backend/ent/schema" - - "./backend/app/api/docs/swagger.json" - - "./backend/app/api/docs/swagger.yaml" + - "./backend/app/api/static/docs/swagger.json" + - "./backend/app/api/static/docs/swagger.yaml" api: desc: Starts the backend api server (depends on generate task) diff --git a/backend/app/api/handlers/debughandlers/debug.go b/backend/app/api/handlers/debughandlers/debug.go new file mode 100644 index 0000000..ffba624 --- /dev/null +++ b/backend/app/api/handlers/debughandlers/debug.go @@ -0,0 +1,16 @@ +package debughandlers + +import ( + "expvar" + "net/http" + "net/http/pprof" +) + +func New(mux *http.ServeMux) { + mux.HandleFunc("/debug/pprof", pprof.Index) + mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + mux.HandleFunc("/debug/pprof/profile", pprof.Profile) + mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + mux.HandleFunc("/debug/pprof/trace", pprof.Trace) + mux.Handle("/debug/vars", expvar.Handler()) +} diff --git a/backend/app/api/v1/controller.go b/backend/app/api/handlers/v1/controller.go similarity index 100% rename from backend/app/api/v1/controller.go rename to backend/app/api/handlers/v1/controller.go diff --git a/backend/app/api/v1/partials.go b/backend/app/api/handlers/v1/partials.go similarity index 100% rename from backend/app/api/v1/partials.go rename to backend/app/api/handlers/v1/partials.go diff --git a/backend/app/api/v1/v1_ctrl_auth.go b/backend/app/api/handlers/v1/v1_ctrl_auth.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_auth.go rename to backend/app/api/handlers/v1/v1_ctrl_auth.go diff --git a/backend/app/api/v1/v1_ctrl_group.go b/backend/app/api/handlers/v1/v1_ctrl_group.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_group.go rename to backend/app/api/handlers/v1/v1_ctrl_group.go diff --git a/backend/app/api/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_items.go rename to backend/app/api/handlers/v1/v1_ctrl_items.go diff --git a/backend/app/api/v1/v1_ctrl_items_attachments.go b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_items_attachments.go rename to backend/app/api/handlers/v1/v1_ctrl_items_attachments.go diff --git a/backend/app/api/v1/v1_ctrl_labels.go b/backend/app/api/handlers/v1/v1_ctrl_labels.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_labels.go rename to backend/app/api/handlers/v1/v1_ctrl_labels.go diff --git a/backend/app/api/v1/v1_ctrl_locations.go b/backend/app/api/handlers/v1/v1_ctrl_locations.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_locations.go rename to backend/app/api/handlers/v1/v1_ctrl_locations.go diff --git a/backend/app/api/v1/v1_ctrl_user.go b/backend/app/api/handlers/v1/v1_ctrl_user.go similarity index 100% rename from backend/app/api/v1/v1_ctrl_user.go rename to backend/app/api/handlers/v1/v1_ctrl_user.go diff --git a/backend/app/api/main.go b/backend/app/api/main.go index 4340f20..9cbff98 100644 --- a/backend/app/api/main.go +++ b/backend/app/api/main.go @@ -2,13 +2,14 @@ package main import ( "context" + "net/http" "os" "path/filepath" "time" atlas "ariga.io/atlas/sql/migrate" "entgo.io/ent/dialect/sql/schema" - "github.com/hay-kot/homebox/backend/app/api/docs" + "github.com/hay-kot/homebox/backend/app/api/static/docs" "github.com/hay-kot/homebox/backend/ent" "github.com/hay-kot/homebox/backend/internal/config" "github.com/hay-kot/homebox/backend/internal/migrations" @@ -153,5 +154,14 @@ func run(cfg *config.Config) error { app.SetupDemo() } + if cfg.Debug.Enabled { + debugrouter := app.debugRouter() + go func() { + if err := http.ListenAndServe(":"+cfg.Debug.Port, debugrouter); err != nil { + log.Fatal().Err(err).Msg("failed to start debug server") + } + }() + } + return app.server.Start(routes) } diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go index b0a1f1c..67882ed 100644 --- a/backend/app/api/routes.go +++ b/backend/app/api/routes.go @@ -11,8 +11,9 @@ import ( "path/filepath" "github.com/go-chi/chi/v5" - _ "github.com/hay-kot/homebox/backend/app/api/docs" - v1 "github.com/hay-kot/homebox/backend/app/api/v1" + "github.com/hay-kot/homebox/backend/app/api/handlers/debughandlers" + v1 "github.com/hay-kot/homebox/backend/app/api/handlers/v1" + _ "github.com/hay-kot/homebox/backend/app/api/static/docs" "github.com/hay-kot/homebox/backend/internal/repo" "github.com/rs/zerolog/log" httpSwagger "github.com/swaggo/http-swagger" // http-swagger middleware @@ -23,10 +24,17 @@ const prefix = "/api" var ( ErrDir = errors.New("path is dir") - //go:embed all:public/* + //go:embed all:static/public/* public embed.FS ) +func (a *app) debugRouter() *http.ServeMux { + dbg := http.NewServeMux() + debughandlers.New(dbg) + + return dbg +} + // registerRoutes registers all the routes for the API func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux { registerMimes() diff --git a/backend/app/api/docs/docs.go b/backend/app/api/static/docs/docs.go similarity index 100% rename from backend/app/api/docs/docs.go rename to backend/app/api/static/docs/docs.go diff --git a/backend/app/api/docs/swagger.json b/backend/app/api/static/docs/swagger.json similarity index 100% rename from backend/app/api/docs/swagger.json rename to backend/app/api/static/docs/swagger.json diff --git a/backend/app/api/docs/swagger.yaml b/backend/app/api/static/docs/swagger.yaml similarity index 100% rename from backend/app/api/docs/swagger.yaml rename to backend/app/api/static/docs/swagger.yaml diff --git a/backend/app/api/public/.gitkeep b/backend/app/api/static/public/.gitkeep similarity index 100% rename from backend/app/api/public/.gitkeep rename to backend/app/api/static/public/.gitkeep diff --git a/backend/internal/config/conf.go b/backend/internal/config/conf.go index f45e19c..8e55756 100644 --- a/backend/internal/config/conf.go +++ b/backend/internal/config/conf.go @@ -24,6 +24,12 @@ type Config struct { Swagger SwaggerConf `yaml:"swagger"` Demo bool `yaml:"demo"` AllowRegistration bool `yaml:"disable_registration" conf:"default:true"` + Debug DebugConf `yaml:"debug"` +} + +type DebugConf struct { + Enabled bool `yaml:"enabled" conf:"default:false"` + Port string `yaml:"port" conf:"default:4000"` } type SwaggerConf struct { diff --git a/frontend/pages/location/[id].vue b/frontend/pages/location/[id].vue index 092ace3..d915e0b 100644 --- a/frontend/pages/location/[id].vue +++ b/frontend/pages/location/[id].vue @@ -124,7 +124,7 @@