46
loading...
This website collects cookies to deliver better user experience
go mid init <name-of-your-project>
go get github.com/justinas/alice
on the cli. main.go
, handlers.go
& middlewares.go
files. package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Lesson struct {
Title string
Summary string
}
func handle(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// create a variable of our defined struct type Lesson
var tempLesson Lesson
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&tempLesson)
if err != nil {
panic(err)
}
defer r.Body.Close()
fmt.Printf("Title: %s. Summary: %s\n", tempLesson.Title, tempLesson.Summary)
w.WriteHeader(http.StatusCreated)
w.Write([]byte("201 - Created"))
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("405 - Method Not Allowed"))
}
}
package main
import (
"log"
"net/http"
"strconv"
"time"
)
func filterContentType(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Inside filterContentType middleware, before the request is handled")
// filter requests by mime type
if r.Header.Get("Content-Type") != "application/json" {
w.WriteHeader(http.StatusUnsupportedMediaType)
w.Write([]byte("415 - Unsupported Media Type. JSON type expected"))
return
}
// handle the request
handler.ServeHTTP(w, r)
log.Println("Inside filterContentType middleware, after the request was handled")
})
}
func setServerTimeCookie(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Inside setServerTimeCookie middleware, before the request is handled")
// handles the request
handler.ServeHTTP(w, r)
cookie := http.Cookie{Name: "Server Time(UTC)", Value: strconv.FormatInt(time.Now().Unix(), 10)}
http.SetCookie(w, &cookie)
log.Println("Inside setServerTimeCookie middleware, after the request is handled")
})
}
package main
// chaing middleware with alice.
// here we will use the alice package to demo chainging middleware
// around our original or main handler.
import (
"log"
"net/http"
"github.com/justinas/alice"
)
func main() {
port := ":8080"
originalHandler := http.HandlerFunc(handle)
chain := alice.New(filterContentType, setServerTimeCookie).Then(originalHandler)
http.Handle("/lesson", chain)
log.Fatal(http.ListenAndServe(port, nil))
}
alice
package makes for clear and readable code when used.