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
17 changes: 17 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: cd

on:
push:
branches: [main]

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Build app
run: ./scripts/buildprod.sh
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: ci

on:
pull_request:
branches: [main]
push:
branches: [main]

jobs:
test:
name: Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Run unit tests
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:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Check code formatting
run: test -z $(go fmt ./...)
- name: Run staticcheck
run: staticcheck ./...
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Tests](https://github.com/stanisram/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 @@ -9,7 +11,7 @@ Make sure you're on Go version 1.22+.
Create a `.env` file in the root of the project with the following contents:

```bash
PORT="8080"
PORT="8080"
```

Run the server:
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!

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

import (
"net/http"
"testing"
)

func TestGetAPIKey(t *testing.T) {
tests := []struct {
name string
headers http.Header
wantAPIKey string
wantErr bool
}{
{
name: "Valid API Key",
headers: http.Header{"Authorization": []string{"ApiKey my-secret-key"}},
wantAPIKey: "my-secret-key",
wantErr: false,
},
{
name: "Missing Authorization Header",
headers: http.Header{},
wantAPIKey: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAPIKey(tt.headers)
if (err != nil) != tt.wantErr {
t.Errorf("GetAPIKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.wantAPIKey {
t.Errorf("GetAPIKey() = %v, want %v", got, tt.wantAPIKey)
}
})
}
}
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)
_, _ = w.Write(dat)
}
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package main
import (
"database/sql"
"embed"
"errors"
"io"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand All @@ -24,7 +27,17 @@ type apiConfig struct {
//go:embed static/*
var staticFiles embed.FS

func loadEnv() {
_, err := os.Stat(".env")
if errors.Is(err, os.ErrNotExist) {
log.Println("Warning: .env file not found, continuing with defaults")
return
}
}

func main() {
loadEnv()

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

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

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