Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abhijithk1/api-service-generator
A CLI application that creates a Golang API Service application template.
https://github.com/abhijithk1/api-service-generator
api-service cli-application code-generation docker gin gin-gonic golang golang-migrate makefile middleware postgresql rest-api server sqlc template template-generator viper
Last synced: about 1 month ago
JSON representation
A CLI application that creates a Golang API Service application template.
- Host: GitHub
- URL: https://github.com/abhijithk1/api-service-generator
- Owner: abhijithk1
- License: mit
- Created: 2024-05-26T05:48:21.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-06-16T13:20:42.000Z (5 months ago)
- Last Synced: 2024-10-02T05:22:06.426Z (about 1 month ago)
- Topics: api-service, cli-application, code-generation, docker, gin, gin-gonic, golang, golang-migrate, makefile, middleware, postgresql, rest-api, server, sqlc, template, template-generator, viper
- Language: Go
- Homepage:
- Size: 77.1 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# API Service Generator
`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](https://github.com/gin-gonic/gin) for building the API.
- [IBM/alchemy-logging](https://github.com/IBM/alchemy-logging) for logging.
- [golang-migrate](https://github.com/golang-migrate/migrate) for database migrations.
- [sqlc](https://github.com/sqlc-dev/sqlc) for generating type-safe Go code from SQL queries.
- [Viper](https://github.com/spf13/viper) for configuration management.## Prerequisites
Ensure the following commands are installed and available in your system's PATH:
1. [Go](https://go.dev/doc/install) (1.16 or later)
2. [golang-migrate CLI](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate)
3. [sqlc CLI](https://docs.sqlc.dev/en/latest/overview/install.html)
4. [Docker](https://docs.docker.com/get-docker/)## Installation
Clone the repository and build the CLI application:
```sh
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.
```sh
mv api-service-generator /usr/local/bin/
```> Note: Can Skip the build and moving the binary process. Instead run: `go install`
## Usage
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:
```sh
api-service-generator go-template --name myservice
```### Prompts
The CLI will prompt you to enter the following details:
1. **Database Driver**: Choose between postgres and mysql *(Default: `postgres`)*
2. **Container Name**: Name for the Docker container *(Default: `dummy_db`)*
3. **Container Port**: Port for the Docker container *(Default: `6432`)*
4. **Database Name**: Name of the database *(Default: `dummy_db`)*
5. **Table Name**: Name of the database table *(Default: `api_table`)*
6. **API Group**: API group for the generated service *(Default: `dummy`)*
7. **Module Path**: Base path for the Go module *(Default: `example/api-service`)*
#### PostgresQL Specific Prompts
1. **POSTGRES_USER**: PostgreSQL user *(Default: `postgres`)*
2. **POSTGRES_PASSWORD**: PostgreSQL password *(Default: `password`)*#### MySQL Specific Prompts
1. **MYSQL_ROOT_PASSWORD**: MySQL root password *(Default: `my-root-secret`)*
2. **MYSQL_USER**: MySQL user *(Default: `mysql`)*
3. **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.
## Project Structure
The generated project has the following structure:
```
| _ api
| | _ v1
| | _
| | _ controller.go
| | _ service.go
| | _ mw
| | _ cors.go
| | _ auth.go
| _ pkg
| | _ db
| | _ migrations
| | _ 000001_init_schema_up.sql
| | _ 000001_init_schema_down.sql
| | _ query
| | _ .sql
| | _ connection.go
| | _ .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
```### Files and Directories
- **api/v1//**: 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.## Running the Service
To run the generated API service:
1. Ensure the Docker container is already running.
2. Run the API service:```sh
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.
### Makefile
The generated Makefile includes commands for running the service, database migrations, testing, building, and generating SQL code:
```Makefile
# Generated By API Service Generatorinclude app.env
migrateup:
migrate -path pkg/db/migrations -database "$(DB_SOURCE)" -verbose upmigratedown:
migrate -path db/migration -database "$(DB_SOURCE)" -verbose downrun: ## run the api-service
go run main.gotest: # run unit tests
go test -v -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
go tool cover -html=coverage.out -o coverage.htmlbuild: ## build the offload-service binary
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.gosqlc: ## run all new database migrations
@echo "Running sqlc code generation..."
sqlc generate.PHONY: migrateup, migratedown, run, test, build, sqlc
```## Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Changelog
The in-progres changes, upcoming changes, and proposals are listed. See the [Changelog](CHANGELOG.md) file for details.