Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: cd

on:
push:
branches:
- main

jobs:
Deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: setup go
uses: actions/setup-go@v4
with:
go-version: 1.22.0

- name: build the app
run: ./scripts/buildprod.sh
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: ci

on:
pull_request:
branches:
- main

jobs:
Tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: setup go
uses: actions/setup-go@v4
with:
go-version: 1.22.0

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

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

- name: run gosec
run: gosec ./...

Style:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: setup go
uses: actions/setup-go@v4
with:
go-version: 1.26.0

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

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

- name: run staticcheck
run: staticcheck ./...



22 changes: 22 additions & 0 deletions .github/workflows/setup-gcloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: setup gcloud

on:
push:
branches:
- main

jobs:
job_id:
runs-on: ubuntu-latest
steps:

- 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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/amd64 debian:stable-slim
FROM debian:stable-slim

RUN apt-get update && apt-get install -y ca-certificates

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![.github/workflows/ci.yml](https://github.com/skwangawur/learn-cicd-starter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/skwangawur/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 @@ -21,3 +24,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!

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

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

func TestGetAPIKey(t *testing.T) {
tests := []struct {
name string
headers http.Header
want string
wantErr error
}{
{
name: "no authorization header",
headers: http.Header{},
want: "",
wantErr: ErrNoAuthHeaderIncluded,
},
{
name: "malformed authorization header",
headers: http.Header{
"Authorization": []string{"Bearer abc123"},
},
want: "",
wantErr: errors.New("malformed authorization header"),
},
{
name: "authorization header without api key",
headers: http.Header{
"Authorization": []string{"ApiKey"},
},
want: "",
wantErr: errors.New("malformed authorization header"),
},
{
name: "valid authorization header",
headers: http.Header{
"Authorization": []string{"ApiKey abc123"},
},
want: "abc123",
wantErr: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAPIKey(tt.headers)

if got != tt.want {
t.Errorf("GetAPIKey() got = %v, want %v", got, tt.want)
}

if err != nil && tt.wantErr == nil {
t.Errorf("GetAPIKey() unexpected error = %v", err)
}

if err != nil && tt.wantErr != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("GetAPIKey() error = %v, want %v", err, tt.wantErr)
}
})
}
}
3 changes: 1 addition & 2 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ func respondWithError(w http.ResponseWriter, code int, msg string, logErr error)

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
dat, err := json.Marshal(payload)
_, err := json.Marshal(payload)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.WriteHeader(code)
w.Write(dat)
}
8 changes: 5 additions & 3 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 All @@ -25,6 +26,7 @@ type apiConfig struct {
var staticFiles embed.FS

func main() {

err := godotenv.Load(".env")
if err != nil {
log.Printf("warning: assuming default configuration. .env unreadable: %v", err)
Expand Down Expand Up @@ -89,10 +91,10 @@ func main() {

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

log.Printf("Serving on port: %s\n", port)
log.Fatal(srv.ListenAndServe())
}