2022-08-30 02:30:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-18 19:45:42 +00:00
|
|
|
"bytes"
|
2022-08-30 02:30:36 +00:00
|
|
|
"context"
|
2023-03-21 19:32:48 +00:00
|
|
|
"fmt"
|
2022-10-25 02:24:18 +00:00
|
|
|
"net/http"
|
2022-09-14 05:15:01 +00:00
|
|
|
"os"
|
2022-09-28 04:26:44 +00:00
|
|
|
"path/filepath"
|
2022-08-30 02:30:36 +00:00
|
|
|
"time"
|
|
|
|
|
2022-09-28 04:26:44 +00:00
|
|
|
atlas "ariga.io/atlas/sql/migrate"
|
|
|
|
"entgo.io/ent/dialect/sql/schema"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
|
2024-01-18 19:45:42 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/currencies"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
2023-08-02 21:00:57 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/migrations"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/config"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/web/mid"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
2024-03-02 00:07:03 +00:00
|
|
|
"github.com/hay-kot/httpkit/graceful"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-09-03 18:38:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/rs/zerolog/pkgerrors"
|
2023-03-23 04:49:49 +00:00
|
|
|
|
|
|
|
_ "github.com/hay-kot/homebox/backend/pkgs/cgofreesqlite"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
2022-09-14 04:06:07 +00:00
|
|
|
var (
|
2022-10-14 01:01:01 +00:00
|
|
|
version = "nightly"
|
|
|
|
commit = "HEAD"
|
|
|
|
buildTime = "now"
|
2022-09-14 04:06:07 +00:00
|
|
|
)
|
|
|
|
|
2023-11-24 19:02:02 +00:00
|
|
|
func build() string {
|
|
|
|
short := commit
|
|
|
|
if len(short) > 7 {
|
|
|
|
short = short[:7]
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s, commit %s, built at %s", version, short, buildTime)
|
|
|
|
}
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// @title Homebox API
|
2022-10-13 05:13:07 +00:00
|
|
|
// @version 1.0
|
2023-03-24 03:10:19 +00:00
|
|
|
// @description Track, Manage, and Organize your Things.
|
2022-10-13 05:13:07 +00:00
|
|
|
// @contact.name Don't
|
|
|
|
// @BasePath /api
|
|
|
|
// @securityDefinitions.apikey Bearer
|
|
|
|
// @in header
|
|
|
|
// @name Authorization
|
|
|
|
// @description "Type 'Bearer TOKEN' to correctly set the API Key"
|
2022-08-30 02:30:36 +00:00
|
|
|
func main() {
|
2023-03-21 04:32:10 +00:00
|
|
|
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
|
|
|
|
2023-11-24 19:02:02 +00:00
|
|
|
cfg, err := config.New(build(), "Homebox inventory management system")
|
2022-08-30 02:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := run(cfg); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cfg *config.Config) error {
|
2022-09-04 03:27:02 +00:00
|
|
|
app := new(cfg)
|
|
|
|
app.setupLogger()
|
2022-09-24 19:33:38 +00:00
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// =========================================================================
|
|
|
|
// Initialize Database & Repos
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
err := os.MkdirAll(cfg.Storage.Data, 0o755)
|
2022-09-24 19:33:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("failed to create data directory")
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:55:26 +00:00
|
|
|
c, err := ent.Open("sqlite3", cfg.Storage.SqliteURL)
|
2022-08-30 02:30:36 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
2022-09-24 19:33:38 +00:00
|
|
|
Str("driver", "sqlite").
|
2024-01-04 17:55:26 +00:00
|
|
|
Str("url", cfg.Storage.SqliteURL).
|
2022-09-03 18:38:35 +00:00
|
|
|
Msg("failed opening connection to sqlite")
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
defer func(c *ent.Client) {
|
2022-09-28 04:26:44 +00:00
|
|
|
err := c.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("failed to close database connection")
|
|
|
|
}
|
2022-08-30 02:30:36 +00:00
|
|
|
}(c)
|
2022-09-28 04:26:44 +00:00
|
|
|
|
|
|
|
temp := filepath.Join(os.TempDir(), "migrations")
|
|
|
|
|
|
|
|
err = migrations.Write(temp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := atlas.NewLocalDir(temp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := []schema.MigrateOption{
|
|
|
|
schema.WithDir(dir),
|
|
|
|
schema.WithDropColumn(true),
|
|
|
|
schema.WithDropIndex(true),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Schema.Create(context.Background(), options...)
|
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
2022-09-24 19:33:38 +00:00
|
|
|
Str("driver", "sqlite").
|
2024-01-04 17:55:26 +00:00
|
|
|
Str("url", cfg.Storage.SqliteURL).
|
2022-09-03 18:38:35 +00:00
|
|
|
Msg("failed creating schema resources")
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 04:26:44 +00:00
|
|
|
err = os.RemoveAll(temp)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("failed to remove temporary directory for database migrations")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-18 19:45:42 +00:00
|
|
|
collectFuncs := []currencies.CollectorFunc{
|
|
|
|
currencies.CollectDefaults(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Options.CurrencyConfig != "" {
|
|
|
|
log.Info().
|
|
|
|
Str("path", cfg.Options.CurrencyConfig).
|
|
|
|
Msg("loading currency config file")
|
|
|
|
|
|
|
|
content, err := os.ReadFile(cfg.Options.CurrencyConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
|
|
|
Str("path", cfg.Options.CurrencyConfig).
|
|
|
|
Msg("failed to read currency config file")
|
|
|
|
}
|
|
|
|
|
|
|
|
collectFuncs = append(collectFuncs, currencies.CollectJSON(bytes.NewReader(content)))
|
|
|
|
}
|
|
|
|
|
|
|
|
currencies, err := currencies.CollectionCurrencies(collectFuncs...)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
|
|
|
Msg("failed to collect currencies")
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
app.bus = eventbus.New()
|
2022-08-30 02:30:36 +00:00
|
|
|
app.db = c
|
2023-08-02 21:00:57 +00:00
|
|
|
app.repos = repo.New(c, app.bus, cfg.Storage.Data)
|
2022-11-13 23:17:55 +00:00
|
|
|
app.services = services.New(
|
|
|
|
app.repos,
|
|
|
|
services.WithAutoIncrementAssetID(cfg.Options.AutoIncrementAssetID),
|
2024-01-18 19:45:42 +00:00
|
|
|
services.WithCurrencies(currencies),
|
2022-11-13 23:17:55 +00:00
|
|
|
)
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
// =========================================================================
|
2023-03-21 04:32:10 +00:00
|
|
|
// Start Server
|
|
|
|
|
2022-10-30 02:15:35 +00:00
|
|
|
logger := log.With().Caller().Logger()
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
router := chi.NewMux()
|
|
|
|
router.Use(
|
|
|
|
middleware.RequestID,
|
|
|
|
middleware.RealIP,
|
|
|
|
mid.Logger(logger),
|
|
|
|
middleware.Recoverer,
|
|
|
|
middleware.StripSlashes,
|
|
|
|
)
|
|
|
|
|
2024-03-02 00:07:03 +00:00
|
|
|
chain := errchain.New(mid.Errors(logger))
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
app.mountRoutes(router, chain, app.repos)
|
2022-10-30 02:15:35 +00:00
|
|
|
|
2024-03-02 00:07:03 +00:00
|
|
|
runner := graceful.NewRunner()
|
|
|
|
|
|
|
|
runner.AddFunc("server", func(ctx context.Context) error {
|
|
|
|
httpserver := http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%s", cfg.Web.Host, cfg.Web.Port),
|
|
|
|
Handler: router,
|
|
|
|
ReadTimeout: cfg.Web.ReadTimeout,
|
|
|
|
WriteTimeout: cfg.Web.WriteTimeout,
|
|
|
|
IdleTimeout: cfg.Web.IdleTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
_ = httpserver.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Info().Msgf("Server is running on %s:%s", cfg.Web.Host, cfg.Web.Port)
|
|
|
|
return httpserver.ListenAndServe()
|
|
|
|
})
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
// =========================================================================
|
|
|
|
// Start Reoccurring Tasks
|
|
|
|
|
2024-03-02 00:07:03 +00:00
|
|
|
runner.AddFunc("eventbus", app.bus.Run)
|
2023-08-02 21:00:57 +00:00
|
|
|
|
2024-03-05 15:36:50 +00:00
|
|
|
runner.AddFunc("seed_database", func(ctx context.Context) error {
|
|
|
|
// TODO: Remove through external API that does setup
|
|
|
|
if cfg.Demo {
|
|
|
|
log.Info().Msg("Running in demo mode, creating demo data")
|
|
|
|
app.SetupDemo()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2024-03-02 00:07:03 +00:00
|
|
|
runner.AddPlugin(NewTask("purge-tokens", time.Duration(24)*time.Hour, func(ctx context.Context) {
|
|
|
|
_, err := app.repos.AuthTokens.PurgeExpiredTokens(ctx)
|
2022-09-03 09:24:28 +00:00
|
|
|
if err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("failed to purge expired tokens")
|
2022-09-03 09:24:28 +00:00
|
|
|
}
|
2024-03-02 00:07:03 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
runner.AddPlugin(NewTask("purge-invitations", time.Duration(24)*time.Hour, func(ctx context.Context) {
|
|
|
|
_, err := app.repos.Groups.InvitationPurge(ctx)
|
2022-10-15 20:15:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("failed to purge expired invitations")
|
|
|
|
}
|
2024-03-02 00:07:03 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
runner.AddPlugin(NewTask("send-notifications", time.Duration(1)*time.Hour, func(ctx context.Context) {
|
2023-03-21 19:32:48 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
if now.Hour() == 8 {
|
|
|
|
fmt.Println("run notifiers")
|
|
|
|
err := app.services.BackgroundService.SendNotifiersToday(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("failed to send notifiers")
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 00:07:03 +00:00
|
|
|
}))
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-10-25 02:24:18 +00:00
|
|
|
if cfg.Debug.Enabled {
|
2024-03-02 00:07:03 +00:00
|
|
|
runner.AddFunc("debug", func(ctx context.Context) error {
|
|
|
|
debugserver := http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%s", cfg.Web.Host, cfg.Debug.Port),
|
|
|
|
Handler: app.debugRouter(),
|
|
|
|
ReadTimeout: cfg.Web.ReadTimeout,
|
|
|
|
WriteTimeout: cfg.Web.WriteTimeout,
|
|
|
|
IdleTimeout: cfg.Web.IdleTimeout,
|
2022-10-25 02:24:18 +00:00
|
|
|
}
|
2024-03-02 00:07:03 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
_ = debugserver.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Info().Msgf("Debug server is running on %s:%s", cfg.Web.Host, cfg.Debug.Port)
|
|
|
|
return debugserver.ListenAndServe()
|
|
|
|
})
|
2022-10-25 02:24:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-02 00:07:03 +00:00
|
|
|
return runner.Start(context.Background())
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|