diff options
author | xengineering <me@xengineering.eu> | 2024-02-11 22:44:32 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-02-13 20:26:42 +0100 |
commit | 40868be7d4b9e1d9909a19dccc25ec49c1b5924b (patch) | |
tree | 97a8923b73c97a4ce92f15ad632cfb65faf06d5c | |
parent | 43fdfde44bce659abd30186150f667d8ba24cf2b (diff) | |
download | ceres-40868be7d4b9e1d9909a19dccc25ec49c1b5924b.tar ceres-40868be7d4b9e1d9909a19dccc25ec49c1b5924b.tar.zst ceres-40868be7d4b9e1d9909a19dccc25ec49c1b5924b.zip |
view: Implement GET handler for model.Recipes
-rw-r--r-- | main.go | 4 | ||||
-rw-r--r-- | view/html/footer.html | 5 | ||||
-rw-r--r-- | view/html/head.html | 8 | ||||
-rw-r--r-- | view/html/recipes.html | 40 | ||||
-rw-r--r-- | view/recipes.go | 23 | ||||
-rw-r--r-- | view/templates.go | 15 |
6 files changed, 95 insertions, 0 deletions
@@ -10,6 +10,7 @@ import ( "syscall" "xengineering.eu/ceres/model" + "xengineering.eu/ceres/view" "github.com/gorilla/mux" ) @@ -21,6 +22,8 @@ func main() { model.InitDatabase() defer model.CloseDatabase() + view.Init() + var srv *http.Server = startServer("127.0.0.1:8080") go srv.ListenAndServe() defer stopServer(srv) @@ -40,6 +43,7 @@ func startServer(addr string) *http.Server { r.PathPrefix("/static/"). Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static)))) + r.HandleFunc("/recipes", view.Recipes).Methods(`GET`) muxer := http.NewServeMux() muxer.Handle("/", r) diff --git a/view/html/footer.html b/view/html/footer.html new file mode 100644 index 0000000..5128b7f --- /dev/null +++ b/view/html/footer.html @@ -0,0 +1,5 @@ +{{define "footer"}} +<footer> + <p>The <a href="https://xengineering.eu/git/ceres">Ceres</a> recipe server is licensed under <a href="https://www.gnu.org/licenses/agpl-3.0.en.html">AGPL v3</a> and developed with <a href="https://simplecss.org/">simple.css</a>.</p> +</footer> +{{end}} diff --git a/view/html/head.html b/view/html/head.html new file mode 100644 index 0000000..91ae004 --- /dev/null +++ b/view/html/head.html @@ -0,0 +1,8 @@ +{{define "head"}} +<head> + <title>Recipes</title> + <meta charset="utf-8"/> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link rel="stylesheet" href="/static/view/static/simple.css/simple.css" type="text/css"> +</head> +{{end}} diff --git a/view/html/recipes.html b/view/html/recipes.html new file mode 100644 index 0000000..1b7b1c1 --- /dev/null +++ b/view/html/recipes.html @@ -0,0 +1,40 @@ +{{define "recipes"}} +<html> + {{ template "head" }} + <header> + <nav> + <a href="/">HOME</a> + </nav> + <h1>Recipe overview</h1> + </header> + <body> + <main> + <p>Here are the available recipes 😋🍳🍔🍕🥘</p> + <input id="search" onkeyup="filter()" type="text" placeholder="Search for a recipe ..."></input> + <ul id="recipes">{{range .}} + <li><a href="./recipe/{{.Id}}">{{.Title}}</a></li>{{end}} + </ul> + </main> + {{ template "footer" }} + <script> + function filter() { + var input, query, ul, li, a, i, txtValue; + input = document.getElementById('search'); + query = input.value.toUpperCase(); + ul = document.getElementById("recipes"); + li = ul.getElementsByTagName('li'); + + for (i = 0; i < li.length; i++) { + a = li[i].getElementsByTagName("a")[0]; + txtValue = a.textContent || a.innerText; + if (txtValue.toUpperCase().indexOf(query) > -1) { + li[i].style.display = ""; + } else { + li[i].style.display = "none"; + } + } + } + </script> + </body> +</html> +{{end}} diff --git a/view/recipes.go b/view/recipes.go new file mode 100644 index 0000000..3a5fbf8 --- /dev/null +++ b/view/recipes.go @@ -0,0 +1,23 @@ +package view + +import ( + "net/http" + + "xengineering.eu/ceres/model" +) + +func Recipes(w http.ResponseWriter, r *http.Request) { + recipes := make(model.Recipes, 0) + + err := recipes.Read() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + err = html.ExecuteTemplate(w, "recipes", recipes) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} diff --git a/view/templates.go b/view/templates.go new file mode 100644 index 0000000..d08f95e --- /dev/null +++ b/view/templates.go @@ -0,0 +1,15 @@ +package view + +import ( + "embed" + "html/template" +) + +//go:embed html/*.html +var htmlFS embed.FS + +var html *template.Template + +func Init() { + html = template.Must(template.New("html").ParseFS(htmlFS, "html/*.html")) +} |