diff --git a/app/facade/.dockerignore b/app/facade/.dockerignore new file mode 100644 index 0000000..c478eb0 --- /dev/null +++ b/app/facade/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.DS_Store +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md + +**/*.exe diff --git a/app/facade/Dockerfile b/app/facade/Dockerfile new file mode 100644 index 0000000..fd242da --- /dev/null +++ b/app/facade/Dockerfile @@ -0,0 +1,69 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/go/dockerfile-reference/ + +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 + +################################################################################ +# Create a stage for building the application. +ARG GO_VERSION=1.23.4 +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build +WORKDIR /src + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into +# the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x + +# This is the architecture you're building for, which is passed in by the builder. +# Placing it here allows the previous steps to be cached across architectures. +ARG TARGETARCH + +# Build the application. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage a bind mount to the current directory to avoid having to copy the +# source code into the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server . + +################################################################################ +# Create a new stage for running the application that contains the minimal +# runtime dependencies for the application. This often uses a different base +# image from the build stage where the necessary files are copied from the build +# stage. +# +# The example below uses the alpine image as the foundation for running the app. +# By specifying the "latest" tag, it will also use whatever happens to be the +# most recent version of that image when you build your Dockerfile. If +# reproducability is important, consider using a versioned tag +# (e.g., alpine:3.17.2) or SHA (e.g., alpine@sha256:c41ab5c992deb4fe7e5da09f67a8804a46bd0592bfdf0b1847dde0e0889d2bff). +FROM debian:12-slim AS final + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ + +# Expose the port that the application listens on. +EXPOSE 8080 + +# What the container should run when it is started. +ENTRYPOINT [ "/bin/server" ] diff --git a/app/facade/conf/conf.go b/app/facade/conf/conf.go index 96b1b16..d915e45 100644 --- a/app/facade/conf/conf.go +++ b/app/facade/conf/conf.go @@ -1,15 +1,12 @@ package conf import ( - "io/ioutil" + _ "embed" "os" - "path/filepath" "sync" "github.com/cloudwego/hertz/pkg/common/hlog" "github.com/kr/pretty" - "gopkg.in/validator.v2" - "gopkg.in/yaml.v2" ) var ( @@ -55,16 +52,20 @@ func GetConf() *Config { return conf } +//go:embed dev/conf.yaml +var configFile []byte + func initConf() { - prefix := "conf" - confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) - content, err := ioutil.ReadFile(confFileRelPath) - if err != nil { - panic(err) - } + //prefix := "conf" + //confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) + //content, err := ioutil.ReadFile(confFileRelPath) + //if err != nil { + // panic(err) + //} conf = new(Config) - err = yaml.Unmarshal(content, conf) + + err := yaml.Unmarshal(configFile, conf) if err != nil { hlog.Error("parse yaml error - %v", err) panic(err) diff --git a/app/user/.dockerignore b/app/user/.dockerignore new file mode 100644 index 0000000..c478eb0 --- /dev/null +++ b/app/user/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.DS_Store +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md + +**/*.exe diff --git a/app/user/Dockerfile b/app/user/Dockerfile new file mode 100644 index 0000000..fd242da --- /dev/null +++ b/app/user/Dockerfile @@ -0,0 +1,69 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/go/dockerfile-reference/ + +# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7 + +################################################################################ +# Create a stage for building the application. +ARG GO_VERSION=1.23.4 +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build +WORKDIR /src + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into +# the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x + +# This is the architecture you're building for, which is passed in by the builder. +# Placing it here allows the previous steps to be cached across architectures. +ARG TARGETARCH + +# Build the application. +# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. +# Leverage a bind mount to the current directory to avoid having to copy the +# source code into the container. +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 GOARCH=$TARGETARCH go build -o /bin/server . + +################################################################################ +# Create a new stage for running the application that contains the minimal +# runtime dependencies for the application. This often uses a different base +# image from the build stage where the necessary files are copied from the build +# stage. +# +# The example below uses the alpine image as the foundation for running the app. +# By specifying the "latest" tag, it will also use whatever happens to be the +# most recent version of that image when you build your Dockerfile. If +# reproducability is important, consider using a versioned tag +# (e.g., alpine:3.17.2) or SHA (e.g., alpine@sha256:c41ab5c992deb4fe7e5da09f67a8804a46bd0592bfdf0b1847dde0e0889d2bff). +FROM debian:12-slim AS final + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ + +# Expose the port that the application listens on. +EXPOSE 8080 + +# What the container should run when it is started. +ENTRYPOINT [ "/bin/server" ] diff --git a/app/user/conf/conf.go b/app/user/conf/conf.go index 5640ca8..fbac3a3 100644 --- a/app/user/conf/conf.go +++ b/app/user/conf/conf.go @@ -1,18 +1,15 @@ package conf import ( + _ "embed" "github.com/spf13/viper" - "io/ioutil" "os" - "path/filepath" "sync" "github.com/cloudwego/kitex/pkg/klog" "github.com/kr/pretty" _ "github.com/spf13/viper" _ "github.com/spf13/viper/remote" - "gopkg.in/validator.v2" - "gopkg.in/yaml.v2" ) var ( @@ -61,15 +58,18 @@ func GetConf() *Config { return conf } +//go:embed dev/conf.yaml +var configFile []byte + func initConf() { - prefix := "conf" - confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) - content, err := ioutil.ReadFile(confFileRelPath) - if err != nil { - panic(err) - } + //prefix := "conf" + //confFileRelPath := filepath.Join(prefix, filepath.Join(GetEnv(), "conf.yaml")) + //content, err := ioutil.ReadFile(confFileRelPath) + //if err != nil { + // panic(err) + //} conf = new(Config) - err = yaml.Unmarshal(content, conf) + err := yaml.Unmarshal(configFile, conf) // viper 获取远程配置测试 err = viper.AddRemoteProvider("consul", conf.Registry.RegistryAddress[0], "USER")