Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var AddFeedbackHandler = http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { var product Product vars := mux.Vars(r) slug := vars["slug"] for _, p := range products { if p.Slug == slug { product = p } } w.Header().Set("Content-Type", "application/json") if product.Slug != "" { payload, _ := json.Marshal(product) w.Write([]byte(payload)) } else { w.Write([]byte("Product Not Found")) } })
The feedback handler will add either positive or negative feedback to the product
We would normally save this data to the database - but for this demo we'll fake it so that as long as the request is successful and we can match a product to our catalog of products we'll return an OK status.
View Source
var GetTokenHandler = http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { token := jwt.New(jwt.SigningMethodHS256) token.Claims["admin"] = true token.Claims["name"] = "Ado Kukic" token.Claims["exp"] = time.Now().Add(time.Hour * 24).Unix() tokenString, _ := token.SignedString(mySigningKey) w.Write([]byte(tokenString)) })
View Source
var NotImplemented = http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Not Implemented")) })
Here we are implementing the NotImplemented handler. Whenever an API endpoint is hit we will simply return the message "Not Implemented"
View Source
var ProductsHandler = http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { payload, _ := json.Marshal(products) w.Header().Set("Content-Type", "application/json") w.Write([]byte(payload)) })
The products handler will be called when the user makes a GET request to the /products endpoint.
This handler will return a list of products available for users to review
View Source
var StatusHandler = http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("API is up and running")) })
Handlers The status handler will be invoked when the user calls the /status route
It will simply return a string with the message "API is up and running"
Functions ¶
This section is empty.
Types ¶
Click to show internal directories.
Click to hide internal directories.