Skip to content

A CLI application that creates a Golang API Service application template.

License

Notifications You must be signed in to change notification settings

abhijithk1/api-service-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

Prerequisites

Ensure the following commands are installed and available in your system's PATH:

  1. Go (1.16 or later)
  2. golang-migrate CLI
  3. sqlc CLI
  4. Docker

Installation

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

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:

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-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

Files and Directories

  • 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.

Running the Service

To run the generated API service:

  1. Ensure the Docker container is already running.

  2. 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.

Makefile

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

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 file for details.

Changelog

The in-progres changes, upcoming changes, and proposals are listed. See the Changelog file for details.