forked from printfcoder/stack-rpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
145 lines (122 loc) · 3.04 KB
/
proxy.go
File metadata and controls
145 lines (122 loc) · 3.04 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
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/util/log"
"github.com/micro/go-micro/web"
)
// exampleCall 方法负责处理/example/call路由
func exampleCall(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// 获取传入参数值
name := r.Form.Get("name")
if len(name) == 0 {
http.Error(
w,
errors.BadRequest("go.micro.api.example", "no content").Error(),
400,
)
return
}
// 序列化响应参数
b, _ := json.Marshal(map[string]interface{}{
"message": "exampleCall 收到了你的消息," + name,
})
w.Write(b)
}
// exampleFooBar 方法负责处理 /example/foo/bar 路由
func exampleFooBar(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(
w,
errors.BadRequest("go.micro.api.example", "require post").Error(),
400,
)
return
}
if len(r.Header.Get("Content-Type")) == 0 {
http.Error(
w,
errors.BadRequest("go.micro.api.example", "need content-type").Error(),
400,
)
return
}
if r.Header.Get("Content-Type") != "application/json" {
http.Error(
w,
errors.BadRequest("go.micro.api.example", "expect application/json").Error(),
400,
)
return
}
// 获取传入参数值
bodyBytes, _ := ioutil.ReadAll(r.Body)
data := struct {
Name string `json:"name"`
}{}
json.Unmarshal(bodyBytes, &data)
// 序列化响应参数
b, _ := json.Marshal(map[string]interface{}{
"message": "exampleFooBar 收到了你的消息," + data.Name,
})
w.Write(b)
}
// uploadFile 方法负责处理/example/foo/upload上传文件的路由
func uploadFile(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.New("foo").Parse(`<html>
<head>
<title>Upload file</title>
</head>
<body>
<form enctype="multipart/form-data" action="http://127.0.0.1:8080/example/foo/upload" method="post">
<input type="file" name="uploadfile" />
<br />
保存目录: <input type="text" name="path" /> 如 /Users/me/Downloads/test/
<br />
<input type="submit" name='上传' value="upload" />
</form>
</body>
</html>`)
t.Execute(w, nil)
return
}
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fmt.Fprintf(w, "%v", handler.Header)
path := r.PostForm.Get("path")
f, err := os.OpenFile(path+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
}
func main() {
// 我们为了方便就直接使用go-web包,因为API需要从注册中心获取服务信息,而go-web包已经有注册服务的能力
service := web.NewService(
web.Name("go.micro.api.example"),
)
service.HandleFunc("/example/call", exampleCall)
service.HandleFunc("/example/foo/bar", exampleFooBar)
service.HandleFunc("/example/foo/upload", uploadFile)
if err := service.Init(); err != nil {
log.Fatal(err)
}
if err := service.Run(); err != nil {
log.Fatal(err)
}
}