-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (122 loc) · 3.58 KB
/
Copy pathmain.go
File metadata and controls
136 lines (122 loc) · 3.58 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"net/http"
"os"
"log"
"github.com/jawher/mow.cli"
)
const (
Version = "0.7.1"
DefaultTheme = "clean"
)
func main() {
app := cli.App("servemd", "a simple http server for markdown content")
app.Version("v version", Version)
app.Spec = "[OPTIONS] [DIR]"
var (
// HTTP Options
host = app.String(cli.StringOpt{
Name: "a host",
Desc: "Host/IP address to listen on for HTTP",
Value: "",
EnvVar: "HOST",
})
port = app.Int(cli.IntOpt{
Name: "p port",
Desc: "TCP PORT to listen on for HTTP",
Value: 3000,
EnvVar: "PORT",
})
users = app.Strings(cli.StringsOpt{
Name: "u auth",
Desc: "Username and password for basic authentication in the form of user:pass",
EnvVar: "BASIC_AUTH",
})
robotsTag = app.String(cli.StringOpt{
Name: "r x-robots-tag",
Desc: "Sets a X-Robots-Tag header",
EnvVar: "X_ROBOTS_TAG",
Value: "",
})
// Content
dir = app.String(cli.StringArg{
Name: "DIR",
Desc: "Directory to serve content from",
Value: ".",
EnvVar: "DOCUMENT_ROOT",
})
extension = app.String(cli.StringOpt{
Name: "e extension",
Desc: "Extension used for markdown files",
Value: ".md",
EnvVar: "DOCUMENT_EXTENSION",
})
index = app.String(cli.StringOpt{
Name: "i index",
Desc: "Filename (without extension) to use for directory index",
Value: "index",
EnvVar: "DIRECTORY_INDEX",
})
// Theme
markdownTheme = app.String(cli.StringOpt{
Name: "m markdown-theme",
Desc: "Theme to use for styling markdown html",
Value: DefaultTheme,
EnvVar: "MARKDOWN_THEME",
})
typekitKitID = app.String(cli.StringOpt{
Name: "t typekit-kit-id",
Desc: "ID of webfont kit to include from typekit",
Value: DefaultTheme,
EnvVar: "TYPEKIT_KIT_ID",
})
codeTheme = app.String(cli.StringOpt{
Name: "c code-theme",
Desc: "Highlight.js theme to use for syntax highlighting",
Value: "",
EnvVar: "CODE_THEME",
})
)
app.Action = func() {
// Static Asset Handler
staticAssetHandler := staticAssetServer()
staticAssetHandlerFunc := func(w http.ResponseWriter, r *http.Request) {
staticAssetHandler.ServeHTTP(w, r)
}
staticAssetHandlerFunc = headerMiddleware(staticAssetHandlerFunc)
staticAssetHandlerFunc = basicAuthMiddleware(staticAssetHandlerFunc, *users)
staticAssetHandlerFunc = robotsTagMiddleware(staticAssetHandlerFunc, *robotsTag)
http.HandleFunc("/assets/", staticAssetHandlerFunc)
// Setup the markdown theme (may be custom or bundled)
themePath, themeHandler := theme(*markdownTheme)
if themeHandler != nil {
themeHandler = headerMiddleware(themeHandler)
themeHandler = basicAuthMiddleware(themeHandler, *users)
themeHandler = robotsTagMiddleware(themeHandler, *robotsTag)
http.HandleFunc(themePath, themeHandler)
}
// Markdown File Handler
markdownHandlerFunc := markdownHandleFunc(MarkdownHandlerOptions{
DocRoot: *dir,
DocExtension: *extension,
DirIndex: *index,
MarkdownTheme: themePath,
TypekitKitID: *typekitKitID,
CodeTheme: *codeTheme,
})
markdownHandlerFunc = headerMiddleware(markdownHandlerFunc)
markdownHandlerFunc = basicAuthMiddleware(markdownHandlerFunc, *users)
markdownHandlerFunc = robotsTagMiddleware(markdownHandlerFunc, *robotsTag)
http.HandleFunc("/", markdownHandlerFunc)
// Start HTTP server
addr := fmt.Sprintf("%s:%d", *host, *port)
log.Printf("Starting server on %s", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Printf("Error starting server: %s", err)
cli.Exit(1)
}
}
app.Run(os.Args)
}