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
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: Check out code
uses: actions/checkout@v4

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

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.26.0"

- name: build
run: ./scripts/buildprod.sh

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

- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v3'
with:
version: '>= 363.0.0'

- name: docker
run: gcloud builds submit --tag us-central1-docker.pkg.dev/solid-binder-491221-g5/notely-ar-repo/notely:latest .

- name: migrate
run: ./scripts/migrateup.sh

- name: Deploy to Cloud Run
run: gcloud run deploy notely --image us-central1-docker.pkg.dev/solid-binder-491221-g5/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project solid-binder-491221-g5 --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: Check out code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.26.0"

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

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

- name: gosec test
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.26.0"

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

- name: style
run: test -z $(go fmt ./...)

- name: staticcheck
run: staticcheck ./...

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
out
.env
learn-cicd-starter
notely

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)

![badge](https://github.com/dylandeyotte/learn-cicd-starter/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!

Dylan's version of Boot.dev's Notely app.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tursodatabase/libsql-client-go v0.0.0-20240220085343-4ae0eb9d0898 h1:1MvEhzI5pvP27e9Dzz861mxk9WzXZLSJwzOU67cKTbU=
github.com/tursodatabase/libsql-client-go v0.0.0-20240220085343-4ae0eb9d0898/go.mod h1:9bKuHS7eZh/0mJndbUOrCx8Ej3PlsRDszj4L7oVYMPQ=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
Expand Down
55 changes: 55 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package auth

import (
"fmt"
"net/http"
"strings"
"testing"
)

func TestAPIKey(t *testing.T) {
tests := []struct {
key string
value string
expect string
err string
}{
{
err: "no authorization header",
},
{
key: "Authorization",
err: "no authorization header",
},
{
key: "Authorization",
value: "h293hr928h3",
err: "malformed authorization header",
},
{
key: "Authorization",
value: "ApiKey hf29hd923",
expect: "hf29hd923",
err: "none",
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("TestGetAPIKey Case #%v:", i), func(t *testing.T) {
header := http.Header{}
header.Add(test.key, test.value)

output, err := GetAPIKey(header)
if err != nil {
if strings.Contains(err.Error(), test.err) {
return
}
t.Error(err)
return
}
if output != test.expect {
t.Error(err)
return
}
})
}
}
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: %v", err)
return
}
}
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

// for the change
import (
"database/sql"
"embed"
"io"
"log"
"net/http"
"os"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand All @@ -24,17 +26,14 @@ type apiConfig struct {
//go:embed static/*
var staticFiles embed.FS

const port = "8080"

func main() {
err := godotenv.Load(".env")
if err != nil {
log.Printf("warning: assuming default configuration. .env unreadable: %v", err)
}

port := os.Getenv("PORT")
if port == "" {
log.Fatal("PORT environment variable is not set")
}

apiCfg := apiConfig{}

// https://github.com/libsql/libsql-client-go/#open-a-connection-to-sqld
Expand Down Expand Up @@ -89,10 +88,14 @@ func main() {

router.Mount("/v1", v1Router)
srv := &http.Server{
Addr: ":" + port,
Handler: router,
Addr: ":" + port,
Handler: router,
ReadHeaderTimeout: time.Second * 30,
}

log.Printf("Serving on port: %s\n", port)
log.Fatal(srv.ListenAndServe())
err = srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
Binary file added notely
Binary file not shown.
6 changes: 3 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

<head>
<meta charset="UTF-8">
<title>Notely</title>
<title>Welcome to Notely</title>
</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>