diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000000..dee7fbf504 --- /dev/null +++ b/.github/workflows/cd.yml @@ -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 + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..f0fb228065 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 ./... \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..e396bc75d6 --- /dev/null +++ b/.vscode/settings.json @@ -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 + } + } + \ No newline at end of file diff --git a/README.md b/README.md index c2bec0368b..b47dfd3948 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..01e949adbc --- /dev/null +++ b/internal/auth/auth_test.go @@ -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) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e1..1cd828132d 100644 --- a/json.go +++ b/json.go @@ -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) + } + } diff --git a/main.go b/main.go index 19d7366c5f..f54e077dde 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -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)