diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000000..961653fcde9 --- /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@v4 + + - name: Setting up Go + uses: actions/setup-go@v5 + with: + go-version: "1.25.1" + + - name: Build app + run: ./scripts/buildprod.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..af44eb84ebe --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,53 @@ +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.25.1" + + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Test security + run: gosec ./... + + - name: Run tests + run: go test ./... + + - name: Output coverage + run: go test ./... -cover + + styling: + 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.25.1" + + - name: Install static checker + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Check for formatting + run: test -z $(go fmt ./...) + + - name: Check for linting + run: staticcheck ./... diff --git a/Dockerfile b/Dockerfile index 2be3d18b812..bb611a09b47 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index c2bec0368b7..96fe1898182 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # learn-cicd-starter (Notely) +![ci](https://github.com/404errorg6/CI-CD-practice/actions/workflows/ci.yml/badge.svg) + This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev). ## Local Development @@ -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! + +404errorg6's version of Bootdev's Notely app diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 00000000000..6136a69a9cc --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,57 @@ +package auth + +import ( + "net/http" + "testing" +) + +type returnData struct { + apiKey string + err error +} + +func getTestHeaders() ([]http.Header, error) { + var headers []http.Header + headerStr := []string{ + "apikey ajsbdjabdjsakkndklsank", + "APIKEY fbjaskbjksabdbshj", + "apiKey asdklnaslkdnalks asndan asklndkla", + "apiKey asdnalkndklansd", + "ApiKey asdlaskl asndklandkl", + "", + "ApiKey This_is_correct_one", + } + + req, err := http.NewRequest("GET", "nothing", nil) + if err != nil { + return headers, err + } + + for _, s := range headerStr { + req.Header.Set("authorization", s) + headers = append(headers, req.Header) + } + return headers, nil +} + +func TestAuth(t *testing.T) { + var got returnData + headers, err := getTestHeaders() + if err != nil { + t.Fatalf("Error getting headers: %v", err) + } + + for i, h := range headers { + got.apiKey, got.err = GetAPIKey(h) + if got.err == nil { + if got.apiKey != "This_is_correct_one" { + t.Fatalf("expectedApi: This_is_correct_one expectedErr: nil\ngotApi: %v gotErr:%v", got.apiKey, got.err) + } + continue + } + if got.err == nil { + t.Fatalf("Test %v: expected error but got API instead: %v", i, got.apiKey) + } + } + +} diff --git a/json.go b/json.go index 1e6e7985e18..7e94055154a 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) + _, err = w.Write(dat) + if err != nil { + log.Printf("Error writing reponse: %v", err) + w.WriteHeader(500) + } } diff --git a/main.go b/main.go index 19d7366c5f7..433b667576b 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,9 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + ReadHeaderTimeout: time.Second * 3, + Addr: ":" + port, + Handler: router, } log.Printf("Serving on port: %s\n", port)