Skip to content

Commit

Permalink
Added Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijith authored and abhijith committed Jun 6, 2024
1 parent 4ebb684 commit b3ad5dd
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ install:
go install

build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go
go build -o api-service-generator

.PHONY: api-service-generator, clean, test, install, build
162 changes: 161 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,161 @@
# api-service-generator
# 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.
- [alog](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/yourusername/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/
```

## 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 template --name myservice
```

### Prompts

The CLI will prompt you to enter the following details:

1. **Database Driver**: (Currently supports only `postgres`)
2. **Container Name**: Name for the PostgreSQL Docker container
3. **Container Port**: Port for the PostgreSQL Docker container
4. **PostgreSQL User**: PostgreSQL user
5. **PostgreSQL Password**: PostgreSQL password
6. **Database Name**: Name of the PostgreSQL database
7. **Table Name**: Name of the database table
8. **API Group**: API group for the generated service
9. **Module Path**: Base path for the Go module

## 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 PostgreSQL Docker container is already running.
2. Run the API service:

```sh
make run
```

The server will start on port 8080.

### Makefile

The generated Makefile includes commands for running the service, database migrations, testing, building, and generating SQL code:

```Makefile
# 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](LICENSE) file for details.

0 comments on commit b3ad5dd

Please sign in to comment.