Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
aabbcb8
Erste Teständerungen im Branch doris-tests
DorisLederle Sep 22, 2025
88aacd4
Update README
DorisLederle Sep 23, 2025
8dc2dc5
Add CI workflow
DorisLederle Sep 23, 2025
0e0e9e3
Update CI to print Go version
DorisLederle Sep 23, 2025
707954b
Adding testfile and broken code for testing
DorisLederle Sep 26, 2025
7539cbc
Changed ci.yml - make it work
DorisLederle Sep 26, 2025
20f381b
Break GetAPIKey for CI test
DorisLederle Sep 26, 2025
b8942e7
Fixed GetAPIKey
DorisLederle Sep 26, 2025
a2a799c
Prepare vor CH2LS3
DorisLederle Sep 26, 2025
5e007c9
Fix code and add coverage step
DorisLederle Sep 26, 2025
5b584be
Split test and coverage steps
DorisLederle Sep 26, 2025
555444b
added README Badge for Test
DorisLederle Sep 27, 2025
48946d9
Merge pull request #1 from DorisLederle/addtests
DorisLederle Sep 28, 2025
5014c16
Add Style job: Go Formatting
DorisLederle Sep 28, 2025
c996c20
Merge pull request #2 from DorisLederle/addtests
DorisLederle Sep 28, 2025
3e41616
Style-Job Formating: split into two steps
DorisLederle Sep 28, 2025
e6c4ca9
Style-Job Formating: split into two steps and reduced empty lines
DorisLederle Sep 28, 2025
da94518
Style-Job Formating: go formating, test -z
DorisLederle Sep 28, 2025
5f671fe
Style-Job Formating: go formating, test -z; after go formatting
DorisLederle Sep 28, 2025
89e568d
Added linting with staticcheck to ci.yml and unused function to main.go
DorisLederle Sep 29, 2025
aa7b3de
Add step staticcheck installation to Style job
DorisLederle Sep 30, 2025
02bf813
Unused fuction made to comment / removed
DorisLederle Sep 30, 2025
6f1a97f
Added 2 steps to Job Tests: -Install gosec and -Run gosec
DorisLederle Sep 30, 2025
40d804f
Fix gosec issues: add ReadHeaderTimeout and handle w.Write error
DorisLederle Sep 30, 2025
f5db4ef
Added new workflow cd.yml with Job 'Deploy'
DorisLederle Oct 2, 2025
cd8c63f
Added Dockerfile and changed main.go for Docker
DorisLederle Oct 2, 2025
504c796
Fix cd.yml: use run instead of uses for build script
DorisLederle Oct 2, 2025
962610f
Workflow-Trigger für Testphase auf push in addtests umgestellt.
DorisLederle Oct 2, 2025
9eda18f
Add GitHub Action to build and push Docker image
DorisLederle Oct 6, 2025
a122661
Merge pull request #3 from DorisLederle/addtests
DorisLederle Oct 6, 2025
352806b
main.yml Authentifizierung angepasst, Add GitHub Action to build and …
DorisLederle Oct 6, 2025
ca4b589
Merge pull request #4 from DorisLederle/addtests
DorisLederle Oct 6, 2025
0993819
Fix build workflow and add logging
DorisLederle Oct 6, 2025
5852eca
Merge pull request #5 from DorisLederle/addtests
DorisLederle Oct 6, 2025
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
25 changes: 25 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# new workflow for continuous deployment
# the ci workflow runs when a pull request is opened,
# but a cd workflow runs when a pull request is merged
# (or when code is pushed directly to the main branch).

on:
push:
branches: [addtests] # zum Testen [addtests], für Produktion: [main]

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

steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup Go toolchain
uses: actions/setup-go@v5
with:
go-version: "1.25.1"

- name: Build the app
run: ./scripts/buildprod.sh
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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: Run Go Tests
run: go test ./...

- name: Run Go test with coverage
run: go test -cover ./...

- 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@v4

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

- name: Run Test -z
run: |
test -z "$(go fmt ./...)"
go fmt ./...

#- name: Run Go Formatting
# run: go fmt ./...

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

- name: Run Go Linting
run: staticcheck ./...

39 changes: 39 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Die Datei, die bei jedem Push auf den main-Branch automatisch:
# 1. Mit GCP authentifiziert
# 2. Das Docker-Image baut
# 3. Es in die Artifact Registry hochlaedt

name: Build and Push to Artifact Registry

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}

- name: Set up gcloud CLI
uses: google-github-actions/setup-gcloud@v1
with:
project_id: tensile-core-474113-t6

- name: Show gcloud config
run: gcloud config list

- name: Who am I
run: gcloud auth list

- name: Build and push Docker image
run: |
gcloud builds submit --tag us-central1-docker.pkg.dev/tensile-core-474113-t6/notely-ar-repo/notely:latest .
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM --platform=linux/amd64 debian:stable-slim
#FROM --platform=linux/amd64 debian:stable-slim
FROM debian:stable-slim

RUN apt-get update && apt-get install -y ca-certificates

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# README Badge for Tests
[![ci](https://github.com/DorisLederle/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)](https://github.com/DorisLederle/learn-cicd-starter/actions/workflows/ci.yml)

<!--[![ci](https://github.com/DorisLederle/learn-cicd-starter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/DorisLederle/learn-cicd-starter/actions/workflows/ci.yml)-->
<!--[![ci](https://github.com/DorisLederle/learn-cicd-starter/actions/workflows/ci.yml/badge.svg?branch=main&event=status)](https://github.com/DorisLederle/learn-cicd-starter/actions/workflows/ci.yml)-->


# 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).
Expand All @@ -21,3 +28,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!

Doris Lederle's version of Boot.dev's Notely app.
2 changes: 2 additions & 0 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var ErrNoAuthHeaderIncluded = errors.New("no authorization header included")
// GetAPIKey -
func GetAPIKey(headers http.Header) (string, error) {
authHeader := headers.Get("Authorization")

if authHeader == "" {
return "", ErrNoAuthHeaderIncluded
}
Expand All @@ -20,4 +21,5 @@ func GetAPIKey(headers http.Header) (string, error) {
}

return splitAuth[1], nil
//return "wrong-key", nil //temporarily broken code for testing
}
84 changes: 84 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package auth

import (
"net/http"
"testing"
)

func TestGetAPIKey_ValidHeader(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "ApiKey abc123")
//headers.Set("Authorization", "abc123")

got, err := GetAPIKey(headers)
want := "abc123"
//want := "wrongkey"

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

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

_, err := GetAPIKey(headers)
if err != ErrNoAuthHeaderIncluded {
t.Errorf("Expected error %v, got %v", ErrNoAuthHeaderIncluded, err)
}
}

func TestGetAPIKey_MalformedHeader(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "Bearer abc123")

_, err := GetAPIKey(headers)
if err == nil || err.Error() != "malformed authorization header" {
t.Errorf("Expected malformed header error, got %v", err)
}
}

func TestGetAPIKey_EmptyKey(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "ApiKey ")

got, err := GetAPIKey(headers)
if err != nil {
t.Fatalf("Expected no error for empty key, got %v", err)
}
if got != "" {
t.Errorf("Expected empty string, got %q", got)
}
}

/*
func TestGetAPIKey_ExtraSpaces(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "ApiKey abc123")

got, err := GetAPIKey(headers)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if got != "abc123" {
t.Errorf("Expected 'abc123', got %q", got)
}
}


func TestGetAPIKey_MultipleParts(t *testing.T) {
headers := http.Header{}
headers.Set("Authorization", "ApiKey abc123 extra")

got, err := GetAPIKey(headers)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if got != "abc123" {
t.Errorf("Expected 'abc123', got %q", got)
}
}
*/
5 changes: 4 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)
//w.Write(dat) //gosec: Errorhandling hinzufügen
if _, err := w.Write(dat); err != nil {
log.Printf("error writing response: %v", err)
}
}
15 changes: 10 additions & 5 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 All @@ -17,9 +18,7 @@ import (
_ "github.com/tursodatabase/libsql-client-go/libsql"
)

type apiConfig struct {
DB *database.Queries
}
type apiConfig struct{ DB *database.Queries }

//go:embed static/*
var staticFiles embed.FS
Expand Down Expand Up @@ -89,10 +88,16 @@ func main() {

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

log.Printf("Serving on port: %s\n", port)
log.Fatal(srv.ListenAndServe())
}

/*func unused() {
// this function does nothing
// and is called nowhere
}*/
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test text