-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
120 lines (98 loc) · 2.87 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"flag"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/elseym/go-tzlib"
"github.com/elseym/go-tzlib/exporters"
"github.com/elseym/go-tzlib/importers"
"github.com/elseym/wo.istes.jetzt/responders"
)
var cfg = struct {
tzlib string
from string
webroot string
endpoint string
bind string
}{}
// init loads the configuration
func init() {
flag.StringVar(&cfg.tzlib, "tzlib", "./tzlib.json", "Use database file <tzlib>")
flag.StringVar(&cfg.from, "from", "", "Import HTML from <from> and write database to <zlib>")
flag.StringVar(&cfg.webroot, "webroot", "", "Serve the <webroot> directory at '/'")
flag.StringVar(&cfg.endpoint, "endpoint", "/dann", "Serve the API at <endpoint>")
flag.StringVar(&cfg.bind, "bind", "localhost:1620", "Listen on <bind> for connections. ")
flag.Parse()
}
// main starts wo.istes.jetzt
func main() {
if cfg.from != "" {
update(cfg.from, cfg.tzlib)
}
log.Print(serve(cfg.bind, cfg.tzlib, cfg.endpoint, cfg.webroot))
}
// serve registers handlers and waits for connections
func serve(bind, tzlibfile, endpoint, webroot string) error {
handleAPI(endpoint, tzlibfile)
// handleWebroot(webroot)
log.Printf("listening on 'http://%s'...", bind)
return http.ListenAndServe(bind, nil)
}
// handleAPI sets up the tzlib api server
func handleAPI(at, jsonfile string) {
tl := loadlib(jsonfile)
if !strings.HasSuffix(at, "/") {
at += "/"
}
handler := responders.DannResponder(tl)
http.Handle(at, http.StripPrefix(at, handler))
}
// handleWebroot sets up the static file server
func handleWebroot(webroot string) {
defer func() {
if r := recover(); r != nil {
log.Fatal("when using --webroot, --endpoint must not equal '/': ", r)
}
}()
_, err := os.Stat(webroot)
if err != nil && webroot != "" {
log.Fatal("webroot '", webroot, "' inaccessible: ", err)
}
if webroot != "" {
handler := responders.PublicResponder(webroot)
http.Handle("/", handler)
}
}
// loadlib is a convenient tzlib loader
func loadlib(jsonfile string) *tzlib.Tzlib {
tl, err := tzlib.Import(importers.NewJSONFile(jsonfile))
if err != nil {
log.Fatal("could not read timezones from'", jsonfile, "': ", err)
}
return tl
}
// update takes parsable html from file <from> or url <from>,
// then creates a new tzlib and writes it to file <to>
func update(from, to string) {
var importer tzlib.Importer
var err error
var l *tzlib.Tzlib
_, err = os.Stat(from)
if err == nil {
importer = importers.NewTimeIsFromFile(from)
} else {
importer = importers.NewTimeIsFromURL(from)
}
l, err = tzlib.Import(importer)
if err != nil {
log.Fatalf("error importing '%s': %s", from, err.Error())
}
err = l.Export(exporters.NewJSONFile(to))
if err != nil {
log.Fatalf("error exporting '%s': %s", to, err.Error())
}
log.Printf("update successful: got %d timezones, data expires %s", len(l.Timezones), l.Expires.Format(time.RFC822Z))
}