Created
September 21, 2018 02:07
-
-
Save juanpabloaj/9ad53a88bc12ace3230ac8feaad5aab5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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