-
Notifications
You must be signed in to change notification settings - Fork 5
/
gone.go
133 lines (113 loc) · 3.38 KB
/
gone.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
121
122
123
124
125
126
127
128
129
130
131
132
133
// Gone is a wiki written in Go. It is designed with server owners and
// administrators in mind and follows the KISS principles.
//
// By default, gone simply starts a HTTP server at port 8080 and servers files
// from the working directory.
// Invoke with -help flag to see configuration options.
package main
import (
"os"
"github.com/fxnn/gone/authenticator"
"github.com/fxnn/gone/authenticator/bruteblocker"
"github.com/fxnn/gone/config"
"github.com/fxnn/gone/http"
"github.com/fxnn/gone/http/templates"
"github.com/fxnn/gone/log"
"github.com/fxnn/gone/store/filestore"
"github.com/fxnn/gopath"
)
const defaultTemplateDirectoryName = ".templates"
func main() {
log.Printf("--- gone startup ---")
cfg := config.FromCommandline()
switch cfg.Command {
case config.CommandExportTemplates:
exportTemplates(cfg)
case config.CommandListen:
listen(cfg)
case config.CommandHelp:
config.PrintUsage()
}
}
func exportTemplates(cfg config.Config) {
var target = templatePath(contentRoot(), cfg)
if target.IsEmpty() {
target = contentRoot().JoinPath(defaultTemplateDirectoryName)
}
if err := templates.NewStaticLoader().WriteAllTemplates(target); err != nil {
log.Fatalf("error exporting templates: %s", err)
}
}
func listen(cfg config.Config) {
var cr = contentRoot()
var auth = authenticator.NewContextAuthenticator()
var httpAuth = createHttpAuthenticator(auth, cr, cfg)
var store = filestore.New(cr, auth)
var loader = createLoader(cr, cfg)
http.ListenAndServe(cfg.BindAddress, httpAuth, store, loader)
}
func createHttpAuthenticator(
auth authenticator.Authenticator,
contentRoot gopath.GoPath,
cfg config.Config,
) authenticator.HttpAuthenticator {
var bruteBlocker = bruteblocker.New(
cfg.BruteforceMaxDelay,
cfg.BruteforceDelayStep,
cfg.BruteforceDelayStep/5,
cfg.BruteforceDelayStep/20,
cfg.BruteforceDropDelayAfter,
)
if cfg.RequireSSLHeader != "" {
log.Printf("Requiring SSL header %s on login (by configuration)", cfg.RequireSSLHeader)
}
return authenticator.NewHttpBasicAuthenticator(
auth,
htpasswdFilePath(contentRoot),
cfg.RequireSSLHeader,
bruteBlocker,
)
}
func htpasswdFilePath(contentRoot gopath.GoPath) gopath.GoPath {
htpasswdFile := contentRoot.JoinPath(".htpasswd")
if !htpasswdFile.IsExists() {
log.Printf("no .htpasswd found")
} else {
log.Printf("using authentication data from .htpasswd")
}
return htpasswdFile
}
func createLoader(contentRoot gopath.GoPath, cfg config.Config) templates.Loader {
var templatePath = templatePath(contentRoot, cfg)
if !templatePath.IsEmpty() {
return templates.NewFilesystemLoader(templatePath)
}
return templates.NewStaticLoader()
}
func templatePath(contentRoot gopath.GoPath, cfg config.Config) (result gopath.GoPath) {
// configuration
result = gopath.FromPath(cfg.TemplatePath)
if !result.IsEmpty() {
if !result.IsDirectory() {
log.Fatalf("configured template path is no directory: %s", result.Path())
}
log.Printf("using templates from %s (by configuration)", result.Path())
return result
}
// convention
result = contentRoot.JoinPath(defaultTemplateDirectoryName)
if result.IsDirectory() {
log.Printf("using templates from %s (by convention)", result.Path())
return result
}
// default
log.Printf("using default templates")
return gopath.Empty()
}
func contentRoot() gopath.GoPath {
if wd, err := os.Getwd(); err == nil {
return gopath.FromPath(wd)
} else {
panic(err)
}
}