diff --git a/handlers/home/handler.go b/handlers/home/handler.go new file mode 100644 index 0000000..5698ad9 --- /dev/null +++ b/handlers/home/handler.go @@ -0,0 +1,37 @@ +package home + +import ( + "bytes" + "fmt" + "librapi/templates" + "net/http" + + "github.com/rs/zerolog/log" +) + +func Handler() func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + getHome(w, r) + default: + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + } + } +} + +func getHome(w http.ResponseWriter, _ *http.Request) { + home := templates.GetHome() + + buf := bytes.NewBufferString("") + if err := home.Execute(buf, nil); err != nil { + log.Err(err).Msg("unable to generate template") + http.Error(w, "unexpected error occurred", http.StatusInternalServerError) + return + } + + if _, err := fmt.Fprint(w, buf); err != nil { + log.Err(err).Msg("unable to write to response") + http.Error(w, "unexpected error occurred", http.StatusInternalServerError) + } +} diff --git a/handlers/login/handler.go b/handlers/login/handler.go index afc2b68..e9a4a07 100644 --- a/handlers/login/handler.go +++ b/handlers/login/handler.go @@ -4,13 +4,14 @@ import ( "bytes" "errors" "fmt" - "librapi/handlers/login/templates" - "librapi/services" "net/http" "os" "sync" "github.com/rs/zerolog/log" + + "librapi/services" + "librapi/templates" ) var ( @@ -140,16 +141,18 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) http.SetCookie(w, cookie) loginSuccess := templates.GetLoginSuccess() - fmt.Fprint(w, loginSuccess.Tree.Root.String()) + buf := bytes.NewBufferString("") + if err := loginSuccess.Execute(buf, nil); err != nil { + log.Err(err).Msg("unable to generate template") + http.Error(w, "unexpected error occurred", http.StatusInternalServerError) + return + } + + fmt.Fprint(w, buf) } func getLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) { loginForm := templates.GetLoginForm() - if loginForm == nil { - log.Error().Msg("unable to load login form") - http.Error(w, "unexpected error occurred", http.StatusInternalServerError) - return - } if s.IsLogged(r) { loginSuccess := templates.GetLoginSuccess() diff --git a/handlers/login/templates/form.html.tpl b/handlers/login/templates/form.html.tpl deleted file mode 100644 index 295bbd3..0000000 --- a/handlers/login/templates/form.html.tpl +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - -

Login

-
-
-
-
- - -
- {{ if .Username.Err }} -
{{.Username.Err}}
- {{ end }} -
-
-
- - -
- {{ if .Password.Err }} -
{{.Password.Err}}
- {{ end }} -
-
-
- -
-
-
-
- {{ if ne (errStr .Error) "" }} -
{{.Error | errStr}}
- {{ end }} - - - \ No newline at end of file diff --git a/handlers/login/templates/success.html.tpl b/handlers/login/templates/success.html.tpl deleted file mode 100644 index e5cdc68..0000000 --- a/handlers/login/templates/success.html.tpl +++ /dev/null @@ -1,13 +0,0 @@ - - - - -

Login

-
You're logged
-

Available urls

- - - - \ No newline at end of file diff --git a/handlers/login/templates/templates.go b/handlers/login/templates/templates.go deleted file mode 100644 index 1cc06b9..0000000 --- a/handlers/login/templates/templates.go +++ /dev/null @@ -1,43 +0,0 @@ -package templates - -import ( - _ "embed" - "html/template" - "os" - "sync" - - "github.com/rs/zerolog/log" -) - -//go:embed form.html.tpl -var form string - -//go:embed success.html.tpl -var success string - -var funcMap = template.FuncMap{ - "errStr": func(err error) string { - if err != nil { - return err.Error() - } - return "" - }, -} - -var GetLoginForm = sync.OnceValue[*template.Template](func() *template.Template { - tmpl, err := template.New("loginForm").Funcs(funcMap).Parse(form) - if err != nil { - log.Err(err).Msg("unable to parse login form") - os.Exit(1) - } - return tmpl -}) - -var GetLoginSuccess = sync.OnceValue[*template.Template](func() *template.Template { - tmpl, err := template.New("loginSuccess").Parse(success) - if err != nil { - log.Err(err).Msg("unable to parse login success") - os.Exit(1) - } - return tmpl -}) diff --git a/handlers/upload/handler.go b/handlers/upload/handler.go index c1232b8..bcea623 100644 --- a/handlers/upload/handler.go +++ b/handlers/upload/handler.go @@ -13,8 +13,8 @@ import ( "github.com/rs/zerolog/log" - "librapi/handlers/upload/templates" "librapi/services" + "librapi/templates" ) const MaxFileSize = 200 // in MB diff --git a/handlers/upload/templates/templates.go b/handlers/upload/templates/templates.go deleted file mode 100644 index 1490773..0000000 --- a/handlers/upload/templates/templates.go +++ /dev/null @@ -1,47 +0,0 @@ -package templates - -import ( - _ "embed" - "html/template" - "mime/multipart" - "os" - "strconv" - "strings" - "sync" - - "github.com/rs/zerolog/log" -) - -var funcMap = template.FuncMap{ - "year": func(s int) string { - if s == 0 { - return "" - } - return strconv.Itoa(s) - }, - "join": func(s []string) string { - if len(s) == 0 { - return "" - } else { - return strings.Join(s, ",") - } - }, - "filename": func(h *multipart.FileHeader) string { - if h != nil { - return h.Filename - } - return "" - }, -} - -//go:embed form.html.tpl -var form string - -var GetUploadForm = sync.OnceValue[*template.Template](func() *template.Template { - tmpl, err := template.New("uploadForm").Funcs(funcMap).Parse(form) - if err != nil { - log.Err(err).Msg("unable to parse upload form") - os.Exit(1) - } - return tmpl -}) diff --git a/main.go b/main.go index 494b686..da2f851 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" + "librapi/handlers/home" "librapi/handlers/login" "librapi/handlers/upload" ) @@ -30,6 +31,7 @@ func main() { srv := server.NewServer( ctx, services.GetEnv().GetPort(), + server.NewHandler("/", home.Handler()), server.NewHandler("/upload", upload.Handler(sessionStore)), server.NewHandler("/login", login.Handler(sessionStore)), ) diff --git a/templates/base.html.tpl b/templates/base.html.tpl new file mode 100644 index 0000000..a5dd393 --- /dev/null +++ b/templates/base.html.tpl @@ -0,0 +1,60 @@ + + + + + + Librapi + + + + +
+

Librapi

+
+ + + +
+ {{ block "content" . }}{{ end }} +
+ + + + + + + \ No newline at end of file diff --git a/templates/home.html.tpl b/templates/home.html.tpl new file mode 100644 index 0000000..40c3404 --- /dev/null +++ b/templates/home.html.tpl @@ -0,0 +1,3 @@ +{{ define "content" }} +

A simple API to store, search and download books.

+{{ end }} \ No newline at end of file diff --git a/templates/login/login_form.html.tpl b/templates/login/login_form.html.tpl new file mode 100644 index 0000000..055ea47 --- /dev/null +++ b/templates/login/login_form.html.tpl @@ -0,0 +1,33 @@ +{{ define "content" }} +

Login

+
+
+
+
+ + +
+ {{ if .Username.Err }} +
{{.Username.Err}}
+ {{ end }} +
+
+
+ + +
+ {{ if .Password.Err }} +
{{.Password.Err}}
+ {{ end }} +
+
+
+ +
+
+
+
+{{ if ne (errStr .Error) "" }} +
{{.Error | errStr}}
+{{ end }} +{{ end }} \ No newline at end of file diff --git a/templates/login/login_success.html.tpl b/templates/login/login_success.html.tpl new file mode 100644 index 0000000..426f290 --- /dev/null +++ b/templates/login/login_success.html.tpl @@ -0,0 +1,4 @@ +{{ define "content" }} +

Login

+
You're logged
+{{ end }} \ No newline at end of file diff --git a/templates/templates.go b/templates/templates.go new file mode 100644 index 0000000..57f1ce2 --- /dev/null +++ b/templates/templates.go @@ -0,0 +1,110 @@ +package templates + +import ( + _ "embed" + "html/template" + "mime/multipart" + "os" + "strconv" + "strings" + "sync" + + "github.com/rs/zerolog/log" +) + +//go:embed base.html.tpl +var base string + +//go:embed login/login_form.html.tpl +var loginForm string + +//go:embed login/login_success.html.tpl +var loginSuccess string + +//go:embed upload_form.html.tpl +var uploadForm string + +//go:embed home.html.tpl +var home string + +var funcMap = template.FuncMap{ + "year": func(s int) string { + if s == 0 { + return "" + } + return strconv.Itoa(s) + }, + "join": func(s []string) string { + if len(s) == 0 { + return "" + } else { + return strings.Join(s, ",") + } + }, + "filename": func(h *multipart.FileHeader) string { + if h != nil { + return h.Filename + } + return "" + }, + "errStr": func(err error) string { + if err != nil { + return err.Error() + } + return "" + }, +} + +var GetHome = 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) + } + + if _, err := baseTmpl.New("home").Parse(home); err != nil { + log.Err(err).Msg("unable to parse home tmpl") + os.Exit(1) + } + + return baseTmpl +}) + +var GetUploadForm = 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) + } + + if _, err := baseTmpl.New("uploadForm").Funcs(funcMap).Parse(uploadForm); err != nil { + log.Err(err).Msg("unable to parse upload tmpl") + os.Exit(1) + } + + return baseTmpl +}) + +var GetLoginForm = 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) + } + + if _, err := baseTmpl.New("loginForm").Funcs(funcMap).Parse(loginForm); err != nil { + log.Err(err).Msg("unable to parse login tmpl") + os.Exit(1) + } + + return baseTmpl +}) + +var GetLoginSuccess = sync.OnceValue[*template.Template](func() *template.Template { + tmpl, err := template.New("loginSuccess").Parse(loginSuccess) + if err != nil { + log.Err(err).Msg("unable to parse login success") + os.Exit(1) + } + return tmpl +}) diff --git a/handlers/upload/templates/form.html.tpl b/templates/upload_form.html.tpl similarity index 80% rename from handlers/upload/templates/form.html.tpl rename to templates/upload_form.html.tpl index bfd048e..b2c6f83 100644 --- a/handlers/upload/templates/form.html.tpl +++ b/templates/upload_form.html.tpl @@ -1,36 +1,5 @@ - - - - - - - - -

Upload a book

+{{define "content" }} +

Upload a book

@@ -92,8 +61,9 @@ {{ if ne .Error "" }}
{{.Error}}
{{ end }} - - - - \ No newline at end of file +{{ end }} \ No newline at end of file