/
main.go
55 lines (42 loc) · 1.15 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"net/http"
urlshort "github.com/anishkumarsingh93/gophercises-url-shortener/pkg"
)
var port = ":8080"
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",
"/google": "https://google.com",
}
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
- path: /apple
url: https://apple.com
`
yamlHandler, err := urlshort.YAMLHandler([]byte(yaml), mapHandler)
if err != nil {
panic(err)
}
//starting the server
fmt.Println("Starting server at port: ", port)
http.ListenAndServe(port, 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!")
}