Skip to content
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

Pull test #66

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
48 changes: 48 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: cd

on:
push:
branches: [main]

jobs:
deploy:
name: deploy
runs-on: ubuntu-latest

env:
DATABASE_URL: '${{ secrets.DATABASE_URL }}'

steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Check out code
uses: actions/checkout@v3

- name: Build app
run: ./scripts/buildprod.sh

- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'

- name: 'Build and push Docker image'
run: gcloud builds submit --tag us-central1-docker.pkg.dev/notely-399022/notely-ar-repo/notely:latest .

- name: Install goose
run: go install github.com/pressly/goose/v3/cmd/goose@latest

- name: Debug DATABASE_URL
run: echo $DATABASE_URL

- name: Run migrations
run: ./scripts/migrateup.sh

- name: 'Deploy to Cloud Run'
run: gcloud run deploy notely --image us-central1-docker.pkg.dev/notely-399022/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project notely-399022 --max-instances=4
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: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Check out code
uses: actions/checkout@v3

- name: Run tests
run: go test ./... -cover

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

- name: Run Security
run: gosec ./...

style:
name: styles
runs-on: ubuntu-latest

steps:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: Check out code
uses: actions/checkout@v3

- name: Format all directories
run: test -z $(go fmt ./...)

- name: install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Lint all directories
run: staticcheck ./...
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# learn-cicd-starter (Notely)
![code coverage badge](https://github.com/raad-dego/lean-cicd-starter/actions/workflows/ci.yml/badge.svg)

# learn-cicd-starterx (Notely)

This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev).

Expand Down
43 changes: 43 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package auth

import (
"net/http"
"testing"
)

func TestGetAPIKeyValidHeader(t *testing.T) {
header := http.Header{}
header.Add("Authorization", "ApiKey my-api-key")

apiKey, err := GetAPIKey(header)

if err != nil {
t.Errorf("Expected no error, got %v", err)
}

expectedAPIKey := "my-api-key"
if apiKey != expectedAPIKey {
t.Errorf("Expected API key to be %s, but got %s", expectedAPIKey, apiKey)
}
}

func TestGetAPIKeyNoHeader(t *testing.T) {
headers := http.Header{}

_, err := GetAPIKey(headers)

if err == nil || err != ErrNoAuthHeaderIncluded {
t.Errorf("Expected ErrNoAuthHeaderIncluded error, got %v", err)
}
}

func TestGetAPIKeyMalformedHeader(t *testing.T) {
headers := http.Header{}
headers.Add("Authorization", "Bearer my-token")

_, err := GetAPIKey(headers)

if err == nil || err.Error() != "malformed authorization header" {
t.Errorf("Expected 'malformed authorization header' error, got %v", err)
}
}
8 changes: 7 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)

_, err = w.Write(dat)
if err != nil {
// Handle the error, log it, or return an appropriate response
http.Error(w, "Failed to write response", http.StatusInternalServerError)
return
}
}
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"strings"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand Down Expand Up @@ -93,12 +94,14 @@ func main() {

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.Println("Test2")
log.Fatal(srv.ListenAndServe())

}

func addParseTimeParam(input string) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>

<body class="section">
<h1>Notely</h1>
<h1>Welcome to Notely</h1>

<div id="userCreationContainer" class="section">
<input id="nameField" type="text" placeholder="Enter your name">
Expand Down Expand Up @@ -190,4 +190,4 @@ <h2>Your Notes</h2>
<!-- your existing CSS code... -->
</body>

</html>
</html>