Skip to content

readme edit #1400

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 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
05c4662
readme edit
Galaxybigbird Apr 15, 2025
4b5fcec
Add CI workflow to force failure for test PR
Galaxybigbird Apr 15, 2025
aec1f50
Update CI workflow to show Go version and succeed
Galaxybigbird Apr 15, 2025
ccdb380
Manual changes: update workflow with job name, Go 1.23.0, and force f…
Galaxybigbird Apr 15, 2025
b555311
feat: add ci workflow with go version check
Galaxybigbird Apr 15, 2025
a2280b5
fix: update Go version in CI workflow
Galaxybigbird Apr 15, 2025
b90399d
fix: update Go version in CI workflow again
Galaxybigbird Apr 15, 2025
fb58a74
Intentionally break a test to verify CI failure
Galaxybigbird Apr 16, 2025
3d75f61
Fix test to expect correct API key value
Galaxybigbird Apr 16, 2025
2f914e1
Print code coverage in CI with go test -cover
Galaxybigbird Apr 16, 2025
a0ea969
Add CI status badge to README
Galaxybigbird Apr 16, 2025
52ea4c3
Add style job to CI for formatting checks
Galaxybigbird Apr 16, 2025
855ab8e
Add staticcheck linting to style job in CI
Galaxybigbird Apr 16, 2025
2f418e1
Add debug step and set working-directory for staticcheck in CI
Galaxybigbird Apr 16, 2025
550bbce
Add gosec security checks to tests job in CI
Galaxybigbird Apr 16, 2025
99474b4
Fix gosec security issues: add ReadHeaderTimeout and handle w.Write e…
Galaxybigbird Apr 16, 2025
51ae23d
Format Go code to pass CI style check
Galaxybigbird Apr 16, 2025
9c07541
Format Go code to pass CI style check
Galaxybigbird Apr 16, 2025
4c1dec5
Add CD workflow for deployment on main branch
Galaxybigbird Apr 16, 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
1 change: 1 addition & 0 deletions .github/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

18 changes: 18 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Continuous Deployment

on:
push:
branches: [main]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Build app
run: scripts/buildprod.sh
57 changes: 57 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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.21.x" # Try a different Go version

- name: Run tests
run: go test -cover ./...

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

- name: Run gosec
run: ~/go/bin/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.21.x"

- name: Check formatting
run: test -z "$(go fmt ./...)"

- name: List files
run: ls -R
working-directory: ${{ github.workspace }}

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
working-directory: ${{ github.workspace }}

- name: Run staticcheck
run: ~/go/bin/staticcheck ./...
working-directory: ${{ github.workspace }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![CI Test Status](https://github.com/Galaxybigbird/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!

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

import (
"net/http"
"testing"
)

func TestGetAPIKey_ValidHeader(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "ApiKey testkey123")
key, err := GetAPIKey(headers)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if key != "testkey123" {
t.Errorf("expected 'testkey123', got '%s'", key)
}
}

func TestGetAPIKey_MissingHeader(t *testing.T) {
headers := http.Header{}
_, err := GetAPIKey(headers)
if err == nil {
t.Error("expected error for missing header, got nil")
}
}

func TestGetAPIKey_MalformedHeader(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "Bearer sometoken")
_, err := GetAPIKey(headers)
if err == nil {
t.Error("expected error for malformed header, got nil")
}

headers.Set("Authorization", "ApiKey")
_, err = GetAPIKey(headers)
if err == nil {
t.Error("expected error for incomplete ApiKey header, got nil")
}
}
5 changes: 4 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)
if _, err := w.Write(dat); err != nil {
// Optionally log the error
// 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,
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: 5 * time.Second,
}

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