-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathhandlers.go
More file actions
34 lines (27 loc) · 856 Bytes
/
handlers.go
File metadata and controls
34 lines (27 loc) · 856 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 handlers
import (
"fmt"
"net/http"
"github.com/practicalgo/code/chap6/complex-server/config"
)
type app struct {
conf config.AppConfig
handler func(w http.ResponseWriter, r *http.Request, conf config.AppConfig)
}
func (a app) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.handler(w, r, a.conf)
}
func apiHandler(w http.ResponseWriter, r *http.Request, conf config.AppConfig) {
fmt.Fprintf(w, "Hello, world!")
}
func healthCheckHandler(w http.ResponseWriter, r *http.Request, conf config.AppConfig) {
if r.Method != "GET" {
conf.Logger.Printf("error=\"Invalid request\" path=%s method=%s", r.URL.Path, r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
fmt.Fprintf(w, "ok")
}
func panicHandler(w http.ResponseWriter, r *http.Request, conf config.AppConfig) {
panic("I panicked")
}