A CLI application that creates a Golang API Service application template.
MIT License
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:
Ensure the following commands are installed and available in your system's PATH:
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:
postgres
)
dummy_db
)
6432
)
dummy_db
)
api_table
)
dummy
)
example/api-service
)
postgres
)
password
)
my-root-secret
)
mysql
)
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
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.