Skip to content

Commit

Permalink
refactor: migrate API specs to OpenAPI 3.1.0 ♻️ (#12)
Browse files Browse the repository at this point in the history
* chore(docs): some comments and nicer descriptions in commands

* refactor: separate entry files into dev and prod build tags ♻️

* refactor: use go generate instead of multiple entry points

* feat: add graphql code generation to gen.go ✨

* chore: remove unused .vscode configs 🔥

* fix: exclude gen.go from build ignore

* refactor: make everything compatible with openapi 3 ♻️

* fix(test): registered routes ✅
  • Loading branch information
kareemmahlees authored Dec 27, 2024
1 parent f621bea commit dfecb91
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 871 deletions.
15 changes: 7 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
build:
@go build -o bin/
build_prod:
@go build -o bin/ -ldflags "-w -s"
run: build
@./bin/meta-x.exe --help
run:
@air

# make sure to run docker first
# WARNING: make sure to run docker first
test:
@go test ./... -race
swag:
@swag fmt
@swag init
graphql:
@go run github.com/99designs/gqlgen generate
generate:
@go generate
58 changes: 58 additions & 0 deletions cmd/gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"errors"
"io/fs"
"log"

graphqlAPI "github.com/99designs/gqlgen/api"
graphqlConfig "github.com/99designs/gqlgen/codegen/config"
"github.com/swaggo/swag/v2/format"
"github.com/swaggo/swag/v2/gen"
)

func main() {
log.Println("Formatting swagger comments...")
err := format.New().Build(&format.Config{
SearchDir: "./",
Excludes: "cmd,.vscode,.github,docs",
})
if err != nil {
log.Fatalf("Error formatting swagger comments: %v", err)
}

config := &gen.Config{
OutputDir: "./docs",
OutputTypes: []string{"json", "yaml"},
MainAPIFile: "./main.go",
SearchDir: "./",
PropNamingStrategy: "camelcase",
GenerateOpenAPI3Doc: true,
}

err = gen.New().Build(config)
if err != nil {
log.Fatalf("Error generating swagger docs: %v", err)
}

log.Println("Swagger documentation generated successfully!")

cfg, err := graphqlConfig.LoadConfigFromDefaultLocations()
if errors.Is(err, fs.ErrNotExist) {
cfg, err = graphqlConfig.LoadDefaultConfig()
}
if err != nil {
log.Fatalf("Error while loading GraphQL config: %v", err)
return
}

log.Println("Generating GraphQL code...")

err = graphqlAPI.Generate(cfg)
if err != nil {
log.Fatalf("Error while generating GraphQL code: %v", err)
return
}

log.Println("Successfully generated GraphQL code!")
}
2 changes: 1 addition & 1 deletion cmd/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var mysqlCommand = &cobra.Command{
Use: "mysql",
Short: "use mysql as the database provider",
Short: "Use MySQL as the database provider",
RunE: func(cmd *cobra.Command, args []string) error {
var mysqlConfig *utils.MySQLConfig

Expand Down
3 changes: 2 additions & 1 deletion cmd/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

var pgCommand = &cobra.Command{
Use: "pg",
Short: "use postgres as the database provider",
Short: "Use Postgres as the database provider",
RunE: func(cmd *cobra.Command, args []string) error {
var pgConfig *utils.PgConfig

// Not handling the error because port always has a default value, `5522`.
port, _ := cmd.Flags().GetInt("port")

connUrl, _ := cmd.Flags().GetString("url")
Expand Down
9 changes: 4 additions & 5 deletions cmd/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (

var sqlite3Command = &cobra.Command{
Use: "sqlite3",
Short: "use sqlite as the database provider",
Short: "Use SQLite as the database provider",
RunE: func(cmd *cobra.Command, args []string) error {
filePath, err := cmd.Flags().GetString("file")
if err != nil {
return err
}
port, err := cmd.Flags().GetInt("port")
if err != nil {
return err
}
// Not handling the error because port always has a default value, `5522`.
port, _ := cmd.Flags().GetInt("port")

sqliteConfig := utils.NewSQLiteConfig(filePath)

conn, err := db.InitDBConn(lib.SQLITE3, sqliteConfig)
Expand Down
Loading

0 comments on commit dfecb91

Please sign in to comment.