-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_server.go
137 lines (110 loc) · 3.02 KB
/
http_server.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
package main
import (
"archive/zip"
"fmt"
"log"
"net/http"
"strings"
"time"
)
const maxUploadingFileSize = 128 << 20 // 128 Mb
const multipartFileName = "file"
type httpServer struct {
s *storage
baseURL string
}
func (s *httpServer) getRouter() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/upload", s.uploadHandler)
mux.Handle("/", http.FileServer(http.Dir(s.s.basePath)))
return mux
}
func (s *httpServer) start(bindAddr string) error {
srv := http.Server{
Addr: bindAddr,
Handler: s.getRouter(),
}
log.Printf("http-server is staring on %s", bindAddr)
return srv.ListenAndServe()
}
func (s *httpServer) uploadHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
s.writeError(res, http.StatusNotFound, "wrong method")
return
}
// validating file size
req.Body = http.MaxBytesReader(res, req.Body, maxUploadingFileSize)
err := req.ParseMultipartForm(maxUploadingFileSize)
if err != nil {
s.writeError(res, http.StatusBadRequest, err.Error())
return
}
group := strings.TrimSpace(req.FormValue("group"))
if group == "" {
s.writeError(res, http.StatusBadRequest, "'group' param should not be empty")
return
}
project := strings.TrimSpace(req.FormValue("project"))
if project == "" {
s.writeError(res, http.StatusBadRequest, "'project' param should not be empty")
return
}
versionKey := strings.TrimSpace(req.FormValue("version"))
if versionKey == "" {
s.writeError(res, http.StatusBadRequest, "'version' param should not be empty")
return
}
versionTS := time.Now()
if ts := strings.TrimSpace(req.FormValue("ts")); ts != "" {
versionTS, err = time.Parse(time.RFC3339, ts)
if err != nil {
return
}
}
sk := newStorageKey(group, project, newStorageKeyVersion(versionKey, versionTS))
log.Printf("uploading file (size: %d) to %s", req.ContentLength, sk.getPath())
// obtaining file data
file, _, err := req.FormFile(multipartFileName)
if err != nil {
s.writeError(res, http.StatusBadRequest, err.Error())
return
}
defer file.Close()
// TODO: check file (non empty)
r, err := zip.NewReader(file, req.ContentLength)
if err != nil {
s.writeError(res, http.StatusInternalServerError, err.Error())
return
}
err = s.s.putResults(r, sk)
if err != nil {
s.writeError(res, http.StatusInternalServerError, err.Error())
return
}
err = s.s.copyHistory(sk)
if err != nil {
s.writeError(res, http.StatusInternalServerError, err.Error())
return
}
err = s.s.generateReport(sk)
if err != nil {
s.writeError(res, http.StatusInternalServerError, err.Error())
return
}
err = s.s.createLastVersionSymlink(sk)
if err != nil {
s.writeError(res, http.StatusInternalServerError, err.Error())
return
}
fmt.Fprintf(res, "%s/%s/report/\n", s.baseURL, sk.getPath())
}
func (s *httpServer) writeError(res http.ResponseWriter, code int, msg string) {
res.WriteHeader(code)
_, err := res.Write([]byte(msg))
if err != nil {
log.Printf("ERROR: %s", err)
}
if code >= 500 {
log.Printf("ERROR: code=%d %s", code, msg)
}
}