This repository has been archived by the owner on Sep 24, 2018. It is now read-only.
/
handler.go
112 lines (96 loc) · 2.86 KB
/
handler.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
package main
import (
"fmt"
"log"
"net/http"
)
func notfound(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not found", http.StatusNotFound)
}
func render(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
if err != nil {
log.Printf("Could not get token: %v", err)
http.Error(w, "Could not authorize", http.StatusUnauthorized)
return
}
s := &Site{
Token: token.Value,
}
s.Build()
address, err := publish(token.Value)
if err != nil {
log.Printf("Failed to publish public folder: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
fmt.Fprint(w, address)
}
func setup(w http.ResponseWriter, r *http.Request) {
token, err := r.Cookie("token")
if err != nil {
log.Printf("Could not get token: %v", err)
http.Error(w, "Could not authorize", http.StatusUnauthorized)
return
}
_, err = getFileList("/blau.io", token.Value)
if err == nil {
err = recursiveDelete("/blau.io", token.Value)
if err != nil {
log.Printf("Could not delete folders: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
err = mkdir("/blau.io", token.Value)
if err != nil {
log.Printf("Could not create folder blau.io: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = mkdir("/blau.io/configuration", token.Value)
if err != nil {
log.Printf("Could not create folder configuration: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = mkdir("/blau.io/content", token.Value)
if err != nil {
log.Printf("Could not create folder content: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = mkdir("/blau.io/PUBLIC", token.Value)
if err != nil {
log.Printf("Could not create folder PUBLIC: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = upload("html/index.html", "/blau.io/configuration/index.html",
token.Value)
if err != nil {
log.Printf("Could not upload index.html: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = upload("html/main.css", "/blau.io/configuration/main.css",
token.Value)
if err != nil {
log.Printf("Could not upload index.html: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = upload("html/main.css", "/blau.io/PUBLIC/main.css",
token.Value)
if err != nil {
log.Printf("Could not upload index.html: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = upload("example/index.md", "/blau.io/content/index.md", token.Value)
if err != nil {
log.Printf("Could not upload hello.md: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}