diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000000..4dbb089cc9 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,53 @@ +name: cd + +on: + push: + branches: [main] + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Set up Goose + run: go install github.com/pressly/goose/v3/cmd/goose@latest + + - name: Build app + run: scripts/buildprod.sh + + - id: 'auth' + uses: 'google-github-actions/auth@v2' + with: + credentials_json: '${{ secrets.GCP_CREDENTIALS }}' + + - name: 'Set up Cloud SDK' + uses: 'google-github-actions/setup-gcloud@v2' + with: + version: '>= 363.0.0' + + - name: Build and push Docker image + run: gcloud builds submit --tag us-central1-docker.pkg.dev/red-girder-468300-f2/notely-ar-repo/notely:latest . + + - name: Debug DATABASE_URL + run: echo "DATABASE_URL starts with ${DATABASE_URL:0:20}" + + - name: Check if DATABASE_URL is set + run: '[ -z "$DATABASE_URL" ] && echo "DATABASE_URL is empty" || echo "DATABASE_URL is set"' + + - name: Migrate DB + run: ./scripts/migrateup.sh + + - name: Deploy to Cloud Run + run: gcloud run deploy notely --image us-central1-docker.pkg.dev/red-girder-468300-f2/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project red-girder-468300-f2 --max-instances=4 \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..1edb331966 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,52 @@ +name: ci + +on: + pull_request: + branches: [main] + +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Run go version + run: go test -cover ./ + + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Run gosec + run: gosec ./... + + style: + name: Style + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Style + run: test -z $(go fmt ./...) + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Run staticcheck + run: staticcheck ./... + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2be3d18b81..621c14581f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,4 +4,6 @@ RUN apt-get update && apt-get install -y ca-certificates ADD notely /usr/bin/notely +ADD static /static + CMD ["notely"] diff --git a/README.md b/README.md index c2bec0368b..1b887a7ed6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ + + # learn-cicd-starter (Notely) This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev). @@ -21,3 +23,5 @@ go build -o notely && ./notely *This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`. You do *not* need to set up a database or any interactivity on the webpage yet. Instructions for that will come later in the course! + +Eric's version of Boot.dev's Notely app. diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..db38c74ee8 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,24 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestCorrectAPIKey(t *testing.T) { + req, _ := http.NewRequest("GET", "/", nil) + req.Header.Set("Authorization", "ApiKey zesxrctfvygbunijmo78516zesxdfjnimkop") + + actual_key, err := GetAPIKey(req.Header) + if actual_key != "zesxrctfvygbunijmo78516zesxdfjnimkop" || err != nil { + t.Errorf("incorrect or missing key") + } +} + +func TestMissingHeader(t *testing.T) { + req, _ := http.NewRequest("GET", "/", nil) + actual_key, err := GetAPIKey(req.Header) + if actual_key != "" || err == nil { + t.Errorf("expected empty key and an error when header is missing, got key=%q, err=%v", actual_key, err) + } +} diff --git a/json.go b/json.go index 1e6e7985e1..a2973c77ed 100644 --- a/json.go +++ b/json.go @@ -30,5 +30,9 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { return } w.WriteHeader(code) - w.Write(dat) + _, err = w.Write(dat) + if err != nil { + // Log the error or handle it appropriately + log.Printf("Error writing response: %v", err) + } } diff --git a/main.go b/main.go index 19d7366c5f..b3a4ade65d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -89,8 +90,9 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: 30 * time.Second, } log.Printf("Serving on port: %s\n", port) diff --git a/scripts/migrateup.sh b/scripts/migrateup.sh index fe69c6fc7a..2428f842ac 100755 --- a/scripts/migrateup.sh +++ b/scripts/migrateup.sh @@ -4,5 +4,4 @@ if [ -f .env ]; then source .env fi -cd sql/schema -goose turso $DATABASE_URL up +goose turso "$DATABASE_URL" up -dir sql/schema diff --git a/static/index.html b/static/index.html index 72be101028..5d4ad73c09 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@
-