Skip to content

Commit 55c4f63

Browse files
author
Vic Shóstak
committed
Update README.md
1 parent d23cebf commit 55c4f63

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# helpers
1+
# gowebly helpers
22

33
[![Go version][go_version_img]][go_dev_url]
44
[![Go report][go_report_img]][go_report_url]
@@ -8,9 +8,65 @@
88
A most useful helpers for build the best **Go** web applications with
99
[`gowebly`][gowebly_url] CLI.
1010

11+
## 📖 List of helpers
12+
13+
### `gowebly.ParseTemplates`
14+
15+
Helper to parse list of the given templates to the HTTP handler.
16+
17+
```go
18+
import (
19+
"log/slog"
20+
21+
"github.com/gowebly/helpers"
22+
)
23+
24+
func handler(w http.ResponseWriter, r *http.Request) {
25+
// Define paths to the user templates.
26+
indexPage := filepath.Join("templates", "pages", "index.html")
27+
indexLoginForm := filepath.Join("templates", "components", "index-login-form.html")
28+
29+
// Parse user templates or return error.
30+
tmpl, err := gowebly.ParseTemplates(indexPage, indexLoginForm) // gowebly helper
31+
if err != nil {
32+
w.WriteHeader(http.StatusBadRequest)
33+
slog.Error(err.Error(), "method", r.Method, "status", http.StatusBadRequest, "path", r.URL.Path)
34+
return
35+
}
36+
37+
// Execute (render) all templates or return error.
38+
if err := tmpl.Execute(w, nil); err != nil {
39+
w.WriteHeader(http.StatusInternalServerError)
40+
slog.Error(err.Error(), "method", r.Method, "status", http.StatusInternalServerError, "path", r.URL.Path)
41+
return
42+
}
43+
}
44+
```
45+
46+
> 💡 Note: The main layout template (`templates/main.html`) is already included.
47+
48+
### `gowebly.StaticFileServerHandler`
49+
50+
Helpers to create a custom handler for serve embed `./static` folder.
51+
52+
```go
53+
import (
54+
"embed"
55+
"net/http"
56+
57+
"github.com/gowebly/helpers"
58+
)
59+
60+
//go:embed static/*
61+
var static embed.FS
62+
63+
// Handle static files (with a custom handler).
64+
http.Handle("/static/", gowebly.StaticFileServerHandler(http.FS(static))) // gowebly helper
65+
```
66+
1167
## ⚠️ License
1268

13-
[`gowebly`][repo_url] is free and open-source software licensed
69+
[`gowebly helpers`][repo_url] is free and open-source software licensed
1470
under the [Apache 2.0 License][repo_license_url], created and supported by
1571
[Vic Shóstak][author_url] with 🩵 for people and robots.
1672

static_file_server_handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"net/http"
66
)
77

8-
// StaticFileServerHandler handles a custom handler for serve static folder in
9-
// the file system.
8+
// StaticFileServerHandler handles a custom handler for serve embed static folder.
109
//
1110
// Example:
1211
//

0 commit comments

Comments
 (0)