-
Notifications
You must be signed in to change notification settings - Fork 20
/
http_index.go
123 lines (106 loc) · 2.8 KB
/
http_index.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
package main
import (
"encoding/json"
"fmt"
"github.com/espebra/filebin2/ds"
"io"
"net/http"
"time"
)
func (h *HTTP) Index(w http.ResponseWriter, r *http.Request) {
type Data struct {
ds.Common
Bin ds.Bin `json:"bin"`
}
var data Data
data.Page = "front"
bin := &ds.Bin{}
bin.Expiration = time.Now().UTC().Add(h.expirationDuration)
err := h.dao.Bin().Insert(bin)
if err != nil {
fmt.Printf("Unable to insert new bin: %s\n", err.Error())
http.Error(w, "Errno 301", http.StatusInternalServerError)
return
}
data.Bin = *bin
if err := h.templates.ExecuteTemplate(w, "index", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 302", http.StatusInternalServerError)
return
}
}
func (h *HTTP) About(w http.ResponseWriter, r *http.Request) {
//w.Header().Set("Cache-Control", "max-age=900")
type Data struct {
ds.Common
Bin ds.Bin `json:"bin"`
}
var data Data
data.Page = "about"
if err := h.templates.ExecuteTemplate(w, "about", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 302", http.StatusInternalServerError)
return
}
}
func (h *HTTP) API(w http.ResponseWriter, r *http.Request) {
type Data struct {
ds.Common
Bin ds.Bin `json:"bin"`
}
var data Data
data.Page = "api"
//w.Header().Set("Cache-Control", "max-age=900")
if err := h.templates.ExecuteTemplate(w, "api", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 302", http.StatusInternalServerError)
return
}
}
func (h *HTTP) APISpec(w http.ResponseWriter, r *http.Request) {
type Data struct {
ds.Common
Bin ds.Bin `json:"bin"`
}
var data Data
data.Page = "api"
w.Header().Set("Content-Type", "application/json")
//w.Header().Set("Cache-Control", "max-age=900")
if err := h.templates.ExecuteTemplate(w, "apispec", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 302", http.StatusInternalServerError)
return
}
}
func (h *HTTP) FilebinStatus(w http.ResponseWriter, r *http.Request) {
type Data struct {
AppStatus bool `json:"app-status"`
DbStatus bool `json:"db-status"`
S3Status bool `json:"s3-status"`
}
var data Data
code := 200
data.AppStatus = true
if h.dao.Status() {
data.DbStatus = true
} else {
data.DbStatus = false
code = 503
}
if h.s3.Status() {
data.S3Status = true
} else {
data.S3Status = false
code = 503
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "max-age=1")
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Printf("Failed to parse json: %s\n", err.Error())
http.Error(w, "Errno 201", http.StatusInternalServerError)
return
}
w.WriteHeader(code)
io.WriteString(w, string(out))
}