From fb06995b2666aca4e35c9cf3457c70d0cc0ab09d Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 11 Jul 2023 13:07:44 +0800 Subject: [PATCH] q! --- goctl_template/api/etc.tpl | 4 +- goctl_template_backend/api/etc.tpl | 4 +- proxyserver/get_zero_Info.go | 125 ++++++++++++++++ proxyserver/main.go | 136 ++++++++++++++++++ proxyserver/main_test.go | 18 +++ server/backend/etc/backend.yaml | 4 +- server/canteen/etc/canteen.yaml | 4 +- server/data-transfer/etc/data-transfer.yaml | 4 +- server/home-user-auth/etc/home-user-auth.yaml | 2 +- server/inventory/etc/inventory.yaml | 4 +- server/map-library/etc/map-library.yaml | 4 +- server/orders/etc/orders.yaml | 4 +- server/product-model/etc/product-model.yaml | 4 +- .../etc/product-template.yaml | 4 +- server/product/etc/product.yaml | 4 +- .../etc/shopping-cart-confirmation.yaml | 4 +- server/upload/etc/upload.yaml | 4 +- .../webset/etc/{wetset.yaml => webset.yaml} | 8 +- server/webset/internal/handler/routes.go | 2 +- ...tinghandler.go => websetsettinghandler.go} | 4 +- ...tsettinglogic.go => websetsettinglogic.go} | 2 +- server/webset/internal/types/types.go | 16 ++- server/webset/test/basic.go | 2 +- server/webset/test/wetset_test.go | 2 +- server/webset/{wetset.go => webset.go} | 2 +- server_api/webset.api | 4 +- 26 files changed, 334 insertions(+), 41 deletions(-) create mode 100644 proxyserver/get_zero_Info.go create mode 100644 proxyserver/main.go create mode 100644 proxyserver/main_test.go rename server/webset/etc/{wetset.yaml => webset.yaml} (62%) rename server/webset/internal/handler/{wetsetsettinghandler.go => websetsettinghandler.go} (95%) rename server/webset/internal/logic/{wetsetsettinglogic.go => websetsettinglogic.go} (95%) rename server/webset/{wetset.go => webset.go} (89%) diff --git a/goctl_template/api/etc.tpl b/goctl_template/api/etc.tpl index d2c383f3..7daa231f 100644 --- a/goctl_template/api/etc.tpl +++ b/goctl_template/api/etc.tpl @@ -4,5 +4,5 @@ Port: {{.port}} SourceMysql: "" Auth: AccessSecret: fusen2023 - AccessExpire: 604800 - RefreshAfter: 345600 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/goctl_template_backend/api/etc.tpl b/goctl_template_backend/api/etc.tpl index d2c383f3..7daa231f 100644 --- a/goctl_template_backend/api/etc.tpl +++ b/goctl_template_backend/api/etc.tpl @@ -4,5 +4,5 @@ Port: {{.port}} SourceMysql: "" Auth: AccessSecret: fusen2023 - AccessExpire: 604800 - RefreshAfter: 345600 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/proxyserver/get_zero_Info.go b/proxyserver/get_zero_Info.go new file mode 100644 index 00000000..543554fa --- /dev/null +++ b/proxyserver/get_zero_Info.go @@ -0,0 +1,125 @@ +package main + +import ( + "io/ioutil" + "log" + "os" + "path/filepath" + "strings" + + "gopkg.in/yaml.v2" +) + +type Config struct { + Host string `yaml:"Host"` + Port int `yaml:"Port"` +} + +type Result struct { + FolderName string + Host string + Port int + PrefixRoute map[string]bool +} + +func GetZeroInfo(rootDir string) (results []*Result) { + entries, err := ioutil.ReadDir(rootDir) + if err != nil { + log.Fatal(err) + } + + for _, entry := range entries { + if entry.IsDir() { + + result, err := findFoldersAndExtractInfo(rootDir, entry) + if err != nil { + log.Fatal(err) + } + + results = append(results, result) + + } + } + + return +} + +func findFoldersAndExtractInfo(rootDir string, entry os.FileInfo) (*Result, error) { + var result *Result + + folderName := entry.Name() + path := filepath.Join(rootDir, folderName) + + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() { + return nil + } + + relPath, err := filepath.Rel(path, path) + if err != nil { + return err + } + + if strings.Contains(relPath, string(os.PathSeparator)) { + return filepath.SkipDir + } + + configPath := filepath.Join(path, "etc", folderName+".yaml") + routesPath := filepath.Join(path, "internal", "handler", "routes.go") + + configContent, err := ioutil.ReadFile(configPath) + if err != nil { + return err + } + + var config Config + err = yaml.Unmarshal(configContent, &config) + if err != nil { + return err + } + + routesContent, err := ioutil.ReadFile(routesPath) + if err != nil { + return err + } + + PrefixRoute := extractPrefixRouteValues(string(routesContent)) + + result = &Result{ + FolderName: folderName, + Host: config.Host, + Port: config.Port, + PrefixRoute: PrefixRoute, + } + + return filepath.SkipDir + }) + + if err != nil { + return nil, err + } + + return result, nil +} + +func extractPrefixRouteValues(content string) map[string]bool { + lines := strings.Split(content, "\n") + var prefixPath map[string]bool = make(map[string]bool) + + for _, line := range lines { + if strings.Contains(line, "Path:") { + path := strings.TrimSpace(strings.TrimPrefix(line, "Path:")) + path1 := strings.Split(strings.Trim(path, `"`), "/")[1] + path1 = "/" + path1 + if _, ok := prefixPath[path1]; !ok { + prefixPath[path1] = true + } + } + } + + return prefixPath +} diff --git a/proxyserver/main.go b/proxyserver/main.go new file mode 100644 index 00000000..dcca254d --- /dev/null +++ b/proxyserver/main.go @@ -0,0 +1,136 @@ +package main + +import ( + "fmt" + "io" + "log" + "net" + "net/http" + "net/url" + "time" +) + +var Backends []*Backend + +func main() { + rootDir := "../server" // Change this to your root directory + vueBuild := "/home/eson/workspace/fusenpack-vue-created" + + mux := http.NewServeMux() + + // Define the static file server that serves the Vue dist folder + fs := http.FileServer(http.Dir(vueBuild)) + + // Define a handler function for the Vue static assets + mux.Handle("/", http.StripPrefix("/", fs)) + + results := GetZeroInfo(rootDir) + for _, result := range results { + fmt.Printf("FolderName: %s, Host: %s, Port: %d, PrefixRoute: %v\n", result.FolderName, result.Host, result.Port, result.PrefixRoute) + var routes []string + for k := range result.PrefixRoute { + routes = append(routes, k) + } + + Backends = append(Backends, + NewBackend(mux, + fmt.Sprintf("http://%s:%d", result.Host, result.Port), + routes...)) + } + + ServerAddress := ":9900" + log.Println("listen on ", ServerAddress) + log.Fatal(http.ListenAndServe(ServerAddress, mux)) +} + +type Backend struct { + HttpAddress string + Client *http.Client + Handler http.HandlerFunc +} + +func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Backend { + + for i, muxPath := range muxPaths { + if muxPath[len(muxPath)-1] != '/' { + muxPath = muxPath + "/" + muxPaths[i] = muxPath + } + } + + client := &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + }).DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + }, + } + + backend := &Backend{ + HttpAddress: httpAddress, + Client: client, + } + + handleRequest := func(w http.ResponseWriter, r *http.Request) { + targetURL, err := url.Parse(httpAddress + r.URL.Path) + if err != nil { + http.Error(w, "Error parsing target URL", http.StatusInternalServerError) + return + } + + // 解析目标URL时已经包含了查询参数 + targetURL.RawQuery = r.URL.RawQuery + + proxyReq, err := http.NewRequest(r.Method, targetURL.String(), r.Body) + if err != nil { + http.Error(w, "Error creating proxy request", http.StatusInternalServerError) + return + } + + // 复制原始请求的 Header + for key, values := range r.Header { + for _, value := range values { + proxyReq.Header.Add(key, value) + } + } + + // 设置 Content-Length 和 Content-Type + proxyReq.ContentLength = r.ContentLength + proxyReq.Header.Set("Content-Type", r.Header.Get("Content-Type")) + + resp, err := backend.Client.Do(proxyReq) + if err != nil { + http.Error(w, "Error sending proxy request", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + // 复制目标服务器的响应 Header + for key, values := range resp.Header { + for _, value := range values { + w.Header().Add(key, value) + } + } + + // 转发目标服务器的响应状态码和主体 + w.WriteHeader(resp.StatusCode) + _, err = io.Copy(w, resp.Body) + if err != nil { + http.Error(w, "Error copying proxy response", http.StatusInternalServerError) + return + } + + } + for _, muxPath := range muxPaths { + mux.HandleFunc(muxPath, handleRequest) + } + + return backend +} diff --git a/proxyserver/main_test.go b/proxyserver/main_test.go new file mode 100644 index 00000000..12e84202 --- /dev/null +++ b/proxyserver/main_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "testing" +) + +func TestProxy(t *testing.T) { + main() +} + +func TestCapture(t *testing.T) { + rootDir := "../server" // Change this to your root directory + results := GetZeroInfo(rootDir) + for _, result := range results { + fmt.Printf("FolderName: %s, Host: %s, Port: %d, PrefixRoute: %v\n", result.FolderName, result.Host, result.Port, result.PrefixRoute) + } +} diff --git a/server/backend/etc/backend.yaml b/server/backend/etc/backend.yaml index f21404d1..18f08394 100644 --- a/server/backend/etc/backend.yaml +++ b/server/backend/etc/backend.yaml @@ -4,5 +4,5 @@ Port: 9901 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen_backend_2023 - AccessExpire: 604800 - RefreshAfter: 345600 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/canteen/etc/canteen.yaml b/server/canteen/etc/canteen.yaml index 46f400c5..4b363db8 100644 --- a/server/canteen/etc/canteen.yaml +++ b/server/canteen/etc/canteen.yaml @@ -4,5 +4,5 @@ Port: 9902 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 + AccessExpire: 2592000 + RefreshAfter: 1592000 diff --git a/server/data-transfer/etc/data-transfer.yaml b/server/data-transfer/etc/data-transfer.yaml index 250dac2d..4a3e767d 100644 --- a/server/data-transfer/etc/data-transfer.yaml +++ b/server/data-transfer/etc/data-transfer.yaml @@ -4,5 +4,5 @@ Port: 9903 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/home-user-auth/etc/home-user-auth.yaml b/server/home-user-auth/etc/home-user-auth.yaml index 39e02f61..6490fea2 100644 --- a/server/home-user-auth/etc/home-user-auth.yaml +++ b/server/home-user-auth/etc/home-user-auth.yaml @@ -6,7 +6,7 @@ SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 AccessExpire: 2592000 - RefreshAfter: 2592000 + RefreshAfter: 1592000 Stripe: SK: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y" diff --git a/server/inventory/etc/inventory.yaml b/server/inventory/etc/inventory.yaml index 9296a16b..a8195506 100644 --- a/server/inventory/etc/inventory.yaml +++ b/server/inventory/etc/inventory.yaml @@ -4,5 +4,5 @@ Port: 9905 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 604800 - RefreshAfter: 345600 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/map-library/etc/map-library.yaml b/server/map-library/etc/map-library.yaml index a4d07e04..0dc22627 100644 --- a/server/map-library/etc/map-library.yaml +++ b/server/map-library/etc/map-library.yaml @@ -4,5 +4,5 @@ Port: 9906 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/orders/etc/orders.yaml b/server/orders/etc/orders.yaml index 26aa6561..77573dcf 100644 --- a/server/orders/etc/orders.yaml +++ b/server/orders/etc/orders.yaml @@ -4,5 +4,5 @@ Port: 9907 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/product-model/etc/product-model.yaml b/server/product-model/etc/product-model.yaml index adce80ef..867437e2 100644 --- a/server/product-model/etc/product-model.yaml +++ b/server/product-model/etc/product-model.yaml @@ -4,5 +4,5 @@ Port: 9909 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/product-template/etc/product-template.yaml b/server/product-template/etc/product-template.yaml index 8991920d..287b4a33 100644 --- a/server/product-template/etc/product-template.yaml +++ b/server/product-template/etc/product-template.yaml @@ -4,5 +4,5 @@ Port: 9910 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/product/etc/product.yaml b/server/product/etc/product.yaml index 1a3ce1fe..658e5411 100644 --- a/server/product/etc/product.yaml +++ b/server/product/etc/product.yaml @@ -4,5 +4,5 @@ Port: 9908 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 60 - RefreshAfter: 60 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml b/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml index 26610567..fa40bb45 100644 --- a/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml +++ b/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml @@ -4,5 +4,5 @@ Port: 9911 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 - AccessExpire: 604800 - RefreshAfter: 345600 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index 6ccd1c84..8481514f 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -5,8 +5,8 @@ SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest" Env: "test" Auth: AccessSecret: fusen2023 - AccessExpire: 604800 - RefreshAfter: 345600 + AccessExpire: 2592000 + RefreshAfter: 1592000 AWS: S3: Credentials: diff --git a/server/webset/etc/wetset.yaml b/server/webset/etc/webset.yaml similarity index 62% rename from server/webset/etc/wetset.yaml rename to server/webset/etc/webset.yaml index 063e8f06..ea7f966f 100644 --- a/server/webset/etc/wetset.yaml +++ b/server/webset/etc/webset.yaml @@ -1,8 +1,8 @@ -Name: wetset +Name: webset Host: 0.0.0.0 -Port: 8888 +Port: 9914 SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest" Auth: AccessSecret: fusen2023 - AccessExpire: 604800 - RefreshAfter: 345600 \ No newline at end of file + AccessExpire: 2592000 + RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/webset/internal/handler/routes.go b/server/webset/internal/handler/routes.go index 700ccc93..d446b0f7 100644 --- a/server/webset/internal/handler/routes.go +++ b/server/webset/internal/handler/routes.go @@ -15,7 +15,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { { Method: http.MethodGet, Path: "/web-set/setting", - Handler: WetSetSettingHandler(serverCtx), + Handler: WebSetSettingHandler(serverCtx), }, }, ) diff --git a/server/webset/internal/handler/wetsetsettinghandler.go b/server/webset/internal/handler/websetsettinghandler.go similarity index 95% rename from server/webset/internal/handler/wetsetsettinghandler.go rename to server/webset/internal/handler/websetsettinghandler.go index 8c080435..651be166 100644 --- a/server/webset/internal/handler/wetsetsettinghandler.go +++ b/server/webset/internal/handler/websetsettinghandler.go @@ -15,7 +15,7 @@ import ( "fusenapi/server/webset/internal/types" ) -func WetSetSettingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func WebSetSettingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( @@ -65,7 +65,7 @@ func WetSetSettingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewWetSetSettingLogic(r.Context(), svcCtx) - resp := l.WetSetSetting(&req, userinfo) + resp := l.WebSetSetting(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/webset/internal/logic/wetsetsettinglogic.go b/server/webset/internal/logic/websetsettinglogic.go similarity index 95% rename from server/webset/internal/logic/wetsetsettinglogic.go rename to server/webset/internal/logic/websetsettinglogic.go index 702ce328..956f5796 100644 --- a/server/webset/internal/logic/wetsetsettinglogic.go +++ b/server/webset/internal/logic/websetsettinglogic.go @@ -29,7 +29,7 @@ func NewWetSetSettingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Wet } } -func (l *WetSetSettingLogic) WetSetSetting(req *types.RequestWebSet, userinfo *auth.UserInfo) (resp *basic.Response) { +func (l *WetSetSettingLogic) WebSetSetting(req *types.RequestWebSet, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null diff --git a/server/webset/internal/types/types.go b/server/webset/internal/types/types.go index f063c69f..bda8ba37 100644 --- a/server/webset/internal/types/types.go +++ b/server/webset/internal/types/types.go @@ -9,7 +9,7 @@ type RequestWebSet struct { Type string `form:"type"` } -type DataWetSet struct { +type DataWebSet struct { Title string `json:"title"` Introduction string `json:"introduction"` List interface{} `json:"list"` @@ -30,6 +30,20 @@ type Auth struct { RefreshAfter int64 `json:"refreshAfter"` } +type File struct { + Filename string `fsfile:"filename"` + Header map[string][]string `fsfile:"header"` + Size int64 `fsfile:"size"` + Data []byte `fsfile:"data"` +} + +type Meta struct { + TotalCount int64 `json:"totalCount"` + PageCount int64 `json:"pageCount"` + CurrentPage int `json:"currentPage"` + PerPage int `json:"perPage"` +} + // Set 设置Response的Code和Message值 func (resp *Response) Set(Code int, Message string) *Response { return &Response{ diff --git a/server/webset/test/basic.go b/server/webset/test/basic.go index 3a8a2151..b554a70c 100644 --- a/server/webset/test/basic.go +++ b/server/webset/test/basic.go @@ -1,4 +1,4 @@ -package wetset_test +package webset_test import ( "fmt" diff --git a/server/webset/test/wetset_test.go b/server/webset/test/wetset_test.go index 44dd81cb..f7a522f4 100644 --- a/server/webset/test/wetset_test.go +++ b/server/webset/test/wetset_test.go @@ -1,4 +1,4 @@ -package wetset_test +package webset_test import ( "fmt" diff --git a/server/webset/wetset.go b/server/webset/webset.go similarity index 89% rename from server/webset/wetset.go rename to server/webset/webset.go index 700cf37c..eb3db943 100644 --- a/server/webset/wetset.go +++ b/server/webset/webset.go @@ -12,7 +12,7 @@ import ( "github.com/zeromicro/go-zero/rest" ) -var configFile = flag.String("f", "etc/wetset.yaml", "the config file") +var configFile = flag.String("f", "etc/webset.yaml", "the config file") func main() { flag.Parse() diff --git a/server_api/webset.api b/server_api/webset.api index 2bb41ded..51e02531 100644 --- a/server_api/webset.api +++ b/server_api/webset.api @@ -9,7 +9,7 @@ info ( import "basic.api" -service wetset { +service webset { @handler WetSetSettingHandler get /web-set/setting(RequestWebSet) returns (response); } @@ -18,7 +18,7 @@ type RequestWebSet { Type string `form:"type"` } -type DataWetSet { +type DataWebSet { Title string `json:"title"` Introduction string `json:"introduction"` List interface{} `json:"list"`