|
1 | | -# helpers |
| 1 | +# gowebly helpers |
2 | 2 |
|
3 | 3 | [![Go version][go_version_img]][go_dev_url] |
4 | 4 | [![Go report][go_report_img]][go_report_url] |
|
8 | 8 | A most useful helpers for build the best **Go** web applications with |
9 | 9 | [`gowebly`][gowebly_url] CLI. |
10 | 10 |
|
| 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 | + |
11 | 67 | ## ⚠️ License |
12 | 68 |
|
13 | | -[`gowebly`][repo_url] is free and open-source software licensed |
| 69 | +[`gowebly helpers`][repo_url] is free and open-source software licensed |
14 | 70 | under the [Apache 2.0 License][repo_license_url], created and supported by |
15 | 71 | [Vic Shóstak][author_url] with 🩵 for people and robots. |
16 | 72 |
|
|
0 commit comments