-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_step_1.go
70 lines (64 loc) · 1.6 KB
/
server_step_1.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
package main
import (
"net/http"
"io"
"bytes"
"encoding/json"
"log"
)
func HelloPath(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("This is an example server.\n"))
// fmt.Fprintf(w, "This is an example server.\n")
// io.WriteString(w, "This is an example server.\n")
}
func UploadPath(w http.ResponseWriter, req *http.Request) {
var Buf bytes.Buffer
// in your case file would be fileupload
token := req.Header.Get("Authorization")
file, header, err := req.FormFile("image")
w.Header().Set("Content-Type", "text/plain")
if err != nil {
response,_:=json.Marshal(HttpResponse{
Status:"fail",
Data:"",
Error:err.Error(),
})
w.Write([]byte(response))
}else {
defer file.Close()
io.Copy(&Buf, file)
data := Buf.Bytes()
client :=InitHttpClientNoKeepAlive()
upload:= UploadInfo{
fullPath:"/"+GetTimeStamp()+"_"+header.Filename,
token:token,
data:data,
}
responseInfo :=upload.UploadFile(client)
if(responseInfo.err!=nil) {
response,_:=json.Marshal(HttpResponse{
Status:"fail",
Data:"",
Error:responseInfo.err.Error(),
})
w.Write([]byte(response))
}else{
response,_:=json.Marshal(HttpResponse{
Status:"success",
Data:responseInfo.response,
Error:"",
})
w.Write([]byte(response))
}
}
}
func RunServerStep1() {
http.HandleFunc("/hello", HelloPath)
http.HandleFunc("/upload/dropbox", UploadPath)
err := http.ListenAndServeTLS(":8443", "ssl/server.crt", "ssl/server.key", nil)
//err := http.ListenAndServe(":8443", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}