/
main.go
197 lines (159 loc) · 4.05 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"time"
"github.com/AlexanderThaller/logger"
"github.com/juju/errgo"
"github.com/julienschmidt/httprouter"
"github.com/spf13/viper"
)
const (
// Name is the name of the application. Used in logging.
Name = "wikgo"
)
var (
BuildHash string
BuildTime string
)
func init() {
err := configure()
if err != nil {
panic(err)
}
loglevel, err := logger.ParsePriority(viper.GetString("LogLevel"))
if err != nil {
panic(err)
}
err = logger.SetLevel(".", loglevel)
if err != nil {
panic(err)
}
}
func main() {
l := logger.New(Name, "main")
l.Info("Version: ", fmt.Sprintf("%v-b%v", BuildHash, BuildTime))
pagesFolder := viper.GetString("PagesFolder")
binding := viper.GetString("Binding")
router := httprouter.New()
router.GET("/", rootHandler)
router.GET("/"+pagesFolder+"/*path", pagesHandler)
l.Notice("Listening on ", binding)
err := http.ListenAndServe(binding, router)
if err != nil {
l.Alert(errgo.Notef(err, "can not listen on binding"))
os.Exit(1)
}
}
func rootHandler(wr http.ResponseWriter, re *http.Request, ps httprouter.Params) {
pagesFolder := viper.GetString("PagesFolder")
http.Redirect(wr, re, "/"+pagesFolder+"/", 301)
}
func pagesHandler(wr http.ResponseWriter, re *http.Request, ps httprouter.Params) {
l := logger.New(Name, "pagesHandler")
timestart := time.Now()
ip, _, _ := net.SplitHostPort(re.RemoteAddr)
path := path.Clean("./" + re.URL.Path)
l.Notice("Sending ", path, " to ", ip)
stat, err := os.Stat(path)
if err != nil {
printerr(l, wr, errgo.Notef(err, "can not stat path"))
return
}
if stat.Mode().IsDir() {
l.Trace("Filetype: Directory")
pagesHandlerDirectory(wr, re, ps)
}
if stat.Mode().IsRegular() {
l.Trace("Filetype: File")
pagesHandlerFile(wr, re, ps)
}
if !stat.Mode().IsDir() && !stat.Mode().IsRegular() {
l.Error("Filetype is not a directory and not a regular file. Something is strange.")
}
l.Debug("Sent ", path, " (", time.Since(timestart), ")")
}
func pagesHandlerDirectory(wr http.ResponseWriter, re *http.Request, ps httprouter.Params) {
l := logger.New(Name, "pagesHandlerDirectory")
urlpath := "./" + re.URL.Path
files, err := ioutil.ReadDir(urlpath)
if err != nil {
printerr(l, wr, errgo.Notef(err, "can not read from directory"))
return
}
fmt.Fprintf(wr, `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>`)
for _, file := range files {
url, err := url.Parse(path.Clean(re.URL.Path + "/" + file.Name()))
if err != nil {
l.Error(errgo.Notef(err, "can not escape url"))
}
fmt.Fprintf(wr, "<a href=%s>%s</a>", url.String(), file.Name())
fmt.Fprint(wr, "<br>\n")
}
fmt.Fprintf(wr, `</body>
</html>`)
}
func pagesHandlerFile(wr http.ResponseWriter, re *http.Request, ps httprouter.Params) {
l := logger.New(Name, "pagesHandlerDirectory")
path := "./" + re.URL.Path
file, err := os.Open(path)
if err != nil {
printerr(l, wr, errgo.Notef(err, "can not open file for reading"))
return
}
defer file.Close()
queries := re.URL.Query()
ext := filepath.Ext(path)
_, sendraw := queries["raw"]
if sendraw {
ext = ""
}
l.Trace("Filepath Extention: ", ext)
switch ext {
case ".asciidoc":
err = asciiDoctor(file, wr)
if err != nil {
printerr(l, wr, errgo.Notef(err, "can not format file with asciidoctor"))
return
}
default:
_, err = io.Copy(wr, file)
if err != nil {
printerr(l, wr, errgo.Notef(err, "can not copy file to response writer"))
return
}
}
}
func printerr(l logger.Logger, wr http.ResponseWriter, err error) {
l.Error(errgo.Details(err))
fmt.Fprintf(wr, errgo.Details(err))
return
}
func asciiDoctor(reader io.Reader, writer io.Writer) error {
stderr := new(bytes.Buffer)
command := exec.Command("asciidoctor", "-")
command.Stdin = reader
command.Stdout = writer
command.Stderr = stderr
err := command.Run()
if err != nil {
return errgo.Notef(errgo.Notef(err, "can not run asciidoctor"),
stderr.String())
}
return nil
}