-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
173 lines (164 loc) · 4.75 KB
/
handlers.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"go-file-cache-server.example.com/controllers"
"go-file-cache-server.example.com/db"
"go-file-cache-server.example.com/error"
"go-file-cache-server.example.com/models"
)
func healthCheckHandler(respWriter http.ResponseWriter, req *http.Request) {
respWriter.WriteHeader(http.StatusOK)
respWriter.Write([]byte("OK"))
}
func cacheFile(respWriter http.ResponseWriter, req *http.Request) {
bs, err := ioutil.ReadAll(req.Body)
if err != nil {
respBody := error.GetHTTPError("could not read request body", err)
bs, err := json.Marshal(respBody)
if err != nil {
log.Println(err)
respWriter.WriteHeader(http.StatusInternalServerError)
return
}
respWriter.Write(bs)
}
// make new CacheFileRequestBody
var cacheFileRequest models.CacheFileRequestBody
json.Unmarshal(bs, &cacheFileRequest)
cacheFileRequest.ID = models.GetID(cacheFileRequest.FileURL)
if cacheFileRequest.AuthType == "" {
cacheFileRequest.AuthType = controllers.NO_AUTH
}
// make new cacheFile
cacheFile, err := db.NewCacheFile(cacheFileRequest)
var cacheFileRespBody models.CacheFileResponseBody
var respStatus int
if err != nil {
cacheFileRespBody = models.CacheFileResponseBody{
Message: err.Error(),
}
respStatus = http.StatusBadRequest
} else {
db.AddCacheFileToDB(cacheFile)
go controllers.CacheFile(cacheFile)
cacheFileRespBody = models.CacheFileResponseBody{
ID: cacheFile.ID,
Status: cacheFile.Status,
Message: "Initialised successfully",
}
respStatus = http.StatusOK
}
bs, _ = json.Marshal(cacheFileRespBody)
respWriter.Header().Add("Content-Type", "application/json")
respWriter.WriteHeader(respStatus)
respWriter.Write(bs)
}
func getCachedFileStatus(respWriter http.ResponseWriter, req *http.Request) {
fileURL := req.URL.Query().Get("fileURL")
id := req.URL.Query().Get("ID")
type responseBody struct {
Message string `json:"message"`
Status string `json:"status"`
}
var respBody responseBody
var respStatus int
if fileURL == "" && id == "" {
respBody = responseBody{
Message: "/cache-file-status requires fileURL or ID as query parameter",
}
respStatus = http.StatusBadRequest
} else if len(id) > 0 {
cacheFile, ok := db.GetCacheFileByID(id)
if !ok {
respBody = responseBody{
Message: "could not find file by id " + id,
}
respStatus = http.StatusBadRequest
} else {
respBody = responseBody{
Status: string(models.GetCacheFileStatus(cacheFile.Status)),
}
respStatus = http.StatusOK
}
} else {
if cacheFile, ok := db.GetCacheFileByURL(fileURL); !ok {
respBody = responseBody{
Message: "could not find file by fileURL " + fileURL,
}
respStatus = http.StatusBadRequest
} else {
respBody = responseBody{
Status: string(models.GetCacheFileStatus(cacheFile.Status)),
}
respStatus = http.StatusOK
}
}
bs, _ := json.Marshal(respBody)
respWriter.Header().Add("Content-Type", "application/json")
respWriter.WriteHeader(respStatus)
respWriter.Write(bs)
}
func getFile(respWriter http.ResponseWriter, req *http.Request) {
fileURL := req.URL.Query().Get("fileURL")
id := req.URL.Query().Get("ID")
type responseBody struct {
Message string `json:"message"`
Status string `json:"status"`
}
var respBody responseBody
var respStatus int
var filePath string = ""
var fileName string = ""
if fileURL == "" && id == "" {
respBody = responseBody{
Message: "/cache-file-status requires fileURL or ID as query parameter",
}
respStatus = http.StatusBadRequest
} else if len(id) > 0 {
cacheFile, ok := db.GetCacheFileByID(id)
if !ok {
respBody = responseBody{
Message: "could not find file by id " + id,
}
respStatus = http.StatusBadRequest
} else {
filePath = cacheFile.LocalPath
fileName = cacheFile.FileName
respStatus = http.StatusOK
}
} else {
if cacheFile, ok := db.GetCacheFileByURL(fileURL); !ok {
respBody = responseBody{
Message: "could not find file by fileURL " + fileURL,
}
respStatus = http.StatusBadRequest
} else {
filePath = cacheFile.LocalPath
fileName = cacheFile.FileName
respStatus = http.StatusOK
}
}
if len(filePath) == 0 {
bs, _ := json.Marshal(respBody)
respWriter.Header().Add("Content-Type", "application/json")
respWriter.WriteHeader(respStatus)
respWriter.Write(bs)
} else {
respWriter.Header().Add("Content-Type", "application/json")
respWriter.Header().Set("Content-Disposition", "attachment; filename="+fileName)
respWriter.Header().Set("content-type", "application/octet-stream")
respWriter.WriteHeader(respStatus)
file, err := os.Open(filePath)
if err != nil {
log.Fatal("error in opening file: ", err)
}
io.Copy(respWriter, file)
}
}
func invalidateCache(respWriter http.ResponseWriter, req *http.Request) {
}