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
22 changes: 22 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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

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

404errorg6's version of Bootdev's Notely app
57 changes: 57 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

}
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)
_, err = w.Write(dat)
if err != nil {
log.Printf("Error writing reponse: %v", err)
w.WriteHeader(500)
}
}
6 changes: 4 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,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)
Expand Down