Skip to content
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

merge #1183

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

merge #1183

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1f6b344
add vannest version
VannestBun Feb 26, 2025
0d309ab
Merge pull request #1 from VannestBun/addtests
VannestBun Feb 26, 2025
d7bae9e
add more a bit
VannestBun Feb 26, 2025
00ee49c
add basic CI
VannestBun Feb 26, 2025
486110d
add ci check go version
VannestBun Feb 26, 2025
0c4368d
test the code
VannestBun Feb 26, 2025
6233966
undo broken code
VannestBun Feb 26, 2025
e114751
add cover in test
VannestBun Feb 26, 2025
6a684aa
Merge pull request #2 from VannestBun/addtests
VannestBun Feb 26, 2025
eb752ad
build status
VannestBun Feb 26, 2025
4a0290f
Merge pull request #3 from VannestBun/addtests
VannestBun Feb 26, 2025
e7ae800
add formating style test
VannestBun Feb 26, 2025
8973346
add staticcheck
VannestBun Feb 26, 2025
02343d9
remove unused function
VannestBun Feb 26, 2025
10b07d4
added gosec security checks
VannestBun Feb 26, 2025
353bd46
fix security
VannestBun Feb 26, 2025
e9d5b0f
format
VannestBun Feb 26, 2025
f1fdb80
added cd flow
VannestBun Feb 27, 2025
a0bfe14
remove ./ in build
VannestBun Feb 27, 2025
c098c71
Merge pull request #4 from VannestBun/addtests
VannestBun Feb 27, 2025
594c5da
update go version
VannestBun Feb 27, 2025
be41d76
added GCP automate builds
VannestBun Feb 27, 2025
48659af
add auth to fix build
VannestBun Feb 27, 2025
5349ab9
added auth before build
VannestBun Feb 27, 2025
ab990a8
change index file to Welcome to Notely
VannestBun Feb 27, 2025
ab5ed11
remove . in deployment
VannestBun Feb 27, 2025
09f92c4
add automatic migration
VannestBun Feb 27, 2025
0ff7991
fix path of db migration
VannestBun Feb 27, 2025
679281d
fix path for migration
VannestBun Feb 27, 2025
8a052b4
fix path
VannestBun Feb 27, 2025
765bcb6
Trigger workflow
VannestBun Feb 27, 2025
dbb36b9
use migration up
VannestBun Feb 27, 2025
4c2fa74
correct path to migrateup.sh
VannestBun Feb 27, 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
50 changes: 50 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Deployment

on:
push:
branches: [main]
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
timeout-minutes: 30

env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install goose
run: |
go install github.com/pressly/goose/v3/cmd/goose@latest

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.22"

- name: Build
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: Build and push image to Google Cloud
run: |
gcloud builds submit --tag us-central1-docker.pkg.dev/notely-452202/notely-ar-repo/notely:latest .

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

- name: Deploy to Cloud Run
run: gcloud run deploy notely --image us-central1-docker.pkg.dev/notely-452202/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project notely-452202 --max-instances=4
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: ci

on:
pull_request:
branches: [main]

jobs:
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: Check Format
run: test -z $(go fmt ./...)

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

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: Check go's version
run: go test ./... -cover

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

- name: security check
run: gosec ./...
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Build Status](https://github.com/vannestbun/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)

# 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 @@ -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!

Vannest's version of Boot.dev's Notely app, add more a bit
43 changes: 43 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package auth

import (
"net/http"
"testing"
)

func TestGetAPIKey(t *testing.T) {
testCases := []struct {
name string
headers http.Header
expectedAPIKey string
expectedError error
}{
{
name: "Valid API Key",
headers: http.Header{"Authorization": {"ApiKey valid-api-key"}},
expectedAPIKey: "valid-api-key",
expectedError: nil,
},
{
name: "No Authorization Header",
headers: http.Header{},
expectedAPIKey: "",
expectedError: ErrNoAuthHeaderIncluded,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apiKey, err := GetAPIKey(tc.headers)

if tc.expectedError != nil && err == nil {
t.Errorf("Test Case: %s - Expected error, but got none", tc.name)
return // Stop if error was expected but not received
}

if apiKey != tc.expectedAPIKey {
t.Errorf("Test Case: %s - API Key - Expected '%s', got '%s'", tc.name, tc.expectedAPIKey, apiKey)
}
})
}
}
6 changes: 5 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,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.Printf("Error marshalling JSON: %s", 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,
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: 30 * time.Second,
}

log.Printf("Serving on port: %s\n", port)
Expand Down
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>

<body class="section">
<h1>Notely</h1>
<h1>Welcome to Notely</h1>

<div id="userCreationContainer" class="section">
<input id="nameField" type="text" placeholder="Enter your name">
Expand Down