Skip to content

Add a note about Daniel's version of Boot.dev's Notely app to README #1449

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 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2ac797e
Add a note about Daniel's version of Boot.dev's Notely app to README
Peridan9 Apr 27, 2025
f8927e2
Add CI workflow for testing with Go 1.23.0
Peridan9 Apr 27, 2025
6512fa8
Remove Force Failure step and add Go version print step in CI workflow
Peridan9 Apr 27, 2025
cef2fea
made sure to exit with exit code 0
Peridan9 Apr 27, 2025
28e72e2
Rename step to echo Go version in CI workflow
Peridan9 Apr 27, 2025
86336e6
Add unit tests for GetAPIKey function and update CI workflow
Peridan9 Apr 27, 2025
e8244c1
Add coverage reporting to Go test step in CI workflow
Peridan9 Apr 27, 2025
7836371
Add newline at end of README.md and move CI badge to the bottom
Peridan9 Apr 27, 2025
2b227f8
Add Go style check to CI workflow and update main function formatting
Peridan9 Apr 28, 2025
955bc0b
Fix formatting of main function declaration
Peridan9 Apr 28, 2025
e24cd29
Add staticcheck to CI workflow and update main.go with unused function
Peridan9 Apr 28, 2025
9b3eaff
Fix formatting of unused function and add newline at end of file
Peridan9 Apr 28, 2025
412bdd2
Remove unused function from main.go
Peridan9 Apr 28, 2025
de23e9f
Add gosec installation and check to CI workflow
Peridan9 Apr 28, 2025
db847fe
Update gosec command in CI workflow to remove quiet flag and output r…
Peridan9 Apr 28, 2025
5619888
Enhance JSON response handling and add server timeouts for improved r…
Peridan9 Apr 28, 2025
87eee71
Add initial CI/CD workflow configuration for deployment
Peridan9 Apr 28, 2025
1eed26c
Fix job name casing in CI/CD workflow configuration
Peridan9 Apr 28, 2025
422f873
Fix path for build script in CI/CD workflow
Peridan9 Apr 28, 2025
2035237
Fix script path in CI/CD workflow for build step
Peridan9 Apr 28, 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
23 changes: 23 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: cd

on:
push:
branches: [main]

jobs:
Deploy:
name: Deploy
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: Build
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:
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: Test
run: go test -cover ./...

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

- name: Check gosec
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: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

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

- name: Check staticcheck
run: staticcheck ./...
30 changes: 30 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
// 1) absolutely no format-on-save in any language:
"editor.formatOnSave": false,

// 2) stop any code-actions on save (imports, formatting, etc):
"editor.codeActionsOnSave": {
"source.organizeImports": "never",
"source.formatDocument": "never"
},

// 3) now explicitly for Go files:
"[go]": {
// zero auto-formatting
"editor.formatOnSave": false,
// no default formatter
"editor.defaultFormatter": null,
// no import/format code actions
"editor.codeActionsOnSave": {
"source.organizeImports": "never",
"source.formatDocument": "never"
}
},

// 4) if you’re still using gopls, turn off its save-format hook:
"go.useLanguageServer": true,
"gopls": {
"ui.formatOnSave": false
}
}

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ 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!

Daniel's version of Boot.dev's Notely app.

![alt text goes here](https://github.com/peridan9/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)
63 changes: 63 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 header",
headers: http.Header{},
want: "",
wantErr: ErrNoAuthHeaderIncluded,
},
{
name: "wrong prefix",
headers: http.Header{"Authorization": []string{"Bearer token"}},
want: "",
wantErr: errors.New("malformed authorization header"),
},
{
name: "missing token",
headers: http.Header{"Authorization": []string{"ApiKey"}},
want: "",
wantErr: errors.New("malformed authorization header"),
},
{
name: "valid key",
headers: http.Header{"Authorization": []string{"ApiKey my-secret-key"}},
want: "my-secret-key",
wantErr: nil,
},
}

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

// error assertion
if tc.wantErr != nil {
if err == nil || err.Error() != tc.wantErr.Error() {
t.Fatalf("expected error %v, got %v", tc.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

// value assertion
if got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
})
}
}
6 changes: 5 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)
if _, err := w.Write(dat); err != nil {
// you can use your logger of choice here
log.Printf("respondJSON: failed to write response: %v", err)
}

}
9 changes: 7 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,12 @@ func main() {

router.Mount("/v1", v1Router)
srv := &http.Server{
Addr: ":" + port,
Handler: router,
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: 5 * time.Second, // give them 5s to send all headers
ReadTimeout: 10 * time.Second, // total time to read request
WriteTimeout: 10 * time.Second, // total time to write response
IdleTimeout: 120 * time.Second, // how long to keep idle connections alive
}

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