-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (131 loc) · 3.42 KB
/
main.go
File metadata and controls
156 lines (131 loc) · 3.42 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
var data = map[string]map[string]map[string]string{}
const maxUploadSize = 20 * 1024 * 1024 // 20 MB
const path = "./shapes.json"
const uploadPath = "./cache"
// load json into data
func init() {
jsonFile, err := os.Open(path)
if err != nil {
logrus.Fatal(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &data)
}
// type Result map[string]map[string]map[string]string
func check(e error) {
if e != nil {
panic(e)
}
}
// show all current shapes
func all(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(data)
return
}
// get a shape or add a shape
func fileServe(w http.ResponseWriter, r *http.Request) {
// params should be in the format /:region/:version/:shape
params := strings.Split(r.URL.Path, "/")
fmt.Println(params)
if len(params) != 4 {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
region, version, shape := params[1], params[2], params[3]
// only allowed 2 methods GET and POST
switch r.Method {
case "GET":
// if the shape exists - serve
if loc, ok := data[region][version][shape]; ok {
http.ServeFile(w, r, loc)
return
}
http.Error(w, "404 not found.", http.StatusNotFound)
return
case "POST":
newpath := filepath.Join(uploadPath, region, version)
if _, err := os.Stat(newpath); os.IsNotExist(err) {
os.MkdirAll(newpath, os.ModePerm)
}
if _, ok := data[region]; !ok {
data[region] = make(map[string]map[string]string)
}
if _, ok := data[region][version]; !ok {
data[region][version] = make(map[string]string)
}
// if the file already exists return 400
if path, ok := data[region][version][shape]; ok {
fmt.Println(path)
http.Error(w, "FILE_ALREADY_EXISTS", http.StatusBadRequest)
return
}
// check file size - limit 20mb
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
fmt.Println(err)
http.Error(w, "FILE_TOO_BIG", http.StatusBadRequest)
return
}
file, header, err := r.FormFile("uploadFile")
filename := header.Filename
if err != nil {
http.Error(w, "INVALID_FILE", http.StatusBadRequest)
return
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, "INVALID_FILE", http.StatusBadRequest)
return
}
// put file in cache
newPath := filepath.Join(newpath, filename)
newFile, err := os.Create(newPath)
if err != nil {
fmt.Println(err)
http.Error(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
}
defer newFile.Close()
if _, err := newFile.Write(fileBytes); err != nil {
http.Error(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
}
// assign path to object
data[region][version][shape] = newPath
w.Write([]byte("SUCCESS"))
// serialize the new data file
bytes, err := json.Marshal(data)
check(err)
f, err := os.Create(path)
defer f.Close()
check(err)
n, err := f.Write(bytes)
fmt.Printf("wrote %d bytes", n)
check(err)
f.Sync()
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}
func main() {
http.HandleFunc("/all", all)
http.HandleFunc("/", fileServe)
fmt.Printf("Starting server for testing HTTP POST...\n")
if err := http.ListenAndServe(":9004", nil); err != nil {
log.Fatal(err)
}
}