Sunday, March 13, 2022

Golang REST API Tutorial (Part I)

On this series, we gonna build the REST API with Golang. As a basis, we'll be using Goyave.  Goyave is an opinionated Golang REST API framework aiming at cleanliness, fast development and power. Goyave applications stay clean and concise thanks to minimalist function calls and route handlers. The framework gives you all the tools to create an easily readable and maintainable web applications, which let you concentrate on the business logic. Although Goyave is a full package requiring very few setup and that handles many things for you, such as headers or marshaling, this characteristic doesn't compromise on your freedom of code.

You can read more about goyave framework from here: https://goyave.dev/

As an example, we'll be build blog API. The requirements is:

  • Go 1.16+
  • Go modules

the directory structure

.
├── database
│   ├── model                // ORM models
│   |   └── ...
│   └── seeder               // Generators for database testing
│       └── ...
├── http
│   ├── controller           // Business logic of the application
│   │   └── ...
│   ├── middleware           // Logic executed before or after controllers
│   │   └── ...
│   ├── validation
│   │   └── validation.go    // Custom validation rules
│   └── route
│       └── route.go         // Routes definition
│
├── resources
│   └── lang
│       └── en-US            // Overrides to the default language lines
│           ├── fields.json
│           ├── locale.json
│           └── rules.json
│
├── test                     // Functional tests
|   └── ...
|
├── .gitignore
├── .golangci.yml            // Settings for the Golangci-lint linter
├── config.example.json      // Example config for local development
├── config.test.json         // Config file used for tests
├── go.mod
└── main.go                  // Application entrypoint
Running the project 

First, make your own configuration for your local environment. You can copy config.example.json to config.json. 
 Run go run main.go in your project's directory to start the server.

0 comments:

Post a Comment