Implement minor fixes #9

Merged
jacob1123 merged 4 commits from minor-fixes into master 2022-02-14 08:48:08 +01:00
5 changed files with 39 additions and 12 deletions

View File

@ -12,7 +12,7 @@ docker:
WORKDIR /app
COPY +build/budgeteer .
ENTRYPOINT ["/app/budgeteer"]
SAVE IMAGE budgeteer:latest
SAVE IMAGE hub.javil.eu/budgeteer:latest
run:
LOCALLY

View File

@ -2,9 +2,7 @@ version: '3.7'
services:
app:
image: budgeteer:dev
build:
context: ./docker/
image: hub.javil.eu/budgeteer:dev
container_name: budgeteer
stdin_open: true # docker run -i
tty: true # docker run -t

View File

@ -2,7 +2,7 @@ version: '3.7'
services:
app:
image: budgeteer:latest
image: hub.javil.eu/budgeteer:latest
container_name: budgeteer
ports:
- 1323:1323

View File

@ -1,8 +1,11 @@
package http
import (
"errors"
"io"
"io/fs"
"net/http"
"path"
"strings"
"git.javil.eu/jacob1123/budgeteer"
@ -18,6 +21,7 @@ type Handler struct {
Service *postgres.Database
TokenVerifier budgeteer.TokenVerifier
CredentialsVerifier *bcrypt.Verifier
StaticFS http.FileSystem
}
const (
@ -37,14 +41,10 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
if err != nil {
panic("couldn't open static files")
}
staticFS := http.FS(static)
h.StaticFS = http.FS(static)
router.Use(enableCachingForStaticFiles())
router.NoRoute(
func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, staticFS)
},
)
router.NoRoute(h.ServeStatic)
withLogin := router.Group("")
withLogin.Use(h.verifyLoginWithRedirect)
@ -81,6 +81,35 @@ func (h *Handler) LoadRoutes(router *gin.Engine) {
transaction.POST("/new", h.newTransaction)
transaction.POST("/:transactionid", h.newTransaction)
}
func (h *Handler) ServeStatic(c *gin.Context) {
h.ServeStaticFile(c, c.Request.URL.Path)
}
func (h *Handler) ServeStaticFile(c *gin.Context, fullPath string) {
file, err := h.StaticFS.Open(fullPath)
if errors.Is(err, fs.ErrNotExist) {
h.ServeStaticFile(c, path.Join("/", "/index.html"))
return
}
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
stat, err := file.Stat()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if stat.IsDir() {
h.ServeStaticFile(c, path.Join(fullPath, "index.html"))
return
}
http.ServeContent(c.Writer, c.Request, stat.Name(), stat.ModTime(), file.(io.ReadSeeker))
}
func enableCachingForStaticFiles() gin.HandlerFunc {
return func(c *gin.Context) {

View File

@ -21,7 +21,7 @@ export interface Budget {
export const useSessionStore = defineStore('session', {
state: () => ({
Session: useStorage<Session>('session', null, undefined, { serializer: StorageSerializers.object }),
Budgets: useStorage<Map<string, Budget>>('budgets', new Map<string, Budget>()),
Budgets: useStorage<Map<string, Budget>>('budgets', new Map<string, Budget>(), undefined, { serializer: StorageSerializers.map }),
}),
getters: {
BudgetsList: (state) => [ ...state.Budgets.values() ],