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

Use oapi-codegen for generate server code, change mux -> chi #13

Merged
merged 1 commit into from
Jun 14, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ attach-db:

# Generate Go server boilerplate from OpenAPI 3
codegen:
oapi-codegen -generate models -o internal/app/apimodels/models.gen.go --package models api/swagger.yaml
oapi-codegen -generate models,chi -o internal/app/server/server.gen.go --package server api/swagger.yaml
106 changes: 44 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,52 @@ Roadmap [here](TODO.md)
├── config
│   ├── config.go
│   └── config.yml
├── internal
│   └── app
│   ├── api # backend handlers
│   │   ├── games.go
│   │   ├── results.go
│   │   ├── services.go
│   │   ├── session.go
│   │   ├── teams.go
│   │   ├── university.go
│   │   └── users.go
│   ├── apimodels # autogenerated code from oapi-codegen
│   │   └── models.go
│   ├── db # wrapper db to struct
│   │   ├── game.go
│   │   ├── result.go
│   │   ├── service.go
│   │   ├── team.go
│   │   ├── university.go
│   │   └── user.go
│   ├── logger
│   │   └── logger.go
│   ├── repository # work with db, query ...
│   │   ├── game.go
│   │   ├── result.go
│   │   ├── service.go
│   │   ├── session.go
│   │   ├── team.go
│   │   ├── university.go
│   │   └── user.go
│   ├── routers
│   │   └── routers.go
│   ├── utils # shared code
│   │   └── helpers.go
│   ├── view # api json template
│   │   ├── game.go
│   │   ├── result.go
│   │   ├── service.go
│   │   ├── team.go
│   │   ├── university.go
│   │   └── user.go
│   └── web # frontend handlers
│   └── handlers.go
├── migrations
│   ├── init.sql # schema DB
│   └── seed
│   └── *.sql # seed data for DB
└── web # frontend
├── static
│   └── index.css # unused
└── templates # html pages
├── games
│   └── index.html
├── index.html
├── services
│   └── index.html
├── teams
│   └── index.html
└── users
└── index.html
├── html
└── internal
   └── app
   ├── database
   ├── db
   │   ├── game.go
   │   ├── result.go
   │   ├── service.go
   │   ├── team.go
   │   ├── university.go
   │   └── user.go
   ├── handlers # backend handlers
   │   ├── games.go
   │   ├── interface.go
   │   ├── results.go
   │   ├── services.go
   │   ├── sessions.go
   │   ├── teams.go
   │   ├── universities.go
   │   └── users.go
   ├── logger
   │   └── logger.go
   ├── repository # work with db, query ...
   │   ├── game.go
   │   ├── result.go
   │   ├── service.go
   │   ├── session.go
   │   ├── team.go
   │   ├── university.go
   │   └── user.go
   ├── server # autogenerated code from oapi-codegen
   │   └── server.gen.go
   ├── utils # shared code
   │   └── helpers.go
   └── view # api json template
   ├── game.go
   ├── result.go
   ├── service.go
   ├── team.go
   ├── university.go
   └── user.go
```

## Development

### Install requriments
### Install requirements

```shell
$ go mod download && go mod tidy
Expand All @@ -103,7 +85,7 @@ will be available on - [http://localhost:4102](http://localhost:4102)
### Generate code from swagger schema

```shell
oapi-codegen -generate models -o internal/app/apimodels/models.go --package models api/swagger.yaml
oapi-codegen -generate models,chi -o internal/app/server/server.gen.go --package routers api/swagger.yaml
```

## DataBase
Expand Down Expand Up @@ -132,7 +114,7 @@ $ docker run -d --name ctf01d-postgres -e POSTGRES_DB=ctf01d -e POSTGRES_USER=po
$ docker exec -it ctf01d-postgres psql -U postgres
```

## Experemental
## Experimental

### fuzz api

Expand Down
4 changes: 2 additions & 2 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ components:
type: object
properties:
id:
type: string
type: integer
description: The unique identifier for the user
user_name:
type: string
Expand Down Expand Up @@ -677,7 +677,7 @@ components:
type: object
properties:
id:
type: string
type: integer
description: Unique identifier for the game
start_time:
type: string
Expand Down
20 changes: 14 additions & 6 deletions cmd/ctf01d/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"ctf01d/config"
"ctf01d/internal/app/database"
"ctf01d/internal/app/routers"
"log/slog"
"net/http"
"os"

"ctf01d/config"
"ctf01d/internal/app/database"
"ctf01d/internal/app/handlers"
"ctf01d/internal/app/server"

"github.com/go-chi/chi/v5"
_ "github.com/lib/pq"
)

Expand All @@ -25,10 +28,15 @@ func main() {
return
}
slog.Info("Database connection established successfully")
router := routers.NewRouter(db)
slog.Info("Server started on http://" + cfg.HTTP.Host + ":" + cfg.HTTP.Port)
defer db.Close()
r := chi.NewRouter()
h := &handlers.Handlers{
DB: db,
}
s := handlers.NewServerInterfaceWrapper(h)

err = http.ListenAndServe(cfg.HTTP.Host+":"+cfg.HTTP.Port, router)
r.Mount("/", server.HandlerFromMux(s, r))
err = http.ListenAndServe(cfg.HTTP.Host+":"+cfg.HTTP.Port, r)
if err != nil {
slog.Error("Server error: " + err.Error())
}
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ module ctf01d
go 1.22.3

require (
github.com/go-chi/chi/v5 v5.0.12
github.com/gorilla/mux v1.8.1
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/lib/pq v1.2.0
golang.org/x/crypto v0.22.0
github.com/oapi-codegen/runtime v1.1.1
golang.org/x/crypto v0.24.0
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/go-chi/chi v1.5.5 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
30 changes: 27 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Loading
Loading