-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeController.go
41 lines (29 loc) · 976 Bytes
/
routeController.go
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
35
36
37
38
39
40
package main
import (
"fmt"
"net/http"
)
var registeredRoutes = map[string]func (http.ResponseWriter, *http.Request){
"/": homeController,
"/home": homeController,
"/contact": contactController,
}
func callController (w http.ResponseWriter, r *http.Request) {
if controllerCall, key := registeredRoutes[r.URL.Path]; key {
controllerCall(w, r)
}else{
notFoundController(w, r)
}
}
func homeController (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "You've made a request to the homeController!")
}
func contactController (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "You've made a request to the contactController!")
}
func notFoundController (w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "The page URL requested %v is invalid.\nPlease try one of the following instead:\n", r.URL.Path)
for key := range registeredRoutes {
fmt.Fprintf(w, "%v\n", key)
}
}