add store directory creation

This commit is contained in:
rmanach 2025-01-03 14:48:49 +01:00
parent 18661f82f8
commit e0d7eeadc3
6 changed files with 78 additions and 39 deletions

View File

@ -1,7 +1,9 @@
API_ADMIN_USERNAME=
API_ADMIN_PASSWORD=
API_SESSION_EXPIRATION_DURATION= # in seconds
API_SESSION_EXPIRATION_DURATION= # in seconds (default to 30s)
API_PORT=
API_PORT= # defaul to 8585
API_SECURE= # default to "false"
API_STORE_DIR= # default to "./store"

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
builds
store
.env

View File

@ -26,7 +26,6 @@ var (
ErrInvalidAuthors = errors.New("must at least contains one author")
ErrFileMaxSizeReached = errors.New("max file size reached, must be <= 200MB")
ErrFileOpen = errors.New("unable to open file from form")
ErrUnauthorized = errors.New("unvalid authorization key")
)
type StrList = []string

View File

@ -9,37 +9,45 @@ import (
"github.com/rs/zerolog/log"
)
const defaultAPISessionExpirationDuration = 30 * time.Second
const defaultPort = 8585
const (
defaultAPISessionExpirationDuration = 30 * time.Second
defaultPort = 8585
defaultMainDir = "./store"
)
var GetEnv = sync.OnceValue[env](newEnv)
var env = sync.OnceValue[environment](newEnv)()
type env struct {
type environment struct {
adminUsername string
adminPassword string
sessionExpirationDuration time.Duration
port int
isSecure bool
storeDir string
}
func (e env) GetCredentials() (username, password string) {
func (e environment) GetCredentials() (username, password string) {
return e.adminUsername, e.adminPassword
}
func (e env) GetSessionExpirationDuration() time.Duration {
func (e environment) GetSessionExpirationDuration() time.Duration {
return e.sessionExpirationDuration
}
func (e env) GetPort() int {
func (e environment) GetPort() int {
return e.port
}
func (e env) IsSecure() bool {
func (e environment) IsSecure() bool {
return e.isSecure
}
func newEnv() env {
env := env{
func (e environment) GetDir() string {
return e.storeDir
}
func newEnv() environment {
env := environment{
adminUsername: os.Getenv("API_ADMIN_USERNAME"),
adminPassword: os.Getenv("API_ADMIN_PASSWORD"),
isSecure: os.Getenv("API_SECURE") == "true",
@ -59,5 +67,20 @@ func newEnv() env {
env.port = defaultPort
}
storeDir := os.Getenv("API_STORE_DIR")
if storeDir == "" {
storeDir = defaultMainDir
}
if err := os.MkdirAll(storeDir, 0755); err != nil { //nolint
log.Fatal().Err(err).Msg("unable to create store dir")
}
env.storeDir = storeDir
return env
}
func GetEnv() environment {
return env
}

View File

@ -1,3 +1,4 @@
{{ define "content" }}
<h3>A simple API to store, search and download books.</h3>
<p>No extra JS, CSS or fancy stuff. You click, you search, you found, you're happy.</p>
{{ end }}

View File

@ -4,7 +4,6 @@ import (
_ "embed"
"html/template"
"mime/multipart"
"os"
"strconv"
"strings"
"sync"
@ -55,56 +54,70 @@ var funcMap = template.FuncMap{
},
}
var GetHome = sync.OnceValue[*template.Template](func() *template.Template {
var homeTmpl = sync.OnceValue[*template.Template](func() *template.Template {
baseTmpl, err := template.New("base").Parse(base)
if err != nil {
log.Err(err).Msg("unable to parse base tmpl")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse base tmpl")
}
if _, err := baseTmpl.New("home").Parse(home); err != nil {
log.Err(err).Msg("unable to parse home tmpl")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse home tmpl")
}
return baseTmpl
})
})()
var GetUploadForm = sync.OnceValue[*template.Template](func() *template.Template {
var uploadFormTmpl = sync.OnceValue[*template.Template](func() *template.Template {
baseTmpl, err := template.New("base").Parse(base)
if err != nil {
log.Err(err).Msg("unable to parse base tmpl")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse base tmpl")
}
if _, err := baseTmpl.New("uploadForm").Funcs(funcMap).Parse(uploadForm); err != nil {
log.Err(err).Msg("unable to parse upload tmpl")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse upload tmpl")
}
return baseTmpl
})
})()
var GetLoginForm = sync.OnceValue[*template.Template](func() *template.Template {
var loginFormTmpl = sync.OnceValue[*template.Template](func() *template.Template {
baseTmpl, err := template.New("base").Parse(base)
if err != nil {
log.Err(err).Msg("unable to parse base tmpl")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse base tmpl")
}
if _, err := baseTmpl.New("loginForm").Funcs(funcMap).Parse(loginForm); err != nil {
log.Err(err).Msg("unable to parse login tmpl")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse login tmpl")
}
return baseTmpl
})
})()
var GetLoginSuccess = sync.OnceValue[*template.Template](func() *template.Template {
tmpl, err := template.New("loginSuccess").Parse(loginSuccess)
var loginSuccessTmpl = sync.OnceValue[*template.Template](func() *template.Template {
baseTmpl, err := template.New("base").Parse(base)
if err != nil {
log.Err(err).Msg("unable to parse login success")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to parse base tmpl")
}
return tmpl
})
if _, err := baseTmpl.New("loginSuccess").Funcs(funcMap).Parse(loginSuccess); err != nil {
log.Fatal().Err(err).Msg("unable to parse login success tmpl")
}
return baseTmpl
})()
func GetHome() *template.Template {
return homeTmpl
}
func GetLoginForm() *template.Template {
return loginFormTmpl
}
func GetLoginSuccess() *template.Template {
return loginSuccessTmpl
}
func GetUploadForm() *template.Template {
return uploadFormTmpl
}