diff --git a/.github/workflows/docker-build-push.yml b/.github/workflows/docker-build-push.yml new file mode 100644 index 0000000..159aba3 --- /dev/null +++ b/.github/workflows/docker-build-push.yml @@ -0,0 +1,34 @@ +name: Publish Docker image + +on: + release: + types: [published] + +jobs: + push_to_registry: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: sj-williams/go-simple-server + + - name: Build and push Docker image + uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml deleted file mode 100644 index ae07a79..0000000 --- a/.github/workflows/docker-image.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Docker Image CI - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - -jobs: - - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Build the Docker image - run: docker build . --file Dockerfile --tag go-simple-server:$(date +%s) - - - name: list docker images - run: docker image ls diff --git a/Dockerfile b/Dockerfile index 7c2e315..5148643 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,27 @@ # syntax=docker/dockerfile:1 +FROM golang:1.21-alpine AS builder -FROM golang:1.18-alpine +RUN addgroup -g 1000 -S appgroup && \ + adduser -u 1000 -S appuser -G appgroup -WORKDIR /app +WORKDIR /src -COPY go.mod ./ -RUN go mod download +COPY server.go . -COPY *.go ./ +RUN go build -o /bin/server ./server.go -RUN go build -o /server +RUN chown -R appuser:appgroup /bin + +FROM scratch + +# Copy static executable +COPY --from=builder /bin/server /bin/server + +# Copy the user and group files from the builder stage +COPY --from=builder /etc/passwd /etc/passwd + +USER 1000 EXPOSE 8080 -CMD [ "/server" ] \ No newline at end of file +CMD ["/bin/server"] \ No newline at end of file diff --git a/README.md b/README.md index 2ee9854..97c1d2d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # go-simple-server -Just setting up to check git tools setup and working +Super simple go http server for network testing etc -Added github actions for docker build, testing with new branch. +## Use + +```bash +curl -d "username=myuser&password=1234" http://localhost:8080/ +``` \ No newline at end of file diff --git a/deployment/deployment.yaml b/deployment/deployment.yaml new file mode 100644 index 0000000..06ee1b9 --- /dev/null +++ b/deployment/deployment.yaml @@ -0,0 +1,17 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: go-simple-server +spec: + replicas: 1 + selector: + matchLabels: + app: go-simple-server + template: + metadata: + labels: + app: go-simple-server + spec: + containers: + - name: go-simple-server + image: # \ No newline at end of file diff --git a/deployment/service.yaml b/deployment/service.yaml new file mode 100644 index 0000000..d02b683 --- /dev/null +++ b/deployment/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: go-simple-server-service +spec: + selector: + app: go-simple-server + ports: + - name: http + port: 8080 + targetPort: 8080 \ No newline at end of file diff --git a/go.mod b/go.mod index 13f5b93..0dc11f9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module example.com/server +module github.com/sj-williams/go-simple-server -go 1.18 +go 1.20 diff --git a/server b/server deleted file mode 100755 index 44ca43a..0000000 Binary files a/server and /dev/null differ diff --git a/server.go b/server.go index 2c45c7d..5d86cdc 100644 --- a/server.go +++ b/server.go @@ -1,15 +1,22 @@ package main import ( - "fmt" - "net/http" + "fmt" + "net/http" ) -func helloWorld(w http.ResponseWriter, r *http.Request){ - fmt.Fprintf(w, "Hello, World!") -} - func main() { - http.HandleFunc("/", helloWorld) - http.ListenAndServe(":8080", nil) -} \ No newline at end of file + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + username := r.FormValue("username") + password := r.FormValue("password") + + if username != "admin" || password != "DummyPassword" { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + + fmt.Fprintf(w, "Welcome, %s!", username) + }) + + http.ListenAndServe(":8080", nil) +}