-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.go
97 lines (81 loc) · 2.01 KB
/
main.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
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os/exec"
"runtime"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowCredentials = true
config.AllowHeaders = []string{"Authorization"}
r.Use(cors.New(config))
debug := flag.Bool("debug", false, "用于本地Web开发")
flag.Parse()
if *debug {
r.Any("/feishu-backup/*proxyPath", proxydev)
} else {
r.Static("/feishu-backup", "./dist")
}
r.Any("/api/feishu/*proxyPath", proxy)
r.GET("/ping", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"code": 0})
})
Open(`http://127.0.0.1:18900/feishu-backup`)
r.Run("0.0.0.0:18900")
}
func proxy(c *gin.Context) {
remote, err := url.Parse("https://open.feishu.cn")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = "/open-apis" + c.Param("proxyPath")
}
proxy.ServeHTTP(c.Writer, c.Request)
}
func proxydev(c *gin.Context) {
remote, err := url.Parse("http://localhost:3800")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = "/feishu-backup" + c.Param("proxyPath")
}
proxy.ServeHTTP(c.Writer, c.Request)
}
var commands = map[string]string{
"windows": "start",
"darwin": "open",
"linux": "xdg-open",
}
func Open(uri string) error {
run, ok := commands[runtime.GOOS]
if !ok {
return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
}
if runtime.GOOS == "windows" {
cmd := exec.Command("cmd", "/C", "start", uri)
return cmd.Start()
} else {
cmd := exec.Command(run, uri)
return cmd.Start()
}
}