Skip to content

myname #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open

myname #1477

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1720514
myname
donutt03 May 5, 2025
6ece40b
add ci.yml
donutt03 May 5, 2025
4401d67
success
donutt03 May 5, 2025
9a9ee14
broken code
donutt03 May 5, 2025
71a992f
fix broken code
donutt03 May 5, 2025
9f40418
add coverage
donutt03 May 5, 2025
b79a389
status
donutt03 May 5, 2025
ba014a0
Merge pull request #1 from donutz03/addtests
donutz03 May 5, 2025
4a19c52
format wrong
donutt03 May 7, 2025
254b839
br
donutt03 May 7, 2025
dbd9d47
add new yml
donutt03 May 7, 2025
01fad7f
security
donutt03 May 7, 2025
d7112c4
security
donutt03 May 7, 2025
accc65f
fix security
donutt03 May 7, 2025
0206706
cd
donutt03 May 7, 2025
3d07bd7
Merge pull request #2 from donutz03/addtests
donutz03 May 7, 2025
5ed3d31
GC integration
donutt03 May 7, 2025
ad74a75
Merge pull request #3 from donutz03/addtests
donutz03 May 7, 2025
858fa49
fix
donutt03 May 7, 2025
bdfd8e7
fix (#4)
donutz03 May 7, 2025
255df3e
a
donutt03 May 7, 2025
48dbdfa
ab
donutt03 May 7, 2025
8c872fd
Merge branch 'main' into addtests
donutz03 May 7, 2025
1a29cc8
Addtests (#5)
donutz03 May 7, 2025
aafe4dc
kk
donutt03 May 7, 2025
4bf4e46
Merge branch 'main' into addtests
donutz03 May 7, 2025
6c868ec
Addtests (#6)
donutz03 May 7, 2025
5ea247e
d
donutt03 May 7, 2025
8df1e42
Merge branch 'main' into addtests
donutz03 May 7, 2025
82baf1f
Addtests (#7)
donutz03 May 7, 2025
60c71a5
d
donutt03 May 7, 2025
a29b9c1
Merge branch 'main' into addtests
donutz03 May 7, 2025
aa5a571
Addtests (#8)
donutz03 May 7, 2025
8ff6fbe
x
donutt03 May 7, 2025
b73703e
Merge branch 'main' into addtests
donutz03 May 7, 2025
4f230dc
Addtests (#9)
donutz03 May 7, 2025
d39d999
Update cd.yml
donutz03 May 7, 2025
b8a865a
auto
donutt03 May 8, 2025
9a60d15
Merge branch 'main' into addtests
donutz03 May 8, 2025
689df6f
a
donutt03 May 8, 2025
6de5498
aaa
donutt03 May 8, 2025
3f3d548
b
donutt03 May 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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: Install 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

- name: Use gcloud CLI
run: gcloud info

- name: Run DB migrations
run: ./scripts/migrateup.sh

- name: Build and push Docker image to Artifact Registry
run: |
gcloud builds submit --tag us-central1-docker.pkg.dev/notely-459117/notely-ar-repo/notely:latest --service-account=projects/notely-459117/serviceAccounts/[email protected] --gcs-log-dir=gs://notely-459117_cloudbuild/logs

- name: Deploy to Cloud Run
run: |
gcloud run deploy notely \
--image us-central1-docker.pkg.dev/notely-459117/notely-ar-repo/notely:latest \
--region us-central1 \
--allow-unauthenticated \
--project notely-459117 \
--max-instances=4
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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: Tests locally auth
run: go test ./... -cover

- name: Install gosec
run: go install github.com/securego/gosec/v2/cmd/gosec@latest

- name: Run gosec security scan
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 format automatically
run: test -z $(go fmt ./...)

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![ci](https://github.com/donutz03/learn-cicd-starter/actions/workflows/ci.yml/badge.svg?branch=addtests)](https://github.com/donutz03/learn-cicd-starter/actions/workflows/ci.yml)

# 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).
Expand All @@ -18,6 +20,7 @@ Run the server:
go build -o notely && ./notely
```

*This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`.
_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!
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!
donutz03
68 changes: 68 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package auth

import (
"errors"
"net/http"
"testing"
)

func TestGetAPIKey(t *testing.T) {
tests := []struct {
name string
headers http.Header
expectedKey string
expectedError error
}{
{
name: "No Authorization header",
headers: http.Header{},
expectedKey: "",
expectedError: ErrNoAuthHeaderIncluded,
},
{
name: "Malformed Authorization header - no space",
headers: http.Header{
"Authorization": []string{"ApiKey"},
},
expectedKey: "",
expectedError: errors.New("malformed authorization header"),
},
{
name: "Malformed Authorization header - wrong prefix",
headers: http.Header{
"Authorization": []string{"Bearer token123"},
},
expectedKey: "",
expectedError: errors.New("malformed authorization header"),
},
{
name: "Valid Authorization header",
headers: http.Header{
"Authorization": []string{"ApiKey test123"},
},
expectedKey: "test123",
expectedError: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key, err := GetAPIKey(tt.headers)
if err != nil && tt.expectedError == nil {
t.Errorf("GetAPIKey() unexpected error = %v", err)
return
}
if err == nil && tt.expectedError != nil {
t.Errorf("GetAPIKey() expected error = %v, got nil", tt.expectedError)
return
}
if err != nil && tt.expectedError != nil && err.Error() != tt.expectedError.Error() {
t.Errorf("GetAPIKey() error = %v, expected error = %v", err, tt.expectedError)
return
}
if key != tt.expectedKey {
t.Errorf("GetAPIKey() key = %v, expected key = %v", key, tt.expectedKey)
}
})
}
}
4 changes: 3 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)
if _, err := w.Write(dat); err != nil {
log.Printf("failed to write response: %v", err)
}
}
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand Down Expand Up @@ -89,8 +90,9 @@ func main() {

router.Mount("/v1", v1Router)
srv := &http.Server{
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: 5 * time.Second,
Addr: ":" + port,
Handler: router,
}

log.Printf("Serving on port: %s\n", port)
Expand Down
Loading