-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.go
59 lines (47 loc) · 1.74 KB
/
examples.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Package main shows examples using the API Router
*/
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/mrz1836/go-api-router"
"github.com/mrz1836/go-logger"
)
// main fires on load (go run examples.go)
func main() {
// Load the router & middleware
router := apirouter.New()
port := "3000"
// Create a middleware stack
s := apirouter.NewStack()
// Use a Julien middleware
s.Use(passThrough)
// Set the main index page (navigating to slash)
router.HTTPRouter.GET("/", s.Wrap(router.Request(index)))
// Set a test method (testing converting a standard handler to a handle)
router.HTTPRouter.GET("/test", s.Wrap(router.Request(apirouter.StandardHandlerToHandle(StdHandler()))))
// Set the options request on slash for CrossOrigin
router.HTTPRouter.OPTIONS("/", router.SetCrossOriginHeaders)
// Logout the loading of the API
logger.Data(2, logger.DEBUG, "starting API server...", logger.MakeParameter("port", port))
logger.Fatalln(http.ListenAndServe(":"+port, router.HTTPRouter))
}
// index basic request to /
func index(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
var welcomeMessageJSON = map[string]interface{}{"message": "Welcome to this simple API example!"}
apirouter.ReturnResponse(w, req, http.StatusOK, welcomeMessageJSON)
}
// passThrough is an example middleware
func passThrough(fn httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
logger.Data(2, logger.DEBUG, "middleware method hit!")
fn(w, r, p)
}
}
// StdHandler is an example standard handler
func StdHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Data(2, logger.DEBUG, "standard handler hit!")
})
}