Skip to content

Commit 2f95e7b

Browse files
feat: add automation workflow with github action
1 parent bda729b commit 2f95e7b

File tree

13 files changed

+103
-121
lines changed

13 files changed

+103
-121
lines changed

.github/workflows/audit.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Audit
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
audit:
11+
runs-on: ubuntu-20.04
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: 1.23.4
19+
20+
- name: Verify Dependencies
21+
run: go mod verify
22+
23+
- name: Build
24+
run: go build -v ./...
25+
26+
- name: Run go vet
27+
run: go vet ./...
28+
29+
- name: Install staticcheck
30+
run: go install honnef.co/go/tools/cmd/staticcheck@latest
31+
32+
- name: Run staticcheck
33+
run: staticcheck ./...
34+
35+
- name: Run Tests
36+
run: go test -race ./...

.vscode/settings.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

app.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FILE: 2025/02/11 02:24:08 Log entry written to file.

cmd/api/api.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"blogsapi/internal/store" //this is required to generate swagger docs
88
"context"
99
"errors"
10+
"expvar"
1011
"fmt"
1112
"net/http"
1213
"os"
@@ -16,6 +17,7 @@ import (
1617

1718
"github.com/go-chi/chi/v5"
1819
"github.com/go-chi/chi/v5/middleware"
20+
"github.com/go-chi/cors"
1921
httpSwagger "github.com/swaggo/http-swagger/v2"
2022
"go.uber.org/zap"
2123
)
@@ -79,12 +81,26 @@ func (app *application) mount() http.Handler {
7981
r.Use(middleware.Logger)
8082
r.Use(middleware.Recoverer)
8183

84+
// Basic CORS
85+
// for more ideas, see: https://developer.github.com/v3/#cross-origin-resource-sharing
86+
r.Use(cors.Handler(cors.Options{
87+
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
88+
AllowedOrigins: []string{"https://*", "http://*"},
89+
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
90+
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
91+
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
92+
ExposedHeaders: []string{"Link"},
93+
AllowCredentials: false,
94+
MaxAge: 300, // Maximum value not ignored by any of major browsers
95+
}))
96+
8297
//Set a timeout value on the request context (ctx), that will signal through ctx.Done() that the request has timed out and further processing should be stopped
8398
r.Use(middleware.Timeout(60 * time.Second))
8499

85100
r.Route("/v1", func(r chi.Router) {
101+
//Operations
86102
r.With(app.BasicAuthMiddleware()).Get("/health", app.healthCheckHandler)
87-
103+
r.With(app.BasicAuthMiddleware()).Get("/debug/vars", expvar.Handler().ServeHTTP)
88104
//dynamically configuring this since later we will have development env, staging env, and production env.
89105
docsURL := fmt.Sprintf("%s/swagger/doc.json", app.config.addr)
90106
r.Get("/swagger/*", httpSwagger.Handler(httpSwagger.URL(docsURL)))
@@ -141,6 +157,7 @@ func (app *application) run(mux http.Handler) error {
141157
IdleTimeout: time.Minute,
142158
}
143159

160+
// Implementing graceful shutdown
144161
shutdown := make(chan error)
145162

146163
go func() {

cmd/api/json.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99

1010
var Validate *validator.Validate
1111

12+
// init is special function in Go. it is used for package initialization and has some unique properties. it is executed before main(), without explicitly called.
13+
// each package can have multiple init() func, but they run only once when the package is imported.
14+
// init() func in imported package run before the init() of the main package
15+
// init() cannot take parameters or return values
16+
1217
func init() {
1318
Validate = validator.New(validator.WithRequiredStructEnabled())
1419
}

cmd/api/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"blogsapi/internal/db"
66
"blogsapi/internal/mailer"
77
"blogsapi/internal/store"
8+
"expvar"
89
"log"
910
"os"
11+
"runtime"
1012
"strconv"
1113
"time"
1214

@@ -112,6 +114,15 @@ func main() {
112114
authenticator: jwtAuthenticator,
113115
}
114116

117+
//Metrics collected
118+
expvar.NewString("version").Set(version)
119+
expvar.Publish("database", expvar.Func(func() any {
120+
return db.Stats()
121+
}))
122+
expvar.Publish("goroutines", expvar.Func(func() any {
123+
return runtime.NumGoroutine()
124+
}))
125+
115126
mux := app.mount()
116127

117128
logger.Fatal(app.run(mux))

cmd/api/users.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"blogsapi/internal/store"
5-
"context"
65
"net/http"
76
"strconv"
87

@@ -137,31 +136,31 @@ func (app *application) activateUserHandler(w http.ResponseWriter, r *http.Reque
137136
}
138137
}
139138

140-
func (app *application) userContextMiddleware(next http.Handler) http.Handler {
141-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
142-
userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
143-
if err != nil {
144-
app.badRequestResponse(w, r, err)
145-
return
146-
}
147-
ctx := r.Context()
148-
user, err := app.store.Users.GetByID(ctx, userID)
149-
if err != nil {
150-
switch err {
151-
case store.ErrNotFound:
152-
app.notFoundResponse(w, r, err)
153-
return
154-
default:
155-
app.internalServerError(w, r, err)
156-
return
157-
}
158-
}
159-
160-
ctx = context.WithValue(ctx, userCtx, user)
161-
next.ServeHTTP(w, r.WithContext(ctx))
162-
163-
})
164-
}
139+
// func (app *application) userContextMiddleware(next http.Handler) http.Handler {
140+
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
141+
// userID, err := strconv.ParseInt(chi.URLParam(r, "userID"), 10, 64)
142+
// if err != nil {
143+
// app.badRequestResponse(w, r, err)
144+
// return
145+
// }
146+
// ctx := r.Context()
147+
// user, err := app.store.Users.GetByID(ctx, userID)
148+
// if err != nil {
149+
// switch err {
150+
// case store.ErrNotFound:
151+
// app.notFoundResponse(w, r, err)
152+
// return
153+
// default:
154+
// app.internalServerError(w, r, err)
155+
// return
156+
// }
157+
// }
158+
159+
// ctx = context.WithValue(ctx, userCtx, user)
160+
// next.ServeHTTP(w, r.WithContext(ctx))
161+
162+
// })
163+
// }
165164

166165
func getUserFromContext(r *http.Request) *store.User {
167166
user, _ := r.Context().Value(userCtx).(*store.User)

cmd/migrate/seed/main.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ require (
1010
github.com/swaggo/http-swagger/v2 v2.0.2
1111
)
1212

13-
require github.com/sendgrid/rest v2.6.9+incompatible // indirect
13+
require (
14+
github.com/go-chi/cors v1.2.1 // indirect
15+
github.com/sendgrid/rest v2.6.9+incompatible // indirect
16+
)
1417

1518
require (
1619
github.com/KyleBanks/depth v1.2.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3G
66
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
77
github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0=
88
github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
9+
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
10+
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
911
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
1012
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
1113
github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ=

0 commit comments

Comments
 (0)