-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (34 loc) · 1.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"embed"
"fmt"
"io/fs"
"net/http"
"github.com/julienschmidt/httprouter"
)
//go:embed resources
var resources embed.FS
func main() {
router := httprouter.New()
directory, _ := fs.Sub(resources, "resources")
router.GET("/", SampleGetHandler)
router.POST("/", SamplePostHandler)
router.GET("/product/:id", GetUsedParamsHandler)
router.GET("/product/:id/items/:itemId", NamedParameterHandler)
router.GET("/images/*image", CatchAllParameterHandler)
router.ServeFiles("/files/*filepath", http.FS(directory))
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, i interface{}) {
fmt.Fprint(w, "Panic ", i)
}
router.GET("/panic", PanicHandler)
// handler custom for not found
router.NotFound = http.HandlerFunc(NotFoundHandler)
// handler custom for method not allowdd
router.MethodNotAllowed = http.HandlerFunc(MethodNotAllowedHandler)
middleware := &LogMiddleware{router}
server := http.Server{
Handler: middleware,
Addr: "localhost:8080",
}
server.ListenAndServe()
}