diff --git a/.env.example b/.env.example index 3c1bb3b..89caa70 100644 --- a/.env.example +++ b/.env.example @@ -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_SECURE= # default to "false" \ No newline at end of file +API_PORT= # defaul to 8585 +API_SECURE= # default to "false" + +API_STORE_DIR= # default to "./store" \ No newline at end of file diff --git a/.gitignore b/.gitignore index cd159b8..b463ea6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ builds +store .env \ No newline at end of file diff --git a/handlers/upload/handler.go b/handlers/upload/handler.go index f76b787..c2ad48a 100644 --- a/handlers/upload/handler.go +++ b/handlers/upload/handler.go @@ -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 diff --git a/services/environments.go b/services/environments.go index 4c3652f..20a5b8b 100644 --- a/services/environments.go +++ b/services/environments.go @@ -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 } diff --git a/templates/home.html.tpl b/templates/home.html.tpl index 40c3404..f6283ad 100644 --- a/templates/home.html.tpl +++ b/templates/home.html.tpl @@ -1,3 +1,4 @@ {{ define "content" }}
No extra JS, CSS or fancy stuff. You click, you search, you found, you're happy.
{{ end }} \ No newline at end of file diff --git a/templates/templates.go b/templates/templates.go index 57f1ce2..6469b6e 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -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 +}