In this guide we are are going to check a simple implementation Revel Framework REST API. In this tutorial we create will be a simple posts CRUD app that will connect to the Postgres Database. Feel free to checkout the repo where the full code is hosted here.
Before proceeding, ensure that golang is nstalled.
Related Content
- How to install Go in Fedora and Rocky Linux/Centos/RHEL
- How to install Go (Golang) in Arch Linux/Manjaro
- Managing Database migrations with Golang goose using incremental SQL changes
Installing Revel
Next we need to install the revel framework. Revel is a golang framework, it can be installed as a golang library.
To get the Revel framework, use this command?
|
|
This command does a couple of things:
- Go uses git to clone the repository into
$GOPATH/src/github.com/revel/revel/
- Go transitively finds all of the dependencies and runs
go get
on them as well.
Next, get and build the revel command line tool
The revel
command line tool is used to build
, run
, and package
Revel applications.
Use go get
to install the revel command line tool:
|
|
Ensure the $GOPATH/bin
directory is in your PATH so that you can reference the command from anywhere.
|
|
Verify that revel works:
|
|
Creating our application Database
We will store our data in postgres database. So before proceeding, we will need a posgres database and some credentials to connect. Ensure that you have a postges databases created with a user and a password that can connect to it.
Now create a database table that we will use. Checkout better way of using db migrations to manage database operations here
|
|
Creating Revel app
Create a revel application using this command. Here we are creating an app called go-revel-crud. The app will be created in the current directory. This command will auto generate boilerplate
like app
, conf
, message
, public
, test
, utils
for the app that we are creating.
|
|
Next, change to the application directory and run the go mod tidy to download the initial application dependencies.
|
|
|
|
After that, open your browser and navigate to localhost:9000
. You will see IT Works! in your browser, that means that your revel app already running. But since we want a REST CRUD API, We will proceed to customize the app in our case.
Routes
Let us create the routes that we will need for our app. URL’s and routes are defined in the conf/routes
file and have three columns as example below:
|
|
By default it has some routes added, so you can figure out how it works. Add these routes to the file conf/routes
|
|
Controllers
The revel.Controller is the context for a single request and controls
Let us create Posts controller functions for out code.It should be located on <app_root_directory>/app/controllers
. We will call it posts.go
.
We need to add ‘package controller’ in the beginning of our code, and by default all controllers should extend a class Controller located on Revel’s library. The controller name needs to be the same declared on routes file. Now we should make a method to render the actions defined in the routes. Revel pages can receive as many attributes as you want.
For our case, let us define controller functions for all our routes:
|
|
Models
For our models, we will create functions that will interact with the database to execute the db queries for the respective functions that we want to achieve. We will have functions to do create records, update, fetch and delete.
Create the models for our posts crud app:
|
|
Other logic
We could also have code for the following things in our code:
- Views - defines the html content to be rendered to the browser parsing the data. By default they are located on:
/<controller_name>/<action>
. You will need to create the Apps directory on:<app_root_directory>/app/views
. - Tests - Revel has a place to tests, it is located on
<app_root_directory>/tests
Runing the app
Once the app is ready, we can run it using the revel run .
command in the same directory. But before doing that, we need to set the application Database URL and the PORT.
|
|
Testing the application
Verify that the healthcheck endpoint is working with this curl command?
|
|
Add post endpoint
Next verify the Add post endpoint:
|
|
Response:
|
|
List Posts endpoint
|
|
Response:
|
|
Get single post
|
|
Response:
|
|
Update post
|
|
Response:
|
|
Delete Post
|
|
Response:
|
|
Conclusion
In this post we managed to explore how to do CRUD with golang revel.