add home handler + add templates folder

This commit is contained in:
rmanach
2025-01-03 13:54:35 +01:00
parent f712242e39
commit 874378cf2d
14 changed files with 267 additions and 216 deletions
+37
View File
@@ -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)
}
}
+11 -8
View File
@@ -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()
-66
View File
@@ -1,66 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<style>
.main-container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: center;
}
.form-item {
align-self: flex-start;
margin: 10px;
width: 300px;
}
.form-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="post" enctype="multipart/form-data">
<div class="main-container">
<div class="form-item">
<div class="form-container">
<label>Username:</label>
<input type="text" name="username" value="{{.Username.Value}}" />
</div>
{{ if .Username.Err }}
<div class="error">{{.Username.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<label>Password:</label>
<input type="password" name="password" value="{{.Password.Value}}" />
</div>
{{ if .Password.Err }}
<div class="error">{{.Password.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<button id="submit" type="submit">Login</button>
</div>
</div>
</div>
</form>
{{ if ne (errStr .Error) "" }}
<div class="error">{{.Error | errStr}}</div>
{{ end }}
</body>
</html>
-13
View File
@@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<body>
<h1>Login</h1>
<div>You're logged</div>
<h2>Available urls</h2>
<ul>
<li><a href="/upload">Upload a book</a></li>
</ul>
</body>
</html>
-43
View File
@@ -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
})
+1 -1
View File
@@ -13,8 +13,8 @@ import (
"github.com/rs/zerolog/log"
"librapi/handlers/upload/templates"
"librapi/services"
"librapi/templates"
)
const MaxFileSize = 200 // in MB
-107
View File
@@ -1,107 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<style>
.main-container {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: center;
}
.form-item {
align-self: flex-start;
margin: 10px;
width: 300px;
}
.form-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>Upload a book</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<div class="main-container">
<div class="form-item">
<div class="form-container">
<label>Book name:</label>
<input type="text" name="name" value="{{.Name.Value}}" />
</div>
{{ if .Name.Err }}
<div class="error">{{.Name.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<label>Editor:</label>
<input type="text" name="editor" value="{{.Editor.Value}}" />
</div>
{{ if .Editor.Err }}
<div class="error">{{.Editor.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<label>Authors:</label>
<input type="text" name="authors" value="{{.Authors.Value | join}}" />
</div>
{{ if .Authors.Err }}
<div class="error">{{.Authors.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<label>Year:</label>
<input type="number" name="year" value="{{.Year.Value | year}}" />
</div>
{{ if .Year.Err }}
<div class="error">{{.Year.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<label>Keywords:</label>
<input type="text" name="keywords" value="{{.Keywords.Value | join}}" />
</div>
{{ if .Keywords.Err }}
<div class="error">{{.Keywords.Err}}</div>
{{ end }}
</div>
<div class="form-item">
<div class="form-container">
<input type="file" name="file" files="{{.File.Value.Header | filename}}" accept=".pdf" />
<button id="submit" type="submit">Upload</button>
</div>
</div>
</div>
</form>
{{ if .File.Err }}
<div class="error">{{.File.Err}}</div>
{{ end }}
{{ if ne .Error "" }}
<div class="error">{{.Error}}</div>
{{ end }}
</body>
<script type="text/javascript">
var isSuccess = {{.IsSuccess }};
var submit = document.getElementById("submit");
submit.addEventListener("submit", function () {
submit.disabled = true;
});
if (isSuccess) {
alert("file uploaded successfully");
}
</script>
</html>
-47
View File
@@ -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
})