-
Notifications
You must be signed in to change notification settings - Fork 375
/
main.go
44 lines (37 loc) · 1013 Bytes
/
main.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
41
42
43
44
package main
import (
"fmt"
"net/http"
"github.com/gophercises/urlshort"
)
func main() {
mux := defaultMux()
// Build the MapHandler using the mux as the fallback
pathsToUrls := map[string]string{
"/urlshort-godoc": "https://godoc.org/github.com/gophercises/urlshort",
"/yaml-godoc": "https://godoc.org/gopkg.in/yaml.v2",
}
mapHandler := urlshort.MapHandler(pathsToUrls, mux)
// Build the YAMLHandler using the mapHandler as the
// fallback
yaml := `
- path: /urlshort
url: https://github.com/gophercises/urlshort
- path: /urlshort-final
url: https://github.com/gophercises/urlshort/tree/solution
`
yamlHandler, err := urlshort.YAMLHandler([]byte(yaml), mapHandler)
if err != nil {
panic(err)
}
fmt.Println("Starting the server on :8080")
http.ListenAndServe(":8080", yamlHandler)
}
func defaultMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
return mux
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, world!")
}