-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
34 lines (28 loc) · 831 Bytes
/
Copy pathmain.go
File metadata and controls
34 lines (28 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"fmt"
"net/http"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "welcome to my server")
}
func greetHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
fmt.Fprintf(w, "Hello %s\n", name)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%s %s\n", r.Method, r.URL.Path)
next(w, r)
}
}
func main() {
http.HandleFunc("/", loggingMiddleware(homeHandler))
http.HandleFunc("/hello", loggingMiddleware(greetHandler))
http.HandleFunc("/health", loggingMiddleware(healthHandler))
fmt.Println("Server is listening on port 8080:")
http.ListenAndServe(":8080", nil)
}