Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Created September 21, 2018 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/9ad53a88bc12ace3230ac8feaad5aab5 to your computer and use it in GitHub Desktop.
Save juanpabloaj/9ad53a88bc12ace3230ac8feaad5aab5 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/http"
)
type handlers struct{}
func (h *handlers) Middleware(next http.HandlerFunc) http.HandlerFunc {
log.Println("defining middleware")
return func(w http.ResponseWriter, r *http.Request) {
log.Println("in middleware")
next(w, r)
}
}
func (h *handlers) SetupRoutes(mux *http.ServeMux) {
mux.HandleFunc("/", h.Middleware(analyzeRequest))
}
func childFunc() {
log.Println("in childFunc")
}
func analyzeRequest(w http.ResponseWriter, r *http.Request) {
log.Println("in analyzeRequest")
childFunc()
message := "hello from go server"
w.Write([]byte(message))
}
func main() {
mux := http.NewServeMux()
handler := &handlers{}
handler.SetupRoutes(mux)
ipPort := ":8080"
srv := &http.Server{
Addr: ipPort,
Handler: mux,
}
log.Println("listening in " + ipPort)
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment