|
| 1 | +# Example middleware template for the GoFlash web framework |
| 2 | + |
| 3 | +<h1 align="center"> |
| 4 | + <a href="https://pkg.go.dev/github.com/goflash/example-template/v2@v2.0.0"> |
| 5 | + <img src="https://pkg.go.dev/badge/github.com/goflash/example-template.svg" alt="Go Reference"> |
| 6 | + </a> |
| 7 | + <a href="https://goreportcard.com/report/github.com/goflash/example-template"> |
| 8 | + <img src="https://img.shields.io/badge/%F0%9F%93%9D%20Go%20Report-A%2B-75C46B?style=flat-square" alt="Go Report Card"> |
| 9 | + </a> |
| 10 | + <a href="https://codecov.io/gh/goflash/example-template"> |
| 11 | + <img src="https://codecov.io/gh/goflash/example-template/graph/badge.svg?token=VRHM48HJ5L" alt="Coverage"> |
| 12 | + </a> |
| 13 | + <a href="https://github.com/goflash/example-template/actions?query=workflow%3ATest"> |
| 14 | + <img src="https://img.shields.io/github/actions/workflow/status/goflash/example-template/test-coverage.yml?branch=main&label=%F0%9F%A7%AA%20Tests&style=flat-square&color=75C46B" alt="Tests"> |
| 15 | + </a> |
| 16 | + <img src="https://img.shields.io/badge/go-1.23%2B-00ADD8?logo=golang" alt="Go Version"> |
| 17 | + <a href="https://docs.goflash.dev"> |
| 18 | + <img src="https://img.shields.io/badge/%F0%9F%92%A1%20GoFlash-docs-00ACD7.svg?style=flat-square" alt="GoFlash Docs"> |
| 19 | + </a> |
| 20 | + <img src="https://img.shields.io/badge/status-stable-green" alt="Status"> |
| 21 | + <img src="https://img.shields.io/badge/license-MIT-blue" alt="License"> |
| 22 | + <br> |
| 23 | + <div style="text-align:center"> |
| 24 | + <a href="https://discord.gg/QHhGHtjjQG"> |
| 25 | + <img src="https://dcbadge.limes.pink/api/server/https://discord.gg/QHhGHtjjQG" alt="Discord"> |
| 26 | + </a> |
| 27 | + </div> |
| 28 | +</h1> |
| 29 | + |
| 30 | +## Features |
| 31 | + |
| 32 | +- Minimal example middleware (`ExampleMiddleware`) showing the pattern |
| 33 | +- Runnable example app under `examples/basic` |
| 34 | +- Go module with Go 1.23 toolchain |
| 35 | +- Ready to copy/fork for new middleware in the GoFlash ecosystem |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +```sh |
| 40 | +go get github.com/goflash/example-template |
| 41 | +``` |
| 42 | + |
| 43 | +Go version: requires Go 1.23+. The module sets `go 1.23` and can be used with newer Go versions. |
| 44 | + |
| 45 | +## Quick start |
| 46 | + |
| 47 | +This is the minimal usage from `examples/basic`: |
| 48 | + |
| 49 | +```go |
| 50 | +package main |
| 51 | + |
| 52 | +import ( |
| 53 | + "log" |
| 54 | + "net/http" |
| 55 | + |
| 56 | + "github.com/goflash/example-template" |
| 57 | + "github.com/goflash/flash/v2" |
| 58 | +) |
| 59 | + |
| 60 | +func main() { |
| 61 | + app := flash.New() |
| 62 | + |
| 63 | + // Use the example middleware |
| 64 | + app.Use(example.ExampleMiddleware) |
| 65 | + |
| 66 | + // Define a simple route |
| 67 | + app.GET("/", func(c flash.Ctx) error { |
| 68 | + return c.String(http.StatusOK, "ok") |
| 69 | + }) |
| 70 | + |
| 71 | + log.Fatal(http.ListenAndServe(":8080", app)) |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Configuration |
| 76 | + |
| 77 | +This template doesn’t prescribe configuration. Customize the middleware to your needs by editing `ExampleMiddleware` (or replacing it with your own). A typical middleware should: |
| 78 | + |
| 79 | +- Accept and return a `flash.Handler` |
| 80 | +- Optionally read/write values on the request context |
| 81 | +- Call `next(c)` to pass control to the next handler |
| 82 | + |
| 83 | +## Examples |
| 84 | + |
| 85 | +- `examples/basic`: minimal setup showing how to plug the middleware into a GoFlash app and serve a simple route. |
| 86 | + |
| 87 | +Run the example locally: |
| 88 | + |
| 89 | +```sh |
| 90 | +cd examples/basic |
| 91 | +go run . |
| 92 | +``` |
| 93 | + |
| 94 | +Then in another shell: |
| 95 | + |
| 96 | +```sh |
| 97 | +curl -s localhost:8080/ |
| 98 | +``` |
| 99 | + |
| 100 | +## Testing |
| 101 | + |
| 102 | +Run tests with coverage: |
| 103 | + |
| 104 | +```sh |
| 105 | +./scripts/coverage.sh |
| 106 | +``` |
| 107 | + |
| 108 | +## Versioning and compatibility |
| 109 | + |
| 110 | +- Module path: `github.com/goflash/example-template` |
| 111 | +- Requires Go 1.23+ |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +Issues and PRs are welcome. Please run tests before submitting. |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +MIT |
0 commit comments