This repository was archived by the owner on Jul 31, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed
Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 1- # golang-template
1+ # middlechain
2+
3+ The simple HTTP middleware chainer for Golang.
4+
5+ ## How to use
6+
7+ ``` go
8+ mux := http.NewServeMux ()
9+
10+ handler := middlechain.Chain (mux,
11+ someMiddleware,
12+ )
13+
14+ // handle http request...
15+
16+ http.ListenAndServe (" :8080" , handler)
17+ ```
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "net/http"
5+ "strings"
6+
7+ "github.com/Pranc1ngPegasus/middlechain"
8+ )
9+
10+ func main () {
11+ mux := http .NewServeMux ()
12+
13+ handler := middlechain .Chain (mux ,
14+ heaatbeat ("/ping" ),
15+ )
16+
17+ if err := http .ListenAndServe (":8080" , handler ); err != nil {
18+ panic (err )
19+ }
20+ }
21+
22+ func heaatbeat (endpoint string ) func (h http.Handler ) http.Handler {
23+ return func (h http.Handler ) http.Handler {
24+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
25+ if (r .Method == http .MethodGet || r .Method == http .MethodHead ) && strings .EqualFold (r .URL .Path , endpoint ) {
26+ w .Header ().Set ("Content-Type" , "text/plain" )
27+ w .WriteHeader (http .StatusOK )
28+ w .Write ([]byte ("." ))
29+
30+ return
31+ }
32+ })
33+ }
34+ }
Original file line number Diff line number Diff line change 1- module github.com/Pranc1ngPegasus/golang-template
1+ module github.com/Pranc1ngPegasus/middlechain
22
33go 1.19
44
You can’t perform that action at this time.
0 commit comments