diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000000..3d9f2c04f50 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,22 @@ +name: cd + +on: + push: + branches: [main] + +jobs: + Deploy: + name: Deploy + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Build production binary + run: ./scripts/buildprod.sh \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..81fff5b17f5 --- /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@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Run tests + run: go test ./... + + - 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: + - name: Check out code + uses: actions/checkout@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Check formatting + run: test -z $(go fmt ./...) + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Run staticcheck + run: staticcheck ./... \ No newline at end of file diff --git a/README.md b/README.md index c2bec0368b7..a96241fca77 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +![Tests](https://github.com/coderyogesh27/learn-cicd-starter/actions/workflows/ci.yml/badge.svg) + +# Notely + +... # 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). @@ -21,3 +26,4 @@ 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! +"Yogesh Jangid's version of Boot.dev's Notely app." \ No newline at end of file diff --git a/internal/auth/get_api_key_test.go b/internal/auth/get_api_key_test.go new file mode 100644 index 00000000000..ad2aab56154 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,45 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + tests := []struct { + name string + headers http.Header + want string + wantErr bool + }{ + { + name: "valid API key", + headers: http.Header{ + "Authorization": []string{"ApiKey test-api-key"}, + }, + want: "test-api-key", + wantErr: false, + }, + { + name: "missing API key", + headers: http.Header{}, + want: "", + 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.want { + t.Errorf("GetAPIKey() = %q, want %q", got, tt.want) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e18..1471f953a2d 100644 --- a/json.go +++ b/json.go @@ -13,9 +13,11 @@ func respondWithError(w http.ResponseWriter, code int, msg string, logErr error) if code > 499 { log.Printf("Responding with 5XX error: %s", msg) } + type errorResponse struct { Error string `json:"error"` } + respondWithJSON(w, code, errorResponse{ Error: msg, }) @@ -23,12 +25,17 @@ 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) if err != nil { log.Printf("Error marshalling JSON: %s", err) - w.WriteHeader(500) + w.WriteHeader(http.StatusInternalServerError) return } + w.WriteHeader(code) - w.Write(dat) + + if _, err := w.Write(dat); err != nil { + log.Printf("Error writing JSON response: %s", err) + } } diff --git a/main.go b/main.go index 19d7366c5f7..c24ae1a2630 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "log" "net/http" "os" + "strconv" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -35,6 +37,11 @@ func main() { log.Fatal("PORT environment variable is not set") } + portNum, err := strconv.Atoi(port) + if err != nil || portNum < 1 || portNum > 65535 { + log.Fatal("PORT must be a valid port number") + } + apiCfg := apiConfig{} // https://github.com/libsql/libsql-client-go/#open-a-connection-to-sqld @@ -71,6 +78,7 @@ func main() { return } defer f.Close() + if _, err := io.Copy(w, f); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -88,11 +96,13 @@ func main() { v1Router.Get("/healthz", handlerReadiness) 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) + log.Printf("Serving on port: %d\n", portNum) log.Fatal(srv.ListenAndServe()) }