2022-08-30 02:30:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-09-04 02:42:03 +00:00
|
|
|
"embed"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"mime"
|
2022-08-30 02:30:36 +00:00
|
|
|
"net/http"
|
2022-09-04 02:42:03 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-10-25 02:24:18 +00:00
|
|
|
"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"
|
2022-12-03 19:55:00 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/authroles"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
2022-08-30 02:30:36 +00:00
|
|
|
httpSwagger "github.com/swaggo/http-swagger" // http-swagger middleware
|
|
|
|
)
|
|
|
|
|
|
|
|
const prefix = "/api"
|
|
|
|
|
2022-09-04 03:11:37 +00:00
|
|
|
var (
|
|
|
|
ErrDir = errors.New("path is dir")
|
|
|
|
|
2022-10-25 02:24:18 +00:00
|
|
|
//go:embed all:static/public/*
|
2022-09-04 03:11:37 +00:00
|
|
|
public embed.FS
|
|
|
|
)
|
|
|
|
|
2022-10-25 02:24:18 +00:00
|
|
|
func (a *app) debugRouter() *http.ServeMux {
|
|
|
|
dbg := http.NewServeMux()
|
|
|
|
debughandlers.New(dbg)
|
|
|
|
|
|
|
|
return dbg
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// registerRoutes registers all the routes for the API
|
2023-03-21 04:32:10 +00:00
|
|
|
func (a *app) mountRoutes(r *chi.Mux, chain *errchain.ErrChain, repos *repo.AllRepos) {
|
2022-09-04 03:10:42 +00:00
|
|
|
registerMimes()
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get("/swagger/*", httpSwagger.Handler(
|
2023-04-01 23:10:27 +00:00
|
|
|
httpSwagger.URL("/swagger/doc.json"),
|
2023-03-21 04:32:10 +00:00
|
|
|
))
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
// =========================================================================
|
|
|
|
// API Version 1
|
|
|
|
|
2022-08-30 18:05:11 +00:00
|
|
|
v1Base := v1.BaseUrlFunc(prefix)
|
2022-10-30 02:15:35 +00:00
|
|
|
|
|
|
|
v1Ctrl := v1.NewControllerV1(
|
|
|
|
a.services,
|
2022-10-30 04:05:38 +00:00
|
|
|
a.repos,
|
2022-10-12 20:53:22 +00:00
|
|
|
v1.WithMaxUploadSize(a.conf.Web.MaxUploadSize),
|
2022-11-13 23:17:55 +00:00
|
|
|
v1.WithRegistration(a.conf.Options.AllowRegistration),
|
2022-10-12 20:53:22 +00:00
|
|
|
v1.WithDemoStatus(a.conf.Demo), // Disable Password Change in Demo Mode
|
|
|
|
)
|
2022-10-30 02:15:35 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get(v1Base("/status"), chain.ToHandlerFunc(v1Ctrl.HandleBase(func() bool { return true }, v1.Build{
|
2022-10-14 01:01:01 +00:00
|
|
|
Version: version,
|
|
|
|
Commit: commit,
|
|
|
|
BuildTime: buildTime,
|
2023-03-21 04:32:10 +00:00
|
|
|
})))
|
2022-10-12 20:53:22 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Post(v1Base("/users/register"), chain.ToHandlerFunc(v1Ctrl.HandleUserRegistration()))
|
|
|
|
r.Post(v1Base("/users/login"), chain.ToHandlerFunc(v1Ctrl.HandleAuthLogin()))
|
2022-10-12 20:53:22 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
userMW := []errchain.Middleware{
|
2022-12-03 19:55:00 +00:00
|
|
|
a.mwAuthToken,
|
|
|
|
a.mwRoles(RoleModeOr, authroles.RoleUser.String()),
|
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get(v1Base("/users/self"), chain.ToHandlerFunc(v1Ctrl.HandleUserSelf(), userMW...))
|
|
|
|
r.Put(v1Base("/users/self"), chain.ToHandlerFunc(v1Ctrl.HandleUserSelfUpdate(), userMW...))
|
|
|
|
r.Delete(v1Base("/users/self"), chain.ToHandlerFunc(v1Ctrl.HandleUserSelfDelete(), userMW...))
|
|
|
|
r.Post(v1Base("/users/logout"), chain.ToHandlerFunc(v1Ctrl.HandleAuthLogout(), userMW...))
|
|
|
|
r.Get(v1Base("/users/refresh"), chain.ToHandlerFunc(v1Ctrl.HandleAuthRefresh(), userMW...))
|
|
|
|
r.Put(v1Base("/users/self/change-password"), chain.ToHandlerFunc(v1Ctrl.HandleUserSelfChangePassword(), userMW...))
|
2022-10-30 02:15:35 +00:00
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Post(v1Base("/groups/invitations"), chain.ToHandlerFunc(v1Ctrl.HandleGroupInvitationsCreate(), userMW...))
|
|
|
|
r.Get(v1Base("/groups/statistics"), chain.ToHandlerFunc(v1Ctrl.HandleGroupStatistics(), userMW...))
|
|
|
|
r.Get(v1Base("/groups/statistics/purchase-price"), chain.ToHandlerFunc(v1Ctrl.HandleGroupStatisticsPriceOverTime(), userMW...))
|
|
|
|
r.Get(v1Base("/groups/statistics/locations"), chain.ToHandlerFunc(v1Ctrl.HandleGroupStatisticsLocations(), userMW...))
|
|
|
|
r.Get(v1Base("/groups/statistics/labels"), chain.ToHandlerFunc(v1Ctrl.HandleGroupStatisticsLabels(), userMW...))
|
2022-10-30 02:15:35 +00:00
|
|
|
|
|
|
|
// TODO: I don't like /groups being the URL for users
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get(v1Base("/groups"), chain.ToHandlerFunc(v1Ctrl.HandleGroupGet(), userMW...))
|
|
|
|
r.Put(v1Base("/groups"), chain.ToHandlerFunc(v1Ctrl.HandleGroupUpdate(), userMW...))
|
|
|
|
|
|
|
|
r.Post(v1Base("/actions/ensure-asset-ids"), chain.ToHandlerFunc(v1Ctrl.HandleEnsureAssetID(), userMW...))
|
|
|
|
r.Post(v1Base("/actions/zero-item-time-fields"), chain.ToHandlerFunc(v1Ctrl.HandleItemDateZeroOut(), userMW...))
|
|
|
|
r.Post(v1Base("/actions/ensure-import-refs"), chain.ToHandlerFunc(v1Ctrl.HandleEnsureImportRefs(), userMW...))
|
|
|
|
|
|
|
|
r.Get(v1Base("/locations"), chain.ToHandlerFunc(v1Ctrl.HandleLocationGetAll(), userMW...))
|
|
|
|
r.Post(v1Base("/locations"), chain.ToHandlerFunc(v1Ctrl.HandleLocationCreate(), userMW...))
|
|
|
|
r.Get(v1Base("/locations/tree"), chain.ToHandlerFunc(v1Ctrl.HandleLocationTreeQuery(), userMW...))
|
|
|
|
r.Get(v1Base("/locations/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleLocationGet(), userMW...))
|
|
|
|
r.Put(v1Base("/locations/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleLocationUpdate(), userMW...))
|
|
|
|
r.Delete(v1Base("/locations/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleLocationDelete(), userMW...))
|
|
|
|
|
|
|
|
r.Get(v1Base("/labels"), chain.ToHandlerFunc(v1Ctrl.HandleLabelsGetAll(), userMW...))
|
|
|
|
r.Post(v1Base("/labels"), chain.ToHandlerFunc(v1Ctrl.HandleLabelsCreate(), userMW...))
|
|
|
|
r.Get(v1Base("/labels/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleLabelGet(), userMW...))
|
|
|
|
r.Put(v1Base("/labels/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleLabelUpdate(), userMW...))
|
|
|
|
r.Delete(v1Base("/labels/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleLabelDelete(), userMW...))
|
|
|
|
|
|
|
|
r.Get(v1Base("/items"), chain.ToHandlerFunc(v1Ctrl.HandleItemsGetAll(), userMW...))
|
|
|
|
r.Post(v1Base("/items"), chain.ToHandlerFunc(v1Ctrl.HandleItemsCreate(), userMW...))
|
|
|
|
r.Post(v1Base("/items/import"), chain.ToHandlerFunc(v1Ctrl.HandleItemsImport(), userMW...))
|
|
|
|
r.Get(v1Base("/items/export"), chain.ToHandlerFunc(v1Ctrl.HandleItemsExport(), userMW...))
|
|
|
|
r.Get(v1Base("/items/fields"), chain.ToHandlerFunc(v1Ctrl.HandleGetAllCustomFieldNames(), userMW...))
|
|
|
|
r.Get(v1Base("/items/fields/values"), chain.ToHandlerFunc(v1Ctrl.HandleGetAllCustomFieldValues(), userMW...))
|
|
|
|
|
|
|
|
r.Get(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemGet(), userMW...))
|
|
|
|
r.Put(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemUpdate(), userMW...))
|
2023-06-02 21:56:40 +00:00
|
|
|
r.Patch(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemPatch(), userMW...))
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Delete(v1Base("/items/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemDelete(), userMW...))
|
|
|
|
|
|
|
|
r.Post(v1Base("/items/{id}/attachments"), chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentCreate(), userMW...))
|
|
|
|
r.Put(v1Base("/items/{id}/attachments/{attachment_id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentUpdate(), userMW...))
|
|
|
|
r.Delete(v1Base("/items/{id}/attachments/{attachment_id}"), chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentDelete(), userMW...))
|
|
|
|
|
|
|
|
r.Get(v1Base("/items/{id}/maintenance"), chain.ToHandlerFunc(v1Ctrl.HandleMaintenanceLogGet(), userMW...))
|
|
|
|
r.Post(v1Base("/items/{id}/maintenance"), chain.ToHandlerFunc(v1Ctrl.HandleMaintenanceEntryCreate(), userMW...))
|
|
|
|
r.Put(v1Base("/items/{id}/maintenance/{entry_id}"), chain.ToHandlerFunc(v1Ctrl.HandleMaintenanceEntryUpdate(), userMW...))
|
|
|
|
r.Delete(v1Base("/items/{id}/maintenance/{entry_id}"), chain.ToHandlerFunc(v1Ctrl.HandleMaintenanceEntryDelete(), userMW...))
|
|
|
|
|
2023-07-31 14:05:37 +00:00
|
|
|
r.Get(v1Base("/assets/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleAssetGet(), userMW...))
|
2023-01-14 18:24:11 +00:00
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// Notifiers
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get(v1Base("/notifiers"), chain.ToHandlerFunc(v1Ctrl.HandleGetUserNotifiers(), userMW...))
|
|
|
|
r.Post(v1Base("/notifiers"), chain.ToHandlerFunc(v1Ctrl.HandleCreateNotifier(), userMW...))
|
|
|
|
r.Put(v1Base("/notifiers/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleUpdateNotifier(), userMW...))
|
|
|
|
r.Delete(v1Base("/notifiers/{id}"), chain.ToHandlerFunc(v1Ctrl.HandleDeleteNotifier(), userMW...))
|
|
|
|
r.Post(v1Base("/notifiers/test"), chain.ToHandlerFunc(v1Ctrl.HandlerNotifierTest(), userMW...))
|
2023-03-07 06:18:58 +00:00
|
|
|
|
2023-01-19 05:44:06 +00:00
|
|
|
// Asset-Like endpoints
|
2023-03-21 04:32:10 +00:00
|
|
|
assetMW := []errchain.Middleware{
|
|
|
|
a.mwAuthToken,
|
|
|
|
a.mwRoles(RoleModeOr, authroles.RoleUser.String(), authroles.RoleAttachments.String()),
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Get(
|
2023-01-19 05:44:06 +00:00
|
|
|
v1Base("/qrcode"),
|
2023-03-21 04:32:10 +00:00
|
|
|
chain.ToHandlerFunc(v1Ctrl.HandleGenerateQRCode(), assetMW...),
|
2023-01-19 05:44:06 +00:00
|
|
|
)
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get(
|
2022-12-03 19:55:00 +00:00
|
|
|
v1Base("/items/{id}/attachments/{attachment_id}"),
|
2023-03-21 04:32:10 +00:00
|
|
|
chain.ToHandlerFunc(v1Ctrl.HandleItemAttachmentGet(), assetMW...),
|
2022-12-03 19:55:00 +00:00
|
|
|
)
|
2022-10-30 02:15:35 +00:00
|
|
|
|
2023-02-13 19:00:29 +00:00
|
|
|
// Reporting Services
|
2023-03-21 04:32:10 +00:00
|
|
|
r.Get(v1Base("/reporting/bill-of-materials"), chain.ToHandlerFunc(v1Ctrl.HandleBillOfMaterialsExport(), userMW...))
|
|
|
|
|
|
|
|
r.NotFound(chain.ToHandlerFunc(notFoundHandler()))
|
2023-02-13 19:00:29 +00:00
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
2022-09-04 02:42:03 +00:00
|
|
|
|
2022-09-04 03:10:42 +00:00
|
|
|
func registerMimes() {
|
2022-09-04 03:06:55 +00:00
|
|
|
err := mime.AddExtensionType(".js", "application/javascript")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mime.AddExtensionType(".mjs", "application/javascript")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-04 02:42:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 03:27:02 +00:00
|
|
|
// notFoundHandler perform the main logic around handling the internal SPA embed and ensuring that
|
|
|
|
// the client side routing is handled correctly.
|
2023-03-21 04:32:10 +00:00
|
|
|
func notFoundHandler() errchain.HandlerFunc {
|
2022-09-04 03:10:42 +00:00
|
|
|
tryRead := func(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()
|
2022-09-04 02:42:03 +00:00
|
|
|
|
2022-09-04 03:10:42 +00:00
|
|
|
stat, _ := f.Stat()
|
|
|
|
if stat.IsDir() {
|
|
|
|
return ErrDir
|
|
|
|
}
|
2022-09-04 02:42:03 +00:00
|
|
|
|
2022-09-04 03:10:42 +00:00
|
|
|
contentType := mime.TypeByExtension(filepath.Ext(requestedPath))
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
_, err = io.Copy(w, f)
|
|
|
|
return err
|
2022-09-04 02:42:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-25 19:24:19 +00:00
|
|
|
err := tryRead(public, "static/public", r.URL.Path, w)
|
2022-09-04 02:42:03 +00:00
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
// Fallback to the index.html file.
|
|
|
|
// should succeed in all cases.
|
|
|
|
err = tryRead(public, "static/public", "index.html", w)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-04 02:42:03 +00:00
|
|
|
}
|
2022-10-30 02:15:35 +00:00
|
|
|
return nil
|
2022-09-04 02:42:03 +00:00
|
|
|
}
|
|
|
|
}
|