Nunu — A CLI tool for building Go applications

Nunu — A CLI tool for building Go applications

  • 28 Jan 2024
  • 4 min read

A very common question for those starting out with Go is how to organize the project structure. Where to place the Controllers, Services and Repositories? How to perform dependency injection? How to keep track of the logs?

And to make this task easier there's Nunu (go-nunu/nunu), a CLI to generate the scaffolding for your project. With it you can create a complete Go application in seconds with a well-organized structure and the main libraries used by the community, such as:

Key features

  • Low learning curve and customization: Encapsulates popular libraries that Gophers are familiar with, allowing you to easily customize the application to meet specific requirements.
  • High performance and scalability: Aims to be high performance and scalable. It uses the latest technologies and best practices to ensure that your application can handle high traffic and large amounts of data.
  • Security and reliability: Uses stable and reliable third-party libraries to ensure the security and reliability of your application.
  • Modular and extensible: Designed to be modular and extensible. You can easily add new features and functionality using third-party libraries or by creating your own modules.
  • Comprehensive documentation and testing: It has comprehensive documentation and tests. It provides extensive documentation and examples to help you get started quickly. It also includes a set of tests to ensure that your application works as expected.

Requirements

To use Nunu you need to have the following software installed on your computer:

  • Go 1.19 or higher
  • Git
  • Docker (optional)
  • MySQL 5.7 or higher (optional)
  • Redis (optional)

Getting Started

Let's install Nunu using the following command:

go install github.com/go-nunu/nunu@latest

If the installation was successful, running the command below will show you how to use Nunu.

nunu -h

Creating a project

To create a project, just run it:

nunu create [project-name]

You will need to choose the type of project:

  • Advanced - More complete structure with db, jwt, cron, migration, test, etc.
  • Basic - Basic structure.

After that, your project will be created, as shown in the image below.

Ao entrar na pasta do seu projeto, você terá uma estrutura inicial da seguinte forma:

.
├── LICENSE
├── README.md
├── README_zh.md
├── cmd
│   └── server
│       ├── main.go
│       ├── wire.go
│       └── wire_gen.go
├── config
│   ├── local.yml
│   └── prod.yml
├── go.mod
├── go.sum
├── internal
│   ├── handler
│   │   ├── handler.go
│   │   └── user.go
│   ├── middleware
│   │   └── cors.go
│   ├── model
│   │   └── user.go
│   ├── repository
│   │   ├── repository.go
│   │   └── user.go
│   ├── server
│   │   └── http.go
│   └── service
│       ├── service.go
│       └── user.go
├── pkg
│   ├── config
│   │   └── config.go
│   ├── helper
│   │   ├── convert
│   │   │   └── convert.go
│   │   ├── md5
│   │   │   └── md5.go
│   │   ├── resp
│   │   │   └── resp.go
│   │   ├── sid
│   │   │   └── sid.go
│   │   └── uuid
│   │       └── uuid.go
│   ├── http
│   │   └── http.go
│   └── log
│       └── log.go
└── storage
    └── logs
        └── server.log

Directory definitions:

  • cmd: The application is bootstrapped via the server/main.go file in this folder.
  • config: Contains our configuration files with port settings, security, database and logs.
  • internal: Here are the main implementations of our application, such as handlers, services, models.
    • handler: Definitions of our controllers.
    • middleware: Contains our middleware, such as: CORS, Authentication, Logging, Caching, Request Tracing.
    • model: Structure defining the entities used in the database.
    • repository: Contains the implementations of the data access layer.
    • server: Contains the application's route definitions, mapping them to the handlers.
    • service: Layer with the definitions of the application's business rules.
  • pkg: Contains independent packages that provide some application utilities.
  • storage: Directory for storing static files, such as: log, upload files, temporary files, etc..

By default, our scaffolding already has a user component (handler/service/repository/model) created.

New components can be created via the command line using:

nunu create handler company
nunu create service company
nunu create repository company
nunu create model company

or

nunu create all company

These commands will create components called CompanyHandler, CompanyService, CompanyRepository and CompanyModel, respectively, and place them in the correct directories.

Running the project

Now that we know the structure of our application, we can run it using the command:

nunu run

When you open the browser at: http://localhost:8000/ you'll see our application running!

Conclusion

The main advantage of using Nunu is that you already have a good structure defined for your project, so you can start development with a stage already completed.