-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmjau.go
103 lines (96 loc) · 2.84 KB
/
mjau.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
// Copyright (c) 2012, Robert Dinu. All rights reserved.
// Use of this source code is governed by a BSD-style
// license which can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"text/template"
ihttp "github.com/noll/mjau/http" // Internal http package.
"github.com/noll/mjau/inventory"
"github.com/noll/mjau/util"
"github.com/noll/mjau/whitelist"
)
const (
ProgName = "mjau"
ProgVersion = "0.1"
)
var (
bFlag = flag.String("b", "0.0.0.0:80", "TCP address to bind to")
eFlag = flag.Bool("e", false, "toggle entity tags validation")
gFlag = flag.Bool("g", false, "toggle response gzip compression")
lFlag = flag.String("l", "fonts/", "path to font library")
mFlag = flag.Uint64("m", 2592000, "Cache-Control max-age value")
oFlag = flag.Bool("o", false, "toggle cross-origin resource sharing")
tFlag = flag.String("t", "templates/", "path to templates directory")
vFlag = flag.Bool("v", false, "display version number and exit")
wFlag = flag.String("w", "whitelist.json", "path to whitelist file")
)
func init() {
util.BlankStrFlagDefault(bFlag, "b")
util.BlankStrFlagDefault(lFlag, "l")
util.BlankStrFlagDefault(tFlag, "t")
util.BlankStrFlagDefault(wFlag, "w")
*lFlag = filepath.FromSlash(*lFlag)
*wFlag = filepath.FromSlash(*wFlag)
}
func main() {
flag.Parse()
if *vFlag {
fmt.Println(ProgName, ProgVersion)
os.Exit(0)
}
// Build font inventory.
fontInventory := inventory.New()
if err := fontInventory.Build(*lFlag); err != nil {
PrintErrorExit(err.Error())
}
if fontInventory.Len() == 0 {
PrintErrorExit(fmt.Sprintf("%s: empty font library", *lFlag))
}
// Read whitelist.
whitelist := whitelist.New()
if err := whitelist.Read(*wFlag); err != nil {
PrintErrorExit(err.Error())
}
if whitelist.Size() == 0 {
PrintErrorExit(fmt.Sprintf("%s: empty whitelist", *wFlag))
}
// Parse templates.
templatesPath := filepath.FromSlash(*tFlag)
eot := filepath.Join(templatesPath, "eot.css.tmpl")
woff := filepath.Join(templatesPath, "woff.css.tmpl")
var templates *template.Template
var err error
if templates, err = template.ParseFiles(eot, woff); err != nil {
PrintErrorExit(err.Error())
}
// Create CSS handler function.
var cssHandler http.HandlerFunc
ctx := ihttp.HandlerContext{
Flags: ihttp.Flags{
AcAllowOrigin: *oFlag,
CcMaxAge: *mFlag,
Etag: *eFlag,
Gzip: *gFlag,
Version: ProgName + "/" + ProgVersion,
},
Inventory: *fontInventory,
Templates: *templates,
Whitelist: *whitelist,
}
cssHandler = ihttp.MakeHandler(ihttp.CssHandler, ctx)
if *gFlag {
// Enable response gzip compression.
cssHandler = ihttp.MakeGzipHandler(cssHandler)
}
// Register CSS HTTP handler.
http.HandleFunc("/css/", cssHandler)
// Start HTTP server.
if err := http.ListenAndServe(*bFlag, nil); err != nil {
PrintErrorExit(err.Error())
}
}