fix: move implementation to "internal" package
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
This commit is contained in:
@@ -3,11 +3,11 @@ dir: mocks/
|
|||||||
outpkg: mocks
|
outpkg: mocks
|
||||||
issue-845-fix: True
|
issue-845-fix: True
|
||||||
packages:
|
packages:
|
||||||
spend-sparrow/service:
|
spend-sparrow/internal/service:
|
||||||
interfaces:
|
interfaces:
|
||||||
Random:
|
Random:
|
||||||
Clock:
|
Clock:
|
||||||
Mail:
|
Mail:
|
||||||
spend-sparrow/db:
|
spend-sparrow/internal/db:
|
||||||
interfaces:
|
interfaces:
|
||||||
Auth:
|
Auth:
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -3,8 +3,8 @@ package db
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
135
internal/default.go
Normal file
135
internal/default.go
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"spend-sparrow/internal/db"
|
||||||
|
"spend-sparrow/internal/handler"
|
||||||
|
"spend-sparrow/internal/handler/middleware"
|
||||||
|
"spend-sparrow/internal/log"
|
||||||
|
"spend-sparrow/internal/service"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
|
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run(ctx context.Context, database *sqlx.DB, migrationsPrefix string, env func(string) string) error {
|
||||||
|
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
log.Info("Starting server...")
|
||||||
|
|
||||||
|
// init server settings
|
||||||
|
serverSettings := types.NewSettingsFromEnv(env)
|
||||||
|
|
||||||
|
// init db
|
||||||
|
err := db.RunMigrations(database, migrationsPrefix)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not run migrations: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// init servers
|
||||||
|
var prometheusServer *http.Server
|
||||||
|
if serverSettings.PrometheusEnabled {
|
||||||
|
prometheusServer := &http.Server{
|
||||||
|
Addr: ":8081",
|
||||||
|
Handler: promhttp.Handler(),
|
||||||
|
ReadHeaderTimeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
go startServer(prometheusServer)
|
||||||
|
}
|
||||||
|
|
||||||
|
httpServer := &http.Server{
|
||||||
|
Addr: ":" + serverSettings.Port,
|
||||||
|
Handler: createHandler(database, serverSettings),
|
||||||
|
ReadHeaderTimeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
go startServer(httpServer)
|
||||||
|
|
||||||
|
// graceful shutdown
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
go shutdownServer(httpServer, ctx, &wg)
|
||||||
|
go shutdownServer(prometheusServer, ctx, &wg)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func startServer(s *http.Server) {
|
||||||
|
log.Info("Starting server on %q", s.Addr)
|
||||||
|
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
log.Error("error listening and serving: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
|
||||||
|
defer wg.Done()
|
||||||
|
if s == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
shutdownCtx := context.Background()
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := s.Shutdown(shutdownCtx); err != nil {
|
||||||
|
log.Error("error shutting down http server: %v", err)
|
||||||
|
} else {
|
||||||
|
log.Info("Gracefully stopped http server on %v", s.Addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||||
|
var router = http.NewServeMux()
|
||||||
|
|
||||||
|
authDb := db.NewAuthSqlite(d)
|
||||||
|
|
||||||
|
randomService := service.NewRandom()
|
||||||
|
clockService := service.NewClock()
|
||||||
|
mailService := service.NewMail(serverSettings)
|
||||||
|
|
||||||
|
authService := service.NewAuth(authDb, randomService, clockService, mailService, serverSettings)
|
||||||
|
accountService := service.NewAccount(d, randomService, clockService)
|
||||||
|
treasureChestService := service.NewTreasureChest(d, randomService, clockService)
|
||||||
|
transactionService := service.NewTransaction(d, randomService, clockService)
|
||||||
|
transactionRecurringService := service.NewTransactionRecurring(d, randomService, clockService, transactionService)
|
||||||
|
|
||||||
|
render := handler.NewRender()
|
||||||
|
indexHandler := handler.NewIndex(render)
|
||||||
|
authHandler := handler.NewAuth(authService, render)
|
||||||
|
accountHandler := handler.NewAccount(accountService, render)
|
||||||
|
treasureChestHandler := handler.NewTreasureChest(treasureChestService, transactionRecurringService, render)
|
||||||
|
transactionHandler := handler.NewTransaction(transactionService, accountService, treasureChestService, render)
|
||||||
|
transactionRecurringHandler := handler.NewTransactionRecurring(transactionRecurringService, render)
|
||||||
|
|
||||||
|
indexHandler.Handle(router)
|
||||||
|
accountHandler.Handle(router)
|
||||||
|
treasureChestHandler.Handle(router)
|
||||||
|
authHandler.Handle(router)
|
||||||
|
transactionHandler.Handle(router)
|
||||||
|
transactionRecurringHandler.Handle(router)
|
||||||
|
|
||||||
|
// Serve static files (CSS, JS and images)
|
||||||
|
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
||||||
|
|
||||||
|
return middleware.Wrapper(
|
||||||
|
router,
|
||||||
|
middleware.GenerateRecurringTransactions(transactionRecurringService),
|
||||||
|
middleware.SecurityHeaders(serverSettings),
|
||||||
|
middleware.CacheControl,
|
||||||
|
middleware.CrossSiteRequestForgery(authService),
|
||||||
|
middleware.Authenticate(authService),
|
||||||
|
middleware.Gzip,
|
||||||
|
middleware.Log,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -2,11 +2,11 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/internal/handler/middleware"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
t "spend-sparrow/template/account"
|
t "spend-sparrow/internal/template/account"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
)
|
)
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/internal/handler/middleware"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
"spend-sparrow/template/auth"
|
"spend-sparrow/internal/template/auth"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -3,9 +3,9 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/db"
|
"spend-sparrow/internal/db"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContextKey string
|
type ContextKey string
|
||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type csrfResponseWriter struct {
|
type csrfResponseWriter struct {
|
||||||
@@ -2,7 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateRecurringTransactions(transactionRecurring service.TransactionRecurring) func(http.Handler) http.Handler {
|
func GenerateRecurringTransactions(transactionRecurring service.TransactionRecurring) func(http.Handler) http.Handler {
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type gzipResponseWriter struct {
|
type gzipResponseWriter struct {
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
@@ -3,7 +3,7 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SecurityHeaders(serverSettings *types.Settings) func(http.Handler) http.Handler {
|
func SecurityHeaders(serverSettings *types.Settings) func(http.Handler) http.Handler {
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/template"
|
"spend-sparrow/internal/template"
|
||||||
"spend-sparrow/template/auth"
|
"spend-sparrow/internal/template/auth"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -2,8 +2,8 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/internal/handler/middleware"
|
||||||
"spend-sparrow/template"
|
"spend-sparrow/internal/template"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
)
|
)
|
||||||
@@ -3,11 +3,11 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/internal/handler/middleware"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
t "spend-sparrow/template/transaction"
|
t "spend-sparrow/internal/template/transaction"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -2,11 +2,11 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/internal/handler/middleware"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
t "spend-sparrow/template/transaction_recurring"
|
t "spend-sparrow/internal/template/transaction_recurring"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TransactionRecurring interface {
|
type TransactionRecurring interface {
|
||||||
@@ -2,12 +2,12 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/internal/handler/middleware"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal/service"
|
||||||
tr "spend-sparrow/template/transaction_recurring"
|
tr "spend-sparrow/internal/template/transaction_recurring"
|
||||||
t "spend-sparrow/template/treasurechest"
|
t "spend-sparrow/internal/template/treasurechest"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/utils"
|
"spend-sparrow/internal/utils"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -3,10 +3,9 @@ package service
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"spend-sparrow/internal/db"
|
||||||
"spend-sparrow/db"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@@ -5,14 +5,13 @@ import (
|
|||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"errors"
|
"errors"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
|
"spend-sparrow/internal/db"
|
||||||
|
"spend-sparrow/internal/log"
|
||||||
|
mailTemplate "spend-sparrow/internal/template/mail"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"spend-sparrow/db"
|
|
||||||
"spend-sparrow/log"
|
|
||||||
mailTemplate "spend-sparrow/template/mail"
|
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"golang.org/x/crypto/argon2"
|
"golang.org/x/crypto/argon2"
|
||||||
)
|
)
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/log"
|
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
|
"spend-sparrow/internal/log"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mail interface {
|
type Mail interface {
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/log"
|
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"spend-sparrow/internal/log"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
@@ -3,9 +3,9 @@ package service
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"spend-sparrow/db"
|
"spend-sparrow/internal/db"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -3,13 +3,12 @@ package service
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"spend-sparrow/internal/db"
|
||||||
|
"spend-sparrow/internal/log"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"spend-sparrow/db"
|
|
||||||
"spend-sparrow/log"
|
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@@ -4,10 +4,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
|
"spend-sparrow/internal/db"
|
||||||
"spend-sparrow/db"
|
"spend-sparrow/internal/log"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
10
internal/service/treasure_chest_test.go
Normal file
10
internal/service/treasure_chest_test.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package service_test
|
||||||
|
|
||||||
|
// import (
|
||||||
|
// "spned-sparrow"
|
||||||
|
// )
|
||||||
|
//
|
||||||
|
// func TestTreasureChestProhibitDeleteIfTransactionRecurringExists(t *testing.T) {
|
||||||
|
// service := main.Setup
|
||||||
|
//
|
||||||
|
// }
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package account
|
package account
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "spend-sparrow/template/svg"
|
import "spend-sparrow/internal/template/svg"
|
||||||
import "spend-sparrow/types"
|
import "spend-sparrow/internal/types"
|
||||||
|
|
||||||
templ Account(accounts []*types.Account) {
|
templ Account(accounts []*types.Account) {
|
||||||
<div class="max-w-6xl mt-10 mx-auto">
|
<div class="max-w-6xl mt-10 mx-auto">
|
||||||
@@ -2,8 +2,8 @@ package transaction
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "time"
|
import "time"
|
||||||
import "spend-sparrow/template/svg"
|
import "spend-sparrow/internal/template/svg"
|
||||||
import "spend-sparrow/types"
|
import "spend-sparrow/internal/types"
|
||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
templ Transaction(items templ.Component, filter types.TransactionItemsFilter, accounts []*types.Account, treasureChests []*types.TreasureChest) {
|
templ Transaction(items templ.Component, filter types.TransactionItemsFilter, accounts []*types.Account, treasureChests []*types.TreasureChest) {
|
||||||
@@ -2,8 +2,8 @@ package transaction_recurring
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "time"
|
import "time"
|
||||||
import "spend-sparrow/template/svg"
|
import "spend-sparrow/internal/template/svg"
|
||||||
import "spend-sparrow/types"
|
import "spend-sparrow/internal/types"
|
||||||
|
|
||||||
templ TransactionRecurringItems(transactionsRecurring []*types.TransactionRecurring, editId, accountId, treasureChestId string) {
|
templ TransactionRecurringItems(transactionsRecurring []*types.TransactionRecurring, editId, accountId, treasureChestId string) {
|
||||||
<!-- Don't use table, because embedded forms are only valid for cells -->
|
<!-- Don't use table, because embedded forms are only valid for cells -->
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package treasurechest
|
package treasurechest
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "spend-sparrow/template/svg"
|
import "spend-sparrow/internal/template/svg"
|
||||||
import "spend-sparrow/types"
|
import "spend-sparrow/internal/types"
|
||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
templ TreasureChest(treasureChests []*types.TreasureChest, monthlySums map[uuid.UUID]int64) {
|
templ TreasureChest(treasureChests []*types.TreasureChest, monthlySums map[uuid.UUID]int64) {
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TriggerToast(w http.ResponseWriter, r *http.Request, class string, message string) {
|
func TriggerToast(w http.ResponseWriter, r *http.Request, class string, message string) {
|
||||||
131
main.go
131
main.go
@@ -1,27 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"spend-sparrow/db"
|
|
||||||
"spend-sparrow/handler"
|
|
||||||
"spend-sparrow/handler/middleware"
|
|
||||||
"spend-sparrow/log"
|
|
||||||
"spend-sparrow/service"
|
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"spend-sparrow/internal"
|
||||||
"sync"
|
"spend-sparrow/internal/log"
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -39,121 +26,9 @@ func main() {
|
|||||||
log.Fatal("Could not close Database data.db: %v", err)
|
log.Fatal("Could not close Database data.db: %v", err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = run(context.Background(), db, os.Getenv)
|
err = internal.Run(context.Background(), db, "", os.Getenv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error running server: %v", err)
|
log.Error("Error running server: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(ctx context.Context, database *sqlx.DB, env func(string) string) error {
|
|
||||||
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
log.Info("Starting server...")
|
|
||||||
|
|
||||||
// init server settings
|
|
||||||
serverSettings := types.NewSettingsFromEnv(env)
|
|
||||||
|
|
||||||
// init db
|
|
||||||
err := db.RunMigrations(database, "")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not run migrations: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// init servers
|
|
||||||
var prometheusServer *http.Server
|
|
||||||
if serverSettings.PrometheusEnabled {
|
|
||||||
prometheusServer := &http.Server{
|
|
||||||
Addr: ":8081",
|
|
||||||
Handler: promhttp.Handler(),
|
|
||||||
ReadHeaderTimeout: 10 * time.Second,
|
|
||||||
}
|
|
||||||
go startServer(prometheusServer)
|
|
||||||
}
|
|
||||||
|
|
||||||
httpServer := &http.Server{
|
|
||||||
Addr: ":" + serverSettings.Port,
|
|
||||||
Handler: createHandler(database, serverSettings),
|
|
||||||
ReadHeaderTimeout: 10 * time.Second,
|
|
||||||
}
|
|
||||||
go startServer(httpServer)
|
|
||||||
|
|
||||||
// graceful shutdown
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(2)
|
|
||||||
go shutdownServer(httpServer, ctx, &wg)
|
|
||||||
go shutdownServer(prometheusServer, ctx, &wg)
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func startServer(s *http.Server) {
|
|
||||||
log.Info("Starting server on %q", s.Addr)
|
|
||||||
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
||||||
log.Error("error listening and serving: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
|
|
||||||
defer wg.Done()
|
|
||||||
if s == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
<-ctx.Done()
|
|
||||||
shutdownCtx := context.Background()
|
|
||||||
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
if err := s.Shutdown(shutdownCtx); err != nil {
|
|
||||||
log.Error("error shutting down http server: %v", err)
|
|
||||||
} else {
|
|
||||||
log.Info("Gracefully stopped http server on %v", s.Addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
|
||||||
var router = http.NewServeMux()
|
|
||||||
|
|
||||||
authDb := db.NewAuthSqlite(d)
|
|
||||||
|
|
||||||
randomService := service.NewRandom()
|
|
||||||
clockService := service.NewClock()
|
|
||||||
mailService := service.NewMail(serverSettings)
|
|
||||||
|
|
||||||
authService := service.NewAuth(authDb, randomService, clockService, mailService, serverSettings)
|
|
||||||
accountService := service.NewAccount(d, randomService, clockService)
|
|
||||||
treasureChestService := service.NewTreasureChest(d, randomService, clockService)
|
|
||||||
transactionService := service.NewTransaction(d, randomService, clockService)
|
|
||||||
transactionRecurringService := service.NewTransactionRecurring(d, randomService, clockService, transactionService)
|
|
||||||
|
|
||||||
render := handler.NewRender()
|
|
||||||
indexHandler := handler.NewIndex(render)
|
|
||||||
authHandler := handler.NewAuth(authService, render)
|
|
||||||
accountHandler := handler.NewAccount(accountService, render)
|
|
||||||
treasureChestHandler := handler.NewTreasureChest(treasureChestService, transactionRecurringService, render)
|
|
||||||
transactionHandler := handler.NewTransaction(transactionService, accountService, treasureChestService, render)
|
|
||||||
transactionRecurringHandler := handler.NewTransactionRecurring(transactionRecurringService, render)
|
|
||||||
|
|
||||||
indexHandler.Handle(router)
|
|
||||||
accountHandler.Handle(router)
|
|
||||||
treasureChestHandler.Handle(router)
|
|
||||||
authHandler.Handle(router)
|
|
||||||
transactionHandler.Handle(router)
|
|
||||||
transactionRecurringHandler.Handle(router)
|
|
||||||
|
|
||||||
// Serve static files (CSS, JS and images)
|
|
||||||
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
|
||||||
|
|
||||||
return middleware.Wrapper(
|
|
||||||
router,
|
|
||||||
middleware.GenerateRecurringTransactions(transactionRecurringService),
|
|
||||||
middleware.SecurityHeaders(serverSettings),
|
|
||||||
middleware.CacheControl,
|
|
||||||
middleware.CrossSiteRequestForgery(authService),
|
|
||||||
middleware.Authenticate(authService),
|
|
||||||
middleware.Gzip,
|
|
||||||
middleware.Log,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package service_test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/db"
|
"spend-sparrow/internal/db"
|
||||||
|
"spend-sparrow/internal/service"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
"spend-sparrow/mocks"
|
"spend-sparrow/mocks"
|
||||||
"spend-sparrow/service"
|
|
||||||
"spend-sparrow/types"
|
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package db_test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"spend-sparrow/db"
|
"spend-sparrow/internal/db"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/types"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -12,8 +12,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/internal"
|
||||||
"spend-sparrow/types"
|
"spend-sparrow/internal/service"
|
||||||
|
"spend-sparrow/internal/types"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
@@ -1979,7 +1980,7 @@ func setupIntegrationTest(t *testing.T) (*sqlx.DB, string, context.Context) {
|
|||||||
testPort += 1024
|
testPort += 1024
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_ = run(ctx, db, getEnv(testPort))
|
_ = internal.Run(ctx, db, "../", getEnv(testPort))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
basePath := "http://localhost:" + strconv.Itoa(int(testPort))
|
basePath := "http://localhost:" + strconv.Itoa(int(testPort))
|
||||||
1
test/treasure_chest_test.go
Normal file
1
test/treasure_chest_test.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package test
|
||||||
Reference in New Issue
Block a user