Facilitator for handling gorilla/mux requests.
Basically http.ResponseWriter
and http.Request
are encapsulated in two structs:
geh.Response
and geh.Request
. Both come with methods that facilitate the implementation
and don't require developer to worry about some things.
First, we create a basic Gorrila/Mux API.
func HomeHandler(
request geh.Request,
response geh.Response,
arguments ...interface{},
) {
printSomething := arguments[0].(func (string))
printSomething("print something")
geh.Status(http.StatusOk).
Json(map[string]interface{}{
"hello": "I",
"am": "okay"
})
}
func main() {
func printSomething(something string) {
print(something)
}
r := geh.NewRouter("API Name", "API Version")
r.HandleFunc(
http.MethodGet, // HTTP Method
"/", // Endpoint
geh.Handler(HomeHandler, printSomething), // Handler
geh.DocEndpoint{ // Documentation to be rendered by swagger
Summary: "Home",
},
)
http.ListenAndServe(":8080", r.GetMuxRouter())
}
Endpoints documentation is generated by Swagger.
It's accessed through the endpoint /docs
.
Returns HTTP Method used in current request.
Returns current URI.
Returns current URL.
Get a URL param value. If not found return an empty string.
func HomeHandler(request geh.Request, response geh.Response) {
response.Text(fmt.sprintf(
"My ID is %s",
request.GetParams("id"),
))
}
Get query string from URL. If not found return an empty string.
func HomeHandler(request geh.Request, geh.Response) {
response.Text(fmt.sprintf(
"My ID in query string is %s",
request.GetQueryString("id")
))
}
Get a header value. If not found return an empty string.
func HomeHandler(request geh.Request, geh.Response) {
response.Text(fmt.sprintf(
"My Content-Type is %s",
request.GetHeader("Content-Type")
))
}