-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
225 lines (188 loc) · 5.4 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"database/sql"
"fmt"
"io"
"log"
"mime"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/jmoiron/sqlx"
_ "modernc.org/sqlite"
)
type Website struct {
ID int `db:"id"`
Hostname string `db:"hostname"`
UpdateTime time.Time `db:"update_time"`
}
type File struct {
ID int `db:"id"`
WebsiteID int `db:"website_id"`
Path string `db:"path"`
UpdateTime time.Time `db:"update_time"`
Blob []byte `db:"blob"`
}
var TABLES string = `
CREATE TABLE IF NOT EXISTS website (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hostname VARCHAR NOT NULL,
update_time TIMESTAMP NOT NULL,
UNIQUE(hostname)
);
CREATE TABLE IF NOT EXISTS file (
id INTEGER PRIMARY KEY AUTOINCREMENT,
website_id INTEGER NOT NULL,
path VARCHAR NOT NULL,
update_time TIMESTAMP NOT NULL,
blob BLOB NOT NULL,
UNIQUE(website_id, path)
FOREIGN KEY(website_id) REFERENCES website(id)
);
`
var db *sqlx.DB
func main() {
fmt.Println("serving your pages")
var err error
db, err = sqlx.Connect("sqlite", "your-pages.db")
if err != nil {
fmt.Printf("could not open database: %s\n", err)
}
defer db.Close()
// setup database
db.MustExec(TABLES)
mux := http.NewServeMux()
mux.HandleFunc("/upload", UploadHandler)
mux.HandleFunc("/", ServeHandler)
log.Fatal(http.ListenAndServe(":4444", mux))
}
func UploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "only HTTP posts requests accepted", http.StatusMethodNotAllowed)
return
}
// // TODO: use a multipart reader
// mpr, err := r.MultipartReader()
// for {
// part, err := mpr.NextPart()
// part.FileName()
// }
r.ParseMultipartForm(32 << 20)
sites := []string{}
for site := range r.MultipartForm.File {
sites = append(sites, site)
}
file, header, err := r.FormFile(sites[0])
if err != nil {
http.Error(w, fmt.Sprint("could not read file: ", err), http.StatusBadRequest)
}
tx := db.MustBegin()
// calling rollback after commit is safe
defer tx.Rollback()
website := &Website{
Hostname: sites[0],
UpdateTime: time.Now(),
}
res, err := tx.NamedQuery(`
INSERT INTO website (hostname, update_time)
VALUES (:hostname, :update_time)
ON CONFLICT(hostname) DO UPDATE SET update_time = :update_time
RETURNING id;
`,
website)
if err != nil {
http.Error(w, fmt.Sprint("could not insert website: ", err), http.StatusBadRequest)
return
}
if res.Next() {
res.Scan(&website.ID)
}
mimeType := mime.TypeByExtension(filepath.Ext(header.Filename))
if mimeType != "application/gzip" {
http.Error(w, "only gzip files accepted", http.StatusBadRequest)
return
}
gzipped, err := gzip.NewReader(file)
if err != nil {
http.Error(w, fmt.Sprint("could not create GZIP reader: ", err), http.StatusInternalServerError)
}
gzipped.Close()
tarred := tar.NewReader(gzipped)
for {
header, err := tarred.Next()
if err == io.EOF {
break
}
if err != nil {
http.Error(w, fmt.Sprint("could not read tar: ", err), http.StatusInternalServerError)
}
if header.Typeflag == tar.TypeDir {
continue
}
// TODO: stream file into database
bytes, err := io.ReadAll(tarred)
if err != nil {
http.Error(w, fmt.Sprint("could not read tar: ", err), http.StatusInternalServerError)
}
path := ""
if filepath.Base(header.Name) == "index.html" {
path = filepath.Dir(header.Name)
if path == "." {
path = ""
} else {
path += "/"
}
} else {
path = header.Name
}
file := &File{
WebsiteID: website.ID,
Path: "/" + path,
Blob: bytes,
UpdateTime: time.Now(),
}
_, err = tx.NamedExec(`
INSERT INTO file (website_id, path, blob, update_time)
VALUES (:website_id, :path, :blob, :update_time)
ON CONFLICT(website_id, path) DO UPDATE SET blob = :blob, update_time = :update_time;
`, file)
if err != nil {
http.Error(w, fmt.Sprint("could not insert file: ", err), http.StatusInternalServerError)
return
}
}
// TODO: clear out files that are no longer in the tar
// could be done by checking if the update time if before/after the request started
err = tx.Commit()
if err != nil {
http.Error(w, fmt.Sprint("could not commit transaction: ", err), http.StatusInternalServerError)
}
fmt.Fprintf(w, "uploaded site %s", sites[0])
}
func ServeHandler(w http.ResponseWriter, r *http.Request) {
host := strings.Split(r.Host, ":")[0]
website := &Website{}
db.Get(website, "SELECT * FROM website WHERE hostname = $1;", host)
if website.ID == 0 {
http.Error(w, "website not found", http.StatusNotFound)
return
}
file := &File{}
// bit of magic to match paths with or without trailing slashes
err := db.Get(file, `SELECT * FROM file WHERE website_id = $1 AND (path = $2 OR path = $2 || '/');`, website.ID, r.URL.Path)
if err == sql.ErrNoRows {
// serve a 404.html if it exists
err := db.Get(file, `SELECT * FROM file WHERE website_id = $1 AND path = $2;`, website.ID, "/404.html")
if err == sql.ErrNoRows {
http.Error(w, "file not found", http.StatusNotFound)
}
// FIXME: I think scoping should be fine here but the two err's give me heebie jeebies
} else if err != nil {
http.Error(w, fmt.Sprint("could not query database: ", err), http.StatusInternalServerError)
}
http.ServeContent(w, r, file.Path, website.UpdateTime, bytes.NewReader(file.Blob))
}