From c74114fe66464dac46f948d6433843ed6c8356bb Mon Sep 17 00:00:00 2001 From: Nicolas CARON Date: Wed, 20 Nov 2024 11:01:04 +0100 Subject: [PATCH] initialisation --- .gitea/workflows/build.yml | 81 ++++++++++++++++++++++++++++++++++++++ .gitignore | 4 ++ Dockerfile | 27 +++++++++++++ config.go | 42 ++++++++++++++++++++ go.mod | 3 ++ main.go | 12 ++++++ 6 files changed, 169 insertions(+) create mode 100644 .gitea/workflows/build.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 config.go create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..a7678ae --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,81 @@ +name: Build and Push Docker Image + +on: + push: + tags: + - '*' + branches: + - main + - dev + +jobs: + build_and_push: + runs-on: ubuntu-latest + + steps: + - name: Install Docker + run: | + apt-get update + apt-get install -y docker.io + + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set Kubernetes Context + uses: azure/k8s-set-context@v4 + with: + method: kubeconfig + kubeconfig: ${{secrets.buildx_kubeconfig}} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver: kubernetes + driver-opts: | + namespace=gitea + + - name: Login to Docker Registry + uses: docker/login-action@v3 + with: + registry: git.coopgo.io + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker image + id: metadata + uses: docker/metadata-action@v3 + with: + images: git.coopgo.io/${{gitea.repository}} + tags: | + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + flavor: | + latest=auto + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: | + ${{ steps.metadata.outputs.tags }} + build-args: | + ACCESS_TOKEN_USR=${{gitea.actor}} + ACCESS_TOKEN_PWD=${{gitea.token}} + + # BUILD WITH KANIKO + # - name: Kaniko build and push + # uses: aevea/action-kaniko@master + # with: + # build_file: Dockerfile + # registry: git.coopgo.io + # username: ${{secrets.registry_user}} + # password: ${{secrets.registry_token}} + # image: ${{gitea.repository}} + # tag: ${{gitea.ref_name}} + # cache: true + # cache_registry: git.coopgo.io/${{gitea.repository}}/cache + # extra-args: | + # ACCESS_TOKEN_USR=${{gitea.actor}} + # ACCESS_TOKEN_PWD=${{gitea.token}} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f77345f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +config.yaml +.vscode +.idea +__debug_bin \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c80c207 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM golang:alpine as builder + +ARG ACCESS_TOKEN_USR="nothing" +ARG ACCESS_TOKEN_PWD="nothing" + +RUN apk add --no-cache ca-certificates tzdata git + +WORKDIR / + +# Create a netrc file using the credentials specified using --build-arg +RUN printf "machine git.coopgo.io\n\ + login ${ACCESS_TOKEN_USR}\n\ + password ${ACCESS_TOKEN_PWD}\n"\ + >> ~/.netrc +RUN chmod 600 ~/.netrc + +COPY . . + +RUN go mod download && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /server + +FROM scratch +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=builder /server / + +EXPOSE 8080 +ENTRYPOINT ["/server"] diff --git a/config.go b/config.go new file mode 100644 index 0000000..ad4a5eb --- /dev/null +++ b/config.go @@ -0,0 +1,42 @@ +package main + +import ( + "strings" + + "github.com/spf13/viper" +) + +func ReadConfig() (*viper.Viper, error) { + defaults := map[string]any{ + "name": "COOPGO Agenda", + "dev_env": false, + "storage": map[string]any{ + "type": "mongodb", + "mongodb": map[string]any{ + "host": "localhost", + "port": "27017", + "db_name": "coopgo_platform", + "collections": map[string]any{ + "events": "diags", + }, + }, + }, + "services": map[string]any{ + "grpc": map[string]any{ + "enable": true, + "port": 8098, + }, + }, + } + + v := viper.New() + for key, value := range defaults { + v.SetDefault(key, value) + } + v.SetConfigName("config") + v.AddConfigPath(".") + v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + v.AutomaticEnv() + err := v.ReadInConfig() + return v, err +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6cc9f5d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.coopgo.io/coopgo-platform/diags + +go 1.18 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a00f0c2 --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "os" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + + "git.coopgo.io/coopgo-platform/diags/grpcapi" + "git.coopgo.io/coopgo-platform/diags/handlers" + "git.coopgo.io/coopgo-platform/diags/storage" +) \ No newline at end of file