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
18 changes: 18 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
pull_request:
branches: [main]

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: "1.26.0"
- name: Build
run: ./scripts/buildprod.sh
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: ci

on:
pull_request:
branches: [main]

jobs:
tests:
name: Tests
runs-on: ubuntu-latest

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

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

- name: Run Tests
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@v6

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

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

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

- name: Run staticcheck
run: staticcheck ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ out
.env
learn-cicd-starter
notely
node_modules
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ go build -o notely && ./notely

*This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`.

vadhe version of Boot.dev's Notely app.
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!


![alt text goes here](https://github.com/vadhe/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)
7 changes: 6 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
return
}
w.WriteHeader(code)
w.Write(dat)
_, err = w.Write(dat)
if err != nil {
// Tangani error, misalnya log atau kirim internal server error
log.Printf("Gagal menulis data ke response: %v", err)
}

}
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"strconv"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand Down Expand Up @@ -34,6 +35,10 @@ func main() {
if port == "" {
log.Fatal("PORT environment variable is not set")
}
portInt, err := strconv.Atoi(port)
if err != nil {
log.Fatal("Format port tidak valid")
}

apiCfg := apiConfig{}

Expand Down Expand Up @@ -89,10 +94,11 @@ func main() {

router.Mount("/v1", v1Router)
srv := &http.Server{
Addr: ":" + port,
Handler: router,
Addr: ":" + port,
Handler: router,
ReadTimeout: 60 * 60 * 60,
}

log.Printf("Serving on port: %s\n", port)
log.Printf("Serving on port: %d\n", portInt)
log.Fatal(srv.ListenAndServe())
}
43 changes: 43 additions & 0 deletions middleware_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/bootdotdev/learn-cicd-starter/internal/database"
)

func TestAuthMiddleware(t *testing.T) {
tests := []struct {
name string
token string
expected int
}{
{
name: "invalid token",
token: "Bearer invalid",
expected: http.StatusUnauthorized,
},
}

apiCfg := apiConfig{}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", tt.token)
rr := httptest.NewRecorder()

nextHandler := func(w http.ResponseWriter, r *http.Request, user database.User) {
w.WriteHeader(http.StatusOK)
}
handler := apiCfg.middlewareAuth(nextHandler)
handler.ServeHTTP(rr, req)

if rr.Code != tt.expected {
t.Errorf("expected status code %d, got %d", tt.expected, rr.Code)
}
})
}
}