-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
56 lines (49 loc) · 1.54 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
56
// sally is an HTTP service that allows you to serve
// vanity import paths for your Go packages.
package main // import "go.uber.org/sally"
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
)
func main() {
yml := flag.String("yml", "sally.yaml", "yaml file to read config from")
tpls := flag.String("templates", "", "directory of .html templates to use")
port := flag.Int("port", 8080, "port to listen and serve on")
flag.Parse()
log.Printf("Parsing yaml at path: %s\n", *yml)
config, err := Parse(*yml)
if err != nil {
log.Fatalf("Failed to parse %s: %v", *yml, err)
}
var templates *template.Template
if *tpls != "" {
log.Printf("Parsing templates at path: %s\n", *tpls)
templates, err = getCombinedTemplates(*tpls)
if err != nil {
log.Fatalf("Failed to parse templates at %s: %v", *tpls, err)
}
} else {
templates = _templates
}
log.Printf("Creating HTTP handler with config: %v", config)
handler, err := CreateHandler(config, templates)
if err != nil {
log.Fatalf("Failed to create handler: %v", err)
}
log.Printf(`Starting HTTP handler on ":%d"`, *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), handler))
}
func getCombinedTemplates(dir string) (*template.Template, error) {
// Clones default templates to then merge with the user defined templates.
// This allows for the user to only override certain templates, but not all
// if they don't want.
templates, err := _templates.Clone()
if err != nil {
return nil, err
}
return templates.ParseGlob(filepath.Join(dir, "*.html"))
}