Skip to content

A CLI (Command Line Interface) to create your apps with Service Weaver, using gomigrate, sqlc, and Fiber.

License

Notifications You must be signed in to change notification settings

renanbastos93/boneless

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

96 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

โœ‹ Welcome to Boneless CLI

A CLI (Command Line Interface) to create your apps with Service Weaver, using gomigrate, sqlc, and Fiber.

Why was the name 'Boneless' chosen for this CLI tool?

The choice of the name "Boneless" for this CLI tool draws inspiration from the historical figure Ivar the Boneless. Ivar the Boneless was a legendary Viking leader known for his strategic prowess, adaptability, and agility on the battlefield. In a similar vein, the name "Boneless" for the CLI tool reflects these qualities in the context of software development. It symbolizes the tool's ability to navigate through complex tasks and challenges effortlessly, just as Ivar the Boneless maneuvered through battles with agility and cunning. By associating the CLI tool with Ivar the Boneless, the name not only captures the spirit of adaptability and flexibility but also adds a touch of historical significance to the tool's identity. In summary, the name "Boneless" pays homage to Ivar the Boneless and serves as a metaphor for the CLI tool's ability to conquer development obstacles with ease and grace.

Which are the tools used behind the Boneless?

  1. Service Weaver: Service Weaver is a tool that facilitates the creation and management of microservices. It helps in defining service boundaries, handling service discovery, and managing communication between microservices.
  2. go-migrate: go-migrate is a database migration tool for Go applications. It allows developers to manage database schema changes and versioning in a structured and automated manner, ensuring smooth database migrations across different environments.
  3. SQLC: SQLC is a tool that generates type-safe Go code based on SQL queries. It helps in writing database code by automating the process of generating Go code from SQL queries, reducing boilerplate code and enhancing type safety.
  4. Fiber: Fiber is a fast and efficient web framework for Go. It provides a lightweight and easy-to-use foundation for building web applications. Fiber offers features like routing, middleware support, and performance optimizations, making it a popular choice for developing high-performance web services.

These tools, when used alongside Boneless, contribute to various aspects of the development process. Service Weaver aids in managing microservices, go-migrate simplifies database migrations, SQLC enhances database code generation, and Fiber provides a robust framework for building web applications. Together, they enhance the functionality and development experience of Boneless.

๐Ÿ’ก About the architecture

Let's dive into the architecture used in Boneless. In this section, we will explore the intricacies and key details of the architecture that powers the project created by Boneless.

What is the architecture used as the foundation for Boneless?

Boneless was inspired by Clean Architecture and offers templates that follow its core concepts. The templates provided by Boneless facilitate the creation of modular, maintainable, and testable codebases by promoting a clear separation of concerns and the independence of business logic from external dependencies. By using Boneless templates, developers can jumpstart their projects with a well-organized structure that aligns with Clean Architecture, enabling them to focus on implementing the domain-specific logic while adhering to best practices. Boneless empowers developers to build robust and scalable applications, leveraging the benefits of Clean Architecture for easier understanding, maintenance, and evolution over time.

image

Based on Service Weaver, which generates connections between components using gRPC, has greatly facilitated the development of our applications.

Here is the current repository structure, reflecting the adopted architecture:

.
โ”œโ”€โ”€ cmd
โ”‚   โ””โ”€โ”€ main.go
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ go.sum
โ”œโ”€โ”€ internal
โ”‚   โ”œโ”€โ”€ app
โ”‚   โ”‚   โ”œโ”€โ”€ component.go
โ”‚   โ”‚   โ”œโ”€โ”€ db
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ migrations
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ schema.sql
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ query.sql
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ sqlc.yaml
โ”‚   โ”‚   โ”œโ”€โ”€ entity.go
โ”‚   โ”‚   โ”œโ”€โ”€ store
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ db.go
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ models.go
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ query.sql.go
โ”‚   โ”‚   โ””โ”€โ”€ weaver_gen.go
โ”‚   โ””โ”€โ”€ bff
โ”‚       โ”œโ”€โ”€ bff.go
โ”‚       โ”œโ”€โ”€ router.go
โ”‚       โ””โ”€โ”€ weaver_gen.go
โ””โ”€โ”€ weaver.toml

In this structure, we can observe the organization of directories and files in the repository. The cmd directory contains the main.go file, which is responsible for starting the application. The go.mod and go.sum files are used to manage project dependencies. The internal the directory is where the main implementation of the application resides. Inside, we have the app directory, which contains components related to the application's domain, such as entities and business logic. The db directory is used to store files related to the database layer, such as migrations and SQL queries. The store directory contains files related to data storage, such as the implementation of database access. The bff directory contains files related to the implementation of the Backend for the Frontend layer. Lastly, the weaver.toml file is used to configure Service Weaver, which facilitates generating connections between application components using gRPC. This directory and file structure reflects the adopted architecture in the repository, following the principles of Clean Architecture and facilitating the organization and maintenance of the application's source code.

โœจ Getting started

Let's create our first project from scratch using Boneless!

Installing dependencies

First, we need to install Boneless. Then, use the install-deps command to ensure its dependencies are installed correctly. If you encounter any issues, open an issue or check the official websites of the dependencies, such as SQLC, Service Weaver, and Go Migrate, for more information.

$ go install github.com/renanbastos93/boneless/cmd/boneless@latest
$ boneless install-deps

To ensure a smooth setup of your Boneless project, let's install all the dependencies, including Boneless itself. If you're not using macOS, you can access their website for detailed instructions on installing Boneless and its dependencies specific to your operating system.

Now that we have installed it, let's use the 'help' command to display the available options.

$ boneless help
Usage: boneless [target]

Targets:
  help                                     Show commands for use
  version                                  Show version
  new <sql|sqlite3>                        Create a project from scratch using Weaver, SQLC, and go-migrate
  create-scratch <sql|sqlite3>             Create a project from scratch using Weaver, SQLC, and go-migrate
  build                                    Build the Weaver component with SQLC
  make-migrate <app-name> <name>           Create a new migration for an app
  migrate <app-name> <up|down>             Run migrations for an app
  create-app <app-name>                    Create a new app based on a template
  build-app <app-name>                     Build an app using Weaver and SQLC
  delete-app <app-name>                    Delete an app created
  install-deps [package]          Installs external dependencies required by Boneless (e.g., weaver, sqlc). If no package is specified, all dependencies are updated.
  update-deps [package]           Updates the specified external dependency (e.g., weaver, migrate). If no package is specified, all dependencies are updated.
  run                                      Run the project using Weaver

Parameters:
  <app-name>                               Name of the app to create or run migrations on
  <name>                                   Name of the migration to create
  <up|down>                                Specify "up" to apply migrations or "down" to rollback migrations
  <sql|sqlite>                             Specify "sql" to use some SQL "sqlite3" to use sqlite3 and it is the default 
  [package]                                Specify the package to update or install like "weaver, sqlc, golang-migrate"


Examples:
  boneless help
  boneless version
  boneless create-scratch
  boneless build
  boneless make-migrate my-app migration-name
  boneless migrate my-app up
  boneless create-app my-app
  boneless build-app my-app
  boneless delete-app my-app
  boneless install-deps
  boneless install-deps weaver
  boneless update-deps
  boneless update-deps sqlc
  boneless run

After that is installed, let's create our first project from scratch using Boneless, a framework based on clean architecture, to efficiently generate and organize the project structure, implement functionality, and deploy it with ease.

๐Ÿ‘ทโ€โ™‚๏ธ Creating Project

Let's dive right in and create a project from scratch, and then run it to see Boneless in action!

For starting we've been thinking of a simple API that uses CRUD.

# First, we need to create go.mod file
$ go mod init bone

# After that let's call command boneless to start a project
# As a default, it was created using SQLite3
$ boneless new

# Here we going to execute all migrations of the `internal/app`
$ boneless migrate app up

# Now, we can use the command `run`
boneless run

    __                           __                                ______    __     ____
   / /_   ____    ____   ___    / /  ___    _____   _____         / ____/   / /    /  _/
  / __ \ / __ \  / __ \ / _ \  / /  / _ \  / ___/  / ___/        / /       / /     / /  
 / /_/ // /_/ / / / / //  __/ / /  /  __/ (__  )  (__  )        / /___    / /___ _/ /   
/_.___/ \____/ /_/ /_/ \___/ /_/   \___/ /____/  /____/         \____/   /_____//___/  

running...

PID: <process-id>
BFF listener available on 127.0.0.1:8090

 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” 
 โ”‚                   Fiber v2.48.0                   โ”‚ 
 โ”‚               http://127.0.0.1:8090               โ”‚ 
 โ”‚                                                   โ”‚ 
 โ”‚ Handlers ............. 6  Processes ........... 1 โ”‚ 
 โ”‚ Prefork ....... Disabled  PID ............. 40506 โ”‚ 
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ 

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ app        : main                                 โ”‚
โ”‚ deployment : <uuid>                               โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Wow, here we created your first app using Boneless. Now, we can be using it, let's use cURL for testing.

# Get All Examples
$ curl --location --request GET 'http://127.0.0.1:8090/examples'

# returns empty because we haven't data yet
# []

# Create an Example
$ curl --location --request POST 'http://127.0.0.1:8090/examples/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "message": "Hello everyone"
}'

# After you created anything we can get an example just
# but you need to know the ID for it
$ curl --location --request GET 'http://127.0.0.1:8090/examples/:id'

# example data
{
        "id": 2104895800, # id is random
        "created_at": "2023-07-20T03:22:37.289-03:00",
        "message": "Hello everyone"
}

That's all folks!