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

Adding initial CI #4

Merged
merged 9 commits into from
Jul 19, 2024
Merged
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
26 changes: 26 additions & 0 deletions .github/workflows/delete-pr-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Delete Merged Branch

on:
pull_request:
types: [closed]

jobs:
run:
if: |
(
github.event.pull_request.merged == true &&
github.event.pull_request.head.ref != 'main'
)
name: Delete Merged PR Branch
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Delete merged branch
shell: bash
run: |
set -e -o pipefail
headRefName=$(gh pr view ${{ github.event.pull_request.number }} --json headRefName -q ".headRefName")
git push origin --delete ${headRefName}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test

on:
pull_request:
types: [opened, synchronize, reopened]

branches:
- 'main'

jobs:
build-and-test:
name: Lint And Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: 'go.work'
cache-dependency-path: 'go.work.sum'

- run: make lint
- run: make tests
5 changes: 3 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ linters:
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
- testableexamples # checks if examples are testable (have an expected output)
- testifylint # checks usage of github.com/stretchr/testify
- testpackage # makes you use a separate _test package
# - testpackage # makes you use a separate _test package
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- unconvert # removes unnecessary type conversions
- unparam # reports unused function parameters
Expand Down Expand Up @@ -351,4 +351,5 @@ issues:
- goconst
- gosec
- noctx
- wrapcheck
- wrapcheck
- gocognit
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @gemyago
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ bin/golangci-lint: ./.golangci-version
.PHONY: lint/golang
lint/golang: bin/golangci-lint
cd ./tests/golang && ../../bin/golangci-lint run --config ../../.golangci.yml ./...
cd ./examples/go-apigen-server && ../../bin/golangci-lint run --config ../../.golangci.yml ./...

.PHONY: lint
lint: lint/golang

.PHONY: tests/golang
tests/golang:
go test ./tests/golang/...
TZ=US/Alaska go test ./tests/golang/...

.PHONY: tests
tests: tests/golang
40 changes: 27 additions & 13 deletions examples/go-apigen-server/cmd/service/main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package main

import (
"log/slog"
"fmt"
"log"
"net/http"
"time"

"github.com/gemyago/apigen/examples/go-apigen-server/pkg/api/http/server"
"github.com/gemyago/apigen/examples/go-apigen-server/pkg/api/http/router"
"github.com/gemyago/apigen/examples/go-apigen-server/pkg/api/http/v1controllers"
"github.com/gemyago/apigen/examples/go-apigen-server/pkg/app"
)

func main() {
// Minimal implementation of the http server startup.
// Real world implementation will likely to be more advanced and have
// things like configuration loading, logging setup, some form of DI e.t.c

port := 8080
readHeaderTimeoutSec := 2
storage := app.NewStorage()
srv := server.NewHTTPServer(server.HTTPServerParams{
ServerCfg: server.ServerCfg{Port: 8080},
Handler: server.NewRouter(server.RoutesDeps{
PetsController: v1controllers.NewPetsController(
v1controllers.PetsControllerDeps{
Commands: app.NewCommands(app.CommandsDeps{Storage: storage}),
Queries: app.NewQueries(app.QueriesDeps{Storage: storage}),
},
),

// Generated routes need a controller implementation to process requests
petsController := v1controllers.NewPetsController(
v1controllers.PetsControllerDeps{
Commands: app.NewCommands(app.CommandsDeps{Storage: storage}),
Queries: app.NewQueries(app.QueriesDeps{Storage: storage}),
},
)

srv := &http.Server{
Addr: fmt.Sprintf("[::]:%d", port),
ReadHeaderTimeout: time.Duration(readHeaderTimeoutSec) * time.Second,
Handler: router.NewHandler(router.HandlerDeps{
PetsController: petsController,
}),
})
slog.Info("Starting server on port: 8080")
}
log.Println("Starting server on port:", port)
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
Expand Down
5 changes: 1 addition & 4 deletions examples/go-apigen-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/gemyago/apigen/examples/go-apigen-server

go 1.22

require (
github.com/go-chi/chi/v5 v5.1.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
)
require golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
2 changes: 0 additions & 2 deletions examples/go-apigen-server/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
83 changes: 83 additions & 0 deletions examples/go-apigen-server/pkg/api/http/router/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package router

import (
"errors"
"log"
"net/http"

"github.com/gemyago/apigen/examples/go-apigen-server/pkg/api/http/v1routes/handlers"
"github.com/gemyago/apigen/examples/go-apigen-server/pkg/app"
)

type httpRouter struct {
*http.ServeMux
}

func (httpRouter) PathValue(r *http.Request, paramName string) string {
return r.PathValue(paramName)
}

func (a httpRouter) HandleRoute(method, pathPattern string, h http.Handler) {
a.ServeMux.Handle(method+" "+pathPattern, h)
}

func handleActionError(_ *http.Request, w http.ResponseWriter, err error) {
code := 500
switch {
case errors.Is(err, app.ErrNotFound):
code = 404
case errors.Is(err, app.ErrConflict):
code = 409
}
w.WriteHeader(code)
log.Printf("Failed to process request: %v\n", err)
}

type responseWriterWrapper struct {
http.ResponseWriter
statusCode int
}

func (lrw *responseWriterWrapper) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}

// HandlerDeps holds dependencies of the generated routes
// usually controller implementations at least.
type HandlerDeps struct {
PetsController *handlers.PetsController
}

// NewHandler creates an minimal example implementation of the router handler
// based on the standard http.ServeMux.
func NewHandler(deps HandlerDeps) http.Handler {
mux := http.NewServeMux()

// Real world instance of the router handler will likely to have a more advanced setup
// below is just some simple access logs on requests processing
muxHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("GET %v %v %v\n", r.URL.String(), r.Proto, r.UserAgent())
defer func() {
if r := recover(); r != nil {
log.Println("Request panic", r)
}
}()
wrapper := &responseWriterWrapper{ResponseWriter: w, statusCode: http.StatusOK}
mux.ServeHTTP(wrapper, r)
log.Printf("%d %v %v\n", wrapper.statusCode, r.Method, r.URL.String())
})

// The httpApp provides a configuration layer of the generated routes
// and also serves as an adapter that allows using different router implementations
httpApp := handlers.NewHttpApp(
httpRouter{ServeMux: mux},
handlers.WithActionErrorHandler(handleActionError),
)

// Register generated Pets routes. There can be multiple different
// routes registered into the same httpApp instance.
handlers.RegisterPetsRoutes(deps.PetsController, httpApp)

return muxHandler
}
61 changes: 0 additions & 61 deletions examples/go-apigen-server/pkg/api/http/server/router.go

This file was deleted.

33 changes: 0 additions & 33 deletions examples/go-apigen-server/pkg/api/http/server/server.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func NewPetsController(deps PetsControllerDeps) *handlers.PetsController {
return handlers.BuildPetsController().
HandleListPets.With(deps.Queries.ListPets).
HandleCreatePet.With(deps.Commands.CreatePet).
HandleGetPetById.With(deps.Queries.GetPetById).
HandleGetPetById.With(deps.Queries.GetPetByID).
Finalize()
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading