/
serve.go
172 lines (140 loc) · 3.26 KB
/
serve.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
package main
import (
"encoding/base64"
"encoding/json"
"flag"
"github.com/kellegous/pork"
"github.com/russross/blackfriday"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
)
func rootPath() (string, error) {
_, file, _, _ := runtime.Caller(0)
return filepath.Abs(filepath.Dir(file))
}
func setup(data string, r pork.Router) {
r.HandleFunc("/save", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid", http.StatusMethodNotAllowed)
return
}
var req struct {
Path string
Data string
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
panic(err)
}
b, err := base64.StdEncoding.DecodeString(req.Data)
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(filepath.Join(data, req.Path), b, os.ModePerm); err != nil {
panic(err)
}
res := map[string]interface{}{
"Error": nil,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(res); err != nil {
panic(err)
}
})
}
type handler struct {
d string
c pork.Handler
t string
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set("Content-Type", "text/html;charset=utf8")
projs, err := projects(h.d)
if err != nil {
panic(err)
}
if err := template.Must(template.ParseFiles(h.t)).Execute(w, projs); err != nil {
panic(err)
}
return
}
h.c.ServeHTTP(w, r)
}
type project struct {
Name string
Desc template.HTML
}
func readProject(dir string) (*project, error) {
r, err := ioutil.ReadFile(filepath.Join(dir, "readme.md"))
if err != nil {
return nil, err
}
return &project{
Name: filepath.Base(dir),
Desc: template.HTML(string(blackfriday.MarkdownCommon(r))),
}, nil
}
func projects(dir string) ([]*project, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
var p []*project = nil
for _, file := range files {
name := file.Name()
if len(name) != 3 {
continue
}
proj, err := readProject(filepath.Join(dir, name))
if err != nil {
continue
}
p = append(p, proj)
}
return p, nil
}
func main() {
flagAddr := flag.String("addr", ":2018", "")
flagData := flag.String("data", "data", "")
flagProd := flag.Bool("prod", false, "")
flag.Parse()
if _, err := os.Stat(*flagData); err != nil {
if err := os.MkdirAll(*flagData, os.ModePerm); err != nil {
panic(err)
}
}
root, err := rootPath()
if err != nil {
panic(err)
}
pub := filepath.Join(root, "pub")
c := pork.Content(pork.NewConfig(pork.None), http.Dir(pub))
if *flagProd {
t, err := ioutil.TempDir(os.TempDir(), "grund-")
if err != nil {
panic(err)
}
if _, err := c.Productionize(http.Dir(t)); err != nil {
panic(err)
}
return
}
r := pork.NewRouter(
func(status int, r *http.Request) {
log.Printf("%d - %s", status, r.URL.Path)
}, nil, nil)
r.Handle("/", &handler{
c: c,
t: filepath.Join(root, "index.tpl.html"),
d: pub,
})
setup(*flagData, r)
if err := http.ListenAndServe(*flagAddr, r); err != nil {
panic(err)
}
}