-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
58 lines (47 loc) · 1.24 KB
/
index.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
package main
import (
"fmt"
"bytes"
"io/ioutil"
"net/http"
"encoding/base64"
"gopkg.in/yaml.v2"
)
type Config struct {
ServerAddress string `yaml:"server_address"`
Port int `yaml:"port"`
}
func main() {
configData, err := ioutil.ReadFile("config.yml")
if err != nil {
fmt.Println("读取配置文件失败:", err)
return
}
var config Config
err = yaml.Unmarshal(configData, &config)
if err != nil {
fmt.Println("解析配置文件失败:", err)
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
content, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "读取请求内容失败", http.StatusInternalServerError)
return
}
encodedContent := base64.StdEncoding.EncodeToString(content)
encodedBytes := []byte(encodedContent)
_, err = http.Post(config.ServerAddress, "text/plain", bytes.NewBuffer(encodedBytes))
if err != nil {
http.Error(w, "发送请求失败", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "已桥接请求")
})
addr := fmt.Sprintf(":%d", config.Port)
err = http.ListenAndServe(addr, nil)
if err != nil {
fmt.Println("启动HTTP服务失败:", err)
return
}
}