diff --git a/goctl_template/api/main.tpl b/goctl_template/api/main.tpl index 5d1a8518..e631d7d7 100644 --- a/goctl_template/api/main.tpl +++ b/goctl_template/api/main.tpl @@ -15,7 +15,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/goctl_template_backend/api/main.tpl b/goctl_template_backend/api/main.tpl index a90d8dfc..c7055076 100644 --- a/goctl_template_backend/api/main.tpl +++ b/goctl_template_backend/api/main.tpl @@ -15,7 +15,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/proxyserver/main.go b/proxyserver/main.go index e6fc3ff5..7e4f93b3 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -30,7 +30,7 @@ func SetCors(w http.ResponseWriter, r *http.Request) { func main() { rootDir := "../server" // Change this to your root directory - vueBuild := "/home/ldm/workdir/fusenpack-vue-created" + vueBuild := "/home/eson/workspace/fusenpack-vue-created" apiURL, err := url.Parse("http://localhost:9900") if err != nil { panic(err) @@ -130,7 +130,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac return } - hasBridge := strings.Contains(r.Header.Get("Access-Control-Request-Headers"), "bridge") + // hasBridge := strings.Contains(r.Header.Get("Access-Control-Request-Headers"), "bridge") // 解析目标URL时已经包含了查询参数 targetURL.RawQuery = r.URL.RawQuery @@ -166,9 +166,9 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac } } - if hasBridge { - w.Header().Add("Access-Control-Allow-Headers", "Bridge") - } + // if hasBridge { + // w.Header().Add("Access-Control-Allow-Headers", "Bridge") + // } // 转发目标服务器的响应状态码和主体 w.WriteHeader(resp.StatusCode) @@ -177,8 +177,8 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac http.Error(w, "Error copying proxy response", http.StatusInternalServerError) return } - } + for _, muxPath := range muxPaths { mux.HandleFunc(muxPath, handleRequest) } diff --git a/server/backend/backend.go b/server/backend/backend.go index 561ac8b6..099cb974 100644 --- a/server/backend/backend.go +++ b/server/backend/backend.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/backend/internal/config" "fusenapi/server/backend/internal/handler" "fusenapi/server/backend/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/canteen/canteen.go b/server/canteen/canteen.go index b252e60b..c408570d 100644 --- a/server/canteen/canteen.go +++ b/server/canteen/canteen.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/canteen/internal/config" "fusenapi/server/canteen/internal/handler" "fusenapi/server/canteen/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/data-transfer/data-transfer.go b/server/data-transfer/data-transfer.go index 45b8540d..8ad3fa67 100644 --- a/server/data-transfer/data-transfer.go +++ b/server/data-transfer/data-transfer.go @@ -6,6 +6,8 @@ import ( config2 "fusenapi/server/data-transfer/internal/config" handler2 "fusenapi/server/data-transfer/internal/handler" svc2 "fusenapi/server/data-transfer/internal/svc" + "fusenapi/utils/auth" + "net/http" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -19,7 +21,9 @@ func main() { var c config2.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc2.NewServiceContext(c) diff --git a/server/home-user-auth/home-user-auth.go b/server/home-user-auth/home-user-auth.go index 4edb9474..55820779 100644 --- a/server/home-user-auth/home-user-auth.go +++ b/server/home-user-auth/home-user-auth.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/home-user-auth/internal/config" "fusenapi/server/home-user-auth/internal/handler" "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/inventory/inventory.go b/server/inventory/inventory.go index 2b2af176..10661756 100644 --- a/server/inventory/inventory.go +++ b/server/inventory/inventory.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/inventory/internal/config" "fusenapi/server/inventory/internal/handler" "fusenapi/server/inventory/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/map-library/map-library.go b/server/map-library/map-library.go index 6958ce1c..b8f7e38b 100644 --- a/server/map-library/map-library.go +++ b/server/map-library/map-library.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/map-library/internal/config" "fusenapi/server/map-library/internal/handler" "fusenapi/server/map-library/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/orders/orders.go b/server/orders/orders.go index 01a2ad6d..fc951aa8 100644 --- a/server/orders/orders.go +++ b/server/orders/orders.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/orders/internal/config" "fusenapi/server/orders/internal/handler" "fusenapi/server/orders/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/product-model/product-model.go b/server/product-model/product-model.go index 06ee4752..a6374de1 100644 --- a/server/product-model/product-model.go +++ b/server/product-model/product-model.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/product-model/internal/config" "fusenapi/server/product-model/internal/handler" "fusenapi/server/product-model/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/product-template/product-template.go b/server/product-template/product-template.go index db5296e8..3819752a 100644 --- a/server/product-template/product-template.go +++ b/server/product-template/product-template.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/product-template/internal/config" "fusenapi/server/product-template/internal/handler" "fusenapi/server/product-template/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/product/product.go b/server/product/product.go index d2082584..13fba403 100644 --- a/server/product/product.go +++ b/server/product/product.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/product/internal/config" "fusenapi/server/product/internal/handler" "fusenapi/server/product/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) handler.RegisterHandlers(server, ctx) diff --git a/server/shopping-cart-confirmation/shopping-cart-confirmation.go b/server/shopping-cart-confirmation/shopping-cart-confirmation.go index f621aaa3..c5f6711c 100644 --- a/server/shopping-cart-confirmation/shopping-cart-confirmation.go +++ b/server/shopping-cart-confirmation/shopping-cart-confirmation.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/shopping-cart-confirmation/internal/config" "fusenapi/server/shopping-cart-confirmation/internal/handler" "fusenapi/server/shopping-cart-confirmation/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/upload/upload.go b/server/upload/upload.go index 48e5f663..bea4af45 100644 --- a/server/upload/upload.go +++ b/server/upload/upload.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/upload/internal/config" "fusenapi/server/upload/internal/handler" "fusenapi/server/upload/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/server/webset/webset.go b/server/webset/webset.go index eb3db943..ab154b3b 100644 --- a/server/webset/webset.go +++ b/server/webset/webset.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "net/http" "fusenapi/server/webset/internal/config" "fusenapi/server/webset/internal/handler" "fusenapi/server/webset/internal/svc" + "fusenapi/utils/auth" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" @@ -20,7 +22,9 @@ func main() { var c config.Config conf.MustLoad(*configFile, &c) - server := rest.MustNewServer(c.RestConf, rest.WithCors()) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + + })) defer server.Stop() ctx := svc.NewServiceContext(c) diff --git a/utils/auth/cors.go b/utils/auth/cors.go new file mode 100644 index 00000000..ff7354f4 --- /dev/null +++ b/utils/auth/cors.go @@ -0,0 +1,11 @@ +package auth + +import "net/http" + +func FsCors(header http.Header) { + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") + header.Set("Access-Control-Allow-Headers", "*") + header.Set("Access-Control-Expose-Headers", "*") + header.Set("Access-Control-Allow-Credentials", "true") +}