-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (71 loc) · 1.79 KB
/
main.go
File metadata and controls
85 lines (71 loc) · 1.79 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/jatin-malik/url-shortener/urlshort"
)
// Command line flags
var yaml_file = flag.String("yaml", "", "yaml file for path and url mappings")
var json_file = flag.String("json", "", "json file for path and url mappings")
func ReadFile(filename string) ([]byte, error) {
file, err := os.Open(filename)
if err != nil {
return nil, errors.New("error opening file")
}
data, err := io.ReadAll(file)
if err != nil {
return nil, errors.New("error reading file")
}
return data, nil
}
func main() {
flag.Parse()
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)
var handler http.Handler = mapHandler
if *yaml_file != "" {
// Build the YAMLHandler using the mapHandler as the
// fallback
yaml_data, err := ReadFile(*yaml_file)
if err != nil {
log.Fatal(err)
}
yamlHandler, err := urlshort.YAMLHandler(yaml_data, mapHandler)
if err != nil {
panic(err)
}
handler = yamlHandler
}
if *json_file != "" {
// Build the JsonHandler using the mapHandler as the fallback
json_data, err := ReadFile(*json_file)
if err != nil {
log.Fatal(err)
}
jsonHandler, err := urlshort.JSONHandler(json_data, mapHandler)
if err != nil {
panic(err)
}
handler = jsonHandler
}
fmt.Println("Starting the server on :8080")
http.ListenAndServe(":8080", handler)
}
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!")
}