From 03ae928d1b55715f6bb56f14ed5fc3cb39db5946 Mon Sep 17 00:00:00 2001 From: dijiachen Date: Tue, 29 Oct 2024 17:26:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=B7=A8?= =?UTF-8?q?=E5=9F=9F=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmd/dev-entrypoint/init_test.go | 20 ++++++++ cnb-builder-shim/go.mod | 20 +++++--- cnb-builder-shim/go.sum | 29 +++++++++++ .../internal/devsandbox/config/loader.go | 51 ++++++++++++++++++- .../internal/devsandbox/config/types.go | 22 ++++++++ .../internal/devsandbox/webserver/server.go | 12 +++++ 6 files changed, 144 insertions(+), 10 deletions(-) diff --git a/cnb-builder-shim/cmd/dev-entrypoint/init_test.go b/cnb-builder-shim/cmd/dev-entrypoint/init_test.go index 1418e44184..12e27d0094 100644 --- a/cnb-builder-shim/cmd/dev-entrypoint/init_test.go +++ b/cnb-builder-shim/cmd/dev-entrypoint/init_test.go @@ -25,6 +25,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/internal/devsandbox/config" "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/logging" ) @@ -100,3 +101,22 @@ version = 'v213' `)) }) }) + +var _ = Describe("Test InitConfig", func() { + var err error + BeforeEach(func() { + err = config.InitConfig() + Expect(err).To(BeNil()) + }) + It("test load config with default values", func() { + sourceConfig := config.G.SourceCode + Expect(sourceConfig.FetchMethod).To(Equal(config.HTTP)) + Expect(sourceConfig.Workspace).To(Equal("/cnb/devsandbox/src")) + corsConfig := config.G.Service.Cors + Expect(corsConfig.AllowOrigins).To(Equal([]string{""})) + Expect(corsConfig.AllowMethods).To(Equal([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"})) + Expect(corsConfig.AllowHeaders).To(Equal([]string{"Origin", "Content-Type", "Authorization"})) + Expect(corsConfig.ExposeHeaders).To(Equal([]string{"Content-Length"})) + Expect(corsConfig.AllowCredentials).To(Equal(true)) + }) +}) diff --git a/cnb-builder-shim/go.mod b/cnb-builder-shim/go.mod index cf93c36358..bcb3b121fe 100644 --- a/cnb-builder-shim/go.mod +++ b/cnb-builder-shim/go.mod @@ -13,7 +13,7 @@ require ( github.com/mholt/archiver/v3 v3.5.1 github.com/onsi/ginkgo/v2 v2.9.4 github.com/onsi/gomega v1.27.6 - github.com/pelletier/go-toml/v2 v2.0.8 + github.com/pelletier/go-toml/v2 v2.2.1 github.com/pkg/errors v0.9.1 github.com/shabbywu/logfmtr v0.2.3 github.com/spf13/pflag v1.0.5 @@ -22,19 +22,23 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect - github.com/bytedance/sonic v1.9.1 // indirect + github.com/bytedance/sonic v1.11.6 // indirect + github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect github.com/docker/cli v24.0.6+incompatible // indirect github.com/docker/distribution v2.8.3+incompatible // indirect github.com/docker/docker v27.3.1+incompatible // indirect github.com/docker/docker-credential-helpers v0.8.0 // indirect github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect - github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gin-contrib/cors v1.7.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/snappy v0.0.4 // indirect @@ -43,10 +47,10 @@ require ( github.com/iand/logfmtr v0.2.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.1 // indirect - github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/klauspost/pgzip v1.2.6 // indirect github.com/kr/pretty v0.3.1 // indirect - github.com/leodido/go-urn v1.2.4 // indirect + github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect @@ -60,13 +64,13 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect - github.com/ugorji/go/codec v1.2.11 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect github.com/ulikunitz/xz v0.5.11 // indirect github.com/vbatts/tar-split v0.11.5 // indirect github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect go.opentelemetry.io/otel/trace v1.31.0 // indirect - golang.org/x/arch v0.3.0 // indirect + golang.org/x/arch v0.7.0 // indirect golang.org/x/crypto v0.28.0 // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/sync v0.8.0 // indirect diff --git a/cnb-builder-shim/go.sum b/cnb-builder-shim/go.sum index 8d3ef8b0c2..7a920de8f6 100644 --- a/cnb-builder-shim/go.sum +++ b/cnb-builder-shim/go.sum @@ -14,6 +14,10 @@ github.com/buildpacks/lifecycle v0.18.3/go.mod h1:lLVVv+RTs2NA5T1Dh/cuvjW4LcUhYs github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= +github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA= github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= @@ -22,6 +26,10 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/containerd/stargz-snapshotter/estargz v0.14.3 h1:OqlDCK3ZVUO6C3B/5FSkDwbkEETK84kQgEeFwDC+62k= github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNAHhJogcZtrNHdKrA99/FCCRjE3HD36o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= @@ -49,6 +57,10 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw= +github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= @@ -67,6 +79,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8= +github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= @@ -103,15 +117,20 @@ github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= +github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU= github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo= @@ -142,6 +161,8 @@ github.com/opencontainers/image-spec v1.1.0-rc5 h1:Ygwkfw9bpDvs+c9E34SdgGOj41dX/ github.com/opencontainers/image-spec v1.1.0-rc5/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg= +github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= @@ -164,6 +185,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -172,12 +194,15 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= @@ -197,6 +222,8 @@ go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06F golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc= +golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= @@ -208,6 +235,7 @@ golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -226,4 +254,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/cnb-builder-shim/internal/devsandbox/config/loader.go b/cnb-builder-shim/internal/devsandbox/config/loader.go index f89455246f..3d5dbde1f2 100644 --- a/cnb-builder-shim/internal/devsandbox/config/loader.go +++ b/cnb-builder-shim/internal/devsandbox/config/loader.go @@ -1,6 +1,10 @@ package config -import "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/utils" +import ( + "strings" + + "github.com/TencentBlueking/bkpaas/cnb-builder-shim/pkg/utils" +) var G *Config @@ -30,11 +34,54 @@ func loadSourceConfigFromEnv() (SourceCodeConfig, error) { }, nil } +func loadCorsConfigFromEnv() (CorsConfig, error) { + allowOrigins := getListFromEnv("ALLOW_ORIGINS", "") + allowMethods := getListFromEnv("ALLOW_METHODS", "GET,POST,PUT,DELETE,OPTIONS") + allowHeaders := getListFromEnv("ALLOW_HEADERS", "Origin,Content-Type,Authorization") + exposeHeaders := getListFromEnv("EXPOSE_HEADERS", "Content-Length") + allowCredentials := utils.EnvOrDefault("ALLOW_CREDENTIALS", "true") == "true" + return CorsConfig{ + AllowOrigins: allowOrigins, + AllowMethods: allowMethods, + AllowHeaders: allowHeaders, + ExposeHeaders: exposeHeaders, + AllowCredentials: allowCredentials, + }, nil +} +func loadServiceConfigFromEnv() (ServiceConfig, error) { + corsConfig, err := loadCorsConfigFromEnv() + if err != nil { + return ServiceConfig{}, err + } + return ServiceConfig{ + Cors: corsConfig, + }, nil +} + func loadConfigFromEnv() (*Config, error) { sourceCodeConfig, err := loadSourceConfigFromEnv() if err != nil { return nil, err } - return &Config{SourceCode: sourceCodeConfig}, nil + serviceConfig, err := loadServiceConfigFromEnv() + if err != nil { + return nil, err + } + return &Config{ + SourceCode: sourceCodeConfig, + Service: serviceConfig, + }, nil +} + +// getListFromEnv 从指定的环境变量中获取列表, 例如 "value1,value2,value3" +func getListFromEnv(envVar string, defaultValue string) []string { + // 获取环境变量的值 + listStr := utils.EnvOrDefault(envVar, defaultValue) + // 使用 "," 分割字符串并去除空格 + elements := strings.Split(listStr, ",") + for i := range elements { + elements[i] = strings.TrimSpace(elements[i]) + } + return elements } diff --git a/cnb-builder-shim/internal/devsandbox/config/types.go b/cnb-builder-shim/internal/devsandbox/config/types.go index daeccf299e..cfa8a4a0ad 100644 --- a/cnb-builder-shim/internal/devsandbox/config/types.go +++ b/cnb-builder-shim/internal/devsandbox/config/types.go @@ -21,8 +21,30 @@ type SourceCodeConfig struct { Workspace string } +// CorsConfig 跨域配置 +type CorsConfig struct { + // 允许的来源 + AllowOrigins []string + // 允许的HTTP方法 + AllowMethods []string + // 允许的请求头 + AllowHeaders []string + // 暴露的响应头 + ExposeHeaders []string + // 凭证共享 + AllowCredentials bool +} + +// ServiceConfig 服务配置 +type ServiceConfig struct { + // 源码获取方式 + Cors CorsConfig +} + // Config 全局配置 type Config struct { // 源码配置 SourceCode SourceCodeConfig + // 服务配置 + Service ServiceConfig } diff --git a/cnb-builder-shim/internal/devsandbox/webserver/server.go b/cnb-builder-shim/internal/devsandbox/webserver/server.go index d876ac8fa8..d5232ef256 100644 --- a/cnb-builder-shim/internal/devsandbox/webserver/server.go +++ b/cnb-builder-shim/internal/devsandbox/webserver/server.go @@ -26,8 +26,10 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/caarlos0/env/v10" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/go-logr/logr" @@ -61,6 +63,16 @@ func New(lg *logr.Logger) (*WebServer, error) { } r := gin.Default() + corsConfig := config.G.Service.Cors + // 添加跨域中间件 + r.Use(cors.New(cors.Config{ + AllowOrigins: corsConfig.AllowOrigins, // 允许所有来源 + AllowMethods: corsConfig.AllowMethods, // 允许的HTTP方法 + AllowHeaders: corsConfig.AllowHeaders, // 允许的请求头 + ExposeHeaders: corsConfig.ExposeHeaders, // 暴露的响应头 + AllowCredentials: corsConfig.AllowCredentials, // 凭证共享 + MaxAge: 12 * time.Hour, // 预检请求缓存时间 + })) r.Use(tokenAuthMiddleware(cfg.Token)) s := &WebServer{ From 6f7b58c5cbaf11414fcfa4d0df8a1d4c748008ea Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 30 Oct 2024 16:54:57 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix=EF=BC=9ACors=20=E5=A4=A7=E5=86=99?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A=E3=80=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20getListFromEnv=20=E5=85=A5=E5=8F=82=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cnb-builder-shim/cmd/dev-entrypoint/init_test.go | 2 +- .../internal/devsandbox/config/loader.go | 14 +++++++------- .../internal/devsandbox/config/types.go | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cnb-builder-shim/cmd/dev-entrypoint/init_test.go b/cnb-builder-shim/cmd/dev-entrypoint/init_test.go index 12e27d0094..cbd4e21f32 100644 --- a/cnb-builder-shim/cmd/dev-entrypoint/init_test.go +++ b/cnb-builder-shim/cmd/dev-entrypoint/init_test.go @@ -112,7 +112,7 @@ var _ = Describe("Test InitConfig", func() { sourceConfig := config.G.SourceCode Expect(sourceConfig.FetchMethod).To(Equal(config.HTTP)) Expect(sourceConfig.Workspace).To(Equal("/cnb/devsandbox/src")) - corsConfig := config.G.Service.Cors + corsConfig := config.G.Service.CORS Expect(corsConfig.AllowOrigins).To(Equal([]string{""})) Expect(corsConfig.AllowMethods).To(Equal([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"})) Expect(corsConfig.AllowHeaders).To(Equal([]string{"Origin", "Content-Type", "Authorization"})) diff --git a/cnb-builder-shim/internal/devsandbox/config/loader.go b/cnb-builder-shim/internal/devsandbox/config/loader.go index 3d5dbde1f2..e5b18854ca 100644 --- a/cnb-builder-shim/internal/devsandbox/config/loader.go +++ b/cnb-builder-shim/internal/devsandbox/config/loader.go @@ -34,13 +34,13 @@ func loadSourceConfigFromEnv() (SourceCodeConfig, error) { }, nil } -func loadCorsConfigFromEnv() (CorsConfig, error) { +func loadCORSConfigFromEnv() (CORSConfig, error) { allowOrigins := getListFromEnv("ALLOW_ORIGINS", "") allowMethods := getListFromEnv("ALLOW_METHODS", "GET,POST,PUT,DELETE,OPTIONS") allowHeaders := getListFromEnv("ALLOW_HEADERS", "Origin,Content-Type,Authorization") exposeHeaders := getListFromEnv("EXPOSE_HEADERS", "Content-Length") allowCredentials := utils.EnvOrDefault("ALLOW_CREDENTIALS", "true") == "true" - return CorsConfig{ + return CORSConfig{ AllowOrigins: allowOrigins, AllowMethods: allowMethods, AllowHeaders: allowHeaders, @@ -49,12 +49,12 @@ func loadCorsConfigFromEnv() (CorsConfig, error) { }, nil } func loadServiceConfigFromEnv() (ServiceConfig, error) { - corsConfig, err := loadCorsConfigFromEnv() + corsConfig, err := loadCORSConfigFromEnv() if err != nil { return ServiceConfig{}, err } return ServiceConfig{ - Cors: corsConfig, + CORS: corsConfig, }, nil } @@ -74,10 +74,10 @@ func loadConfigFromEnv() (*Config, error) { }, nil } -// getListFromEnv 从指定的环境变量中获取列表, 例如 "value1,value2,value3" -func getListFromEnv(envVar string, defaultValue string) []string { +// 从指定的环境变量中获取列表, 例如 "value1,value2,value3" +func getListFromEnv(envVar string, defaultEnvValue string) []string { // 获取环境变量的值 - listStr := utils.EnvOrDefault(envVar, defaultValue) + listStr := utils.EnvOrDefault(envVar, defaultEnvValue) // 使用 "," 分割字符串并去除空格 elements := strings.Split(listStr, ",") for i := range elements { diff --git a/cnb-builder-shim/internal/devsandbox/config/types.go b/cnb-builder-shim/internal/devsandbox/config/types.go index cfa8a4a0ad..7f4077fcd6 100644 --- a/cnb-builder-shim/internal/devsandbox/config/types.go +++ b/cnb-builder-shim/internal/devsandbox/config/types.go @@ -21,8 +21,8 @@ type SourceCodeConfig struct { Workspace string } -// CorsConfig 跨域配置 -type CorsConfig struct { +// CORSConfig 跨域配置 +type CORSConfig struct { // 允许的来源 AllowOrigins []string // 允许的HTTP方法 @@ -37,8 +37,8 @@ type CorsConfig struct { // ServiceConfig 服务配置 type ServiceConfig struct { - // 源码获取方式 - Cors CorsConfig + // 跨域配置 + CORS CORSConfig } // Config 全局配置 From 32fd4287d9d2a3117eadc8871cb76313d0ed9d25 Mon Sep 17 00:00:00 2001 From: dijiachen Date: Wed, 30 Oct 2024 21:38:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=EF=BC=9A=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cnb-builder-shim/internal/devsandbox/webserver/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cnb-builder-shim/internal/devsandbox/webserver/server.go b/cnb-builder-shim/internal/devsandbox/webserver/server.go index d5232ef256..4f6b81ce77 100644 --- a/cnb-builder-shim/internal/devsandbox/webserver/server.go +++ b/cnb-builder-shim/internal/devsandbox/webserver/server.go @@ -63,7 +63,7 @@ func New(lg *logr.Logger) (*WebServer, error) { } r := gin.Default() - corsConfig := config.G.Service.Cors + corsConfig := config.G.Service.CORS // 添加跨域中间件 r.Use(cors.New(cors.Config{ AllowOrigins: corsConfig.AllowOrigins, // 允许所有来源