api-service-generator
is a CLI tool built with the Cobra CLI package that allows developers to quickly generate a basic Golang REST API service. The generated service uses the following packages:
- Gin Framework for building the API.
- IBM/alchemy-logging for logging.
- golang-migrate for database migrations.
- sqlc for generating type-safe Go code from SQL queries.
- Viper for configuration management.
Ensure the following commands are installed and available in your system's PATH:
- Go (1.16 or later)
- golang-migrate CLI
- sqlc CLI
- Docker
Clone the repository and build the CLI application:
git clone https://github.com/abhijithk1/api-service-generator.git
cd api-service-generator
go build -o api-service-generator
Move the api-service-generator
binary to a directory in your PATH.
mv api-service-generator /usr/local/bin/
Note: Can Skip the build and moving the binary process. Instead run:
go install
The template
subcommand is used to create a basic Golang REST API service. The CLI takes a single --name
flag and then prompts for the other inputs:
api-service-generator go-template --name myservice
The CLI will prompt you to enter the following details:
- Database Driver: Choose between postgres and mysql (Default:
postgres
) - Container Name: Name for the Docker container (Default:
dummy_db
) - Container Port: Port for the Docker container (Default:
6432
) - Database Name: Name of the database (Default:
dummy_db
) - Table Name: Name of the database table (Default:
api_table
) - API Group: API group for the generated service (Default:
dummy
) - Module Path: Base path for the Go module (Default:
example/api-service
)
- POSTGRES_USER: PostgreSQL user (Default:
postgres
) - POSTGRES_PASSWORD: PostgreSQL password (Default:
password
)
- MYSQL_ROOT_PASSWORD: MySQL root password (Default:
my-root-secret
) - MYSQL_USER: MySQL user (Default:
mysql
) - MYSQL_PASSWORD: MySQL password (Default:
password
)
The CLI will automatically spin up a Docker container based on the provided inputs and configure the API service to connect to it.
The generated project has the following structure:
<api-service>
| _ api
| | _ v1
| | _ <api_group>
| | _ controller.go
| | _ service.go
| | _ mw
| | _ cors.go
| | _ auth.go
| _ pkg
| | _ db
| | _ migrations
| | _ 000001_init_schema_up.sql
| | _ 000001_init_schema_down.sql
| | _ query
| | _ <table_name>.sql
| | _ connection.go
| | _ <table_name>.sql.go
| | _ migrate.go
| | _ main_test.go
| | _ db.go
| | _ models.go
| _ utils
| | _ config.go
| | _ utils.go
| _ go.mod
| _ go.sum
| _ main.go
| _ Makefile
| _ sqlc.yaml
| _ app.env
| _ api.http
- api/v1/<api_group>/: Contains the controller and service logic for the API group.
- api/v1/mw/: Middleware functions (e.g., CORS, authentication).
- pkg/db/: Database-related files, including migrations, queries, and connection setup.
- utils/: Utility functions and configuration handling.
- main.go: Entry point of the application.
- Makefile: Contains commands to build and run the application.
- sqlc.yaml: Configuration for sqlc to generate Go code from SQL queries.
- app.env: Environment variables for the application.
- api.http: HTTP file for testing API endpoints.
To run the generated API service:
-
Ensure the Docker container is already running.
-
Run the API service:
make run
The server will start on port 8080. The migration is done automatically. If needed, you can migrate it manually using the Makefile commands.
The generated Makefile includes commands for running the service, database migrations, testing, building, and generating SQL code:
# Generated By API Service Generator
include app.env
migrateup:
migrate -path pkg/db/migrations -database "$(DB_SOURCE)" -verbose up
migratedown:
migrate -path db/migration -database "$(DB_SOURCE)" -verbose down
run: ## run the api-service
go run main.go
test: # run unit tests
go test -v -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
go tool cover -html=coverage.out -o coverage.html
build: ## build the offload-service binary
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go
sqlc: ## run all new database migrations
@echo "Running sqlc code generation..."
sqlc generate
.PHONY: migrateup, migratedown, run, test, build, sqlc
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
The in-progres changes, upcoming changes, and proposals are listed. See the Changelog file for details.