Rails Like Golang Setup With Revel and GORM
The popularity of Golang has been increasing since its inception. The recent survey shows 76% of the users are using Golang at work and 66% are using it outside the workplace.
There are many developers who are transitioning from other languages and frameworks to Golang for better performance. Many are moving their existing applications to Golang or developing their new apps in Golang.
Why Revel?
Like many others, our application was written in Ruby on Rails. We started working on a new application, we started considering Golang for it. Since the team was familiar with the frameworks like Rails and Django, we started exploring various frameworks of Golang in search of an MVC framework.
We came across Revel. A beautiful framework that is very similar to Rails.
Why GORM?
As we started to explore Revel, we wanted an ORM as powerful as Active Record. Its hard (almost impossible) to find an ORM as powerful and easy as Active Record. But we landed on GORM. GORM is a simple and easy to code ORM for Golang.
What are we building?
We are going to set up the project and build a few simple APIs to demonstrate how similar Revel is to Rails.
We will write go REST APIs that create new users, fetches the data for an user, and deletes an user.
Prerequisites
We assume these packages are already installed on your machine
- Go
- MySQL
- IDE of your choice. We prefer Visual Studio Code.
Setup
The first step is to install Revel with these commands
$ go get github.com/revel/revel
$ go get github.com/revel/cmd/revel
Now, create the project with the following command:
$ cd $GOPATH/src
$ revel new -a sampleapp
Revel executing: create a skeleton Revel application
Your application has been created in:
/home/user1/go/src/sampleappYou can run it with:
revel run -a sampleapp
Try to run the application with this command
$ revel run -a sampleapp
If you get issues related to Memcached, Redis or go-cache, please install these libs
$ go get -u github.com/bradfitz/gomemcache/memcache
$ go get -u github.com/garyburd/redigo/redis
$ go get -u github.com/patrickmn/go-cache
Now, install GORM
$ go get -u gorm.io/gorm
For MySQL driver install the following lib
go get -u github.com/go-sql-driver/mysql
MySQL user and DB setup
On MySQL console (sudo mysql
) as root user please run these commands to create a user and DB for the project
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'tmppwd';
CREATE DATABASE sampleapp;GRANT ALL PRIVILEGES ON sampleapp.* TO 'user1'@'localhost';
DB Config
To connect to the MySQL DB we need the following configurations
In file conf/app.conf
add the following code for your environment. I am using dev
environment in the example.
[dev]db.info = user1:tmppwd@tcp(localhost:3306)/sampleapp?parseTime=True
The above code only adds a configuration. Now we add code to create the DB object which will be used to communicate with the DB.
The following code goes in the file app/init.go
Key things to notice here are including _ “github.com/go-sql-driver/mysql”
, implementation of initDB
method and loading using the method in the start of the application by registering it with revel.OnAppStart(initDB)
.
DB
object can be used across the project to refer to the DB connection.
Creating Model
First, create a directory named models in the app directory.
mkdir app/models
We need to create a table in the database with the following command on MySQL console.
mysql> CREATE TABLE `users` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL,
`email` varchar(128) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_users_on_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Let’s create our first model ‘User’. In Golang, the model is essentially a struct.
In the above file app/models/user.go
the structure of the model is defined along with few methods to create or update an entry, to find an entry and to delete an entry.
Routes
Just like Rails (Ruby on Rails) we need to create API routes. The syntax looks very similar. Add these lines in conf/routes
GET /users/:id Users.GetUserPOST /users Users.CreateDELETE /users/:id Users.Delete
The above APIs point to Users
controller.
Controller
We need to create a controller (and methods) for the APIs.
Create a file app/controllers/users.go
with the following content:
Run the project
Run the project with the following command:
$ revel run -a sampleapp
Following command to call the POST API which creates a new user in the users table.
$ curl --header "Content-Type: application/json" \
--request POST \
--data '{"name": "user1", "email": "user1@example.com"}' \
http://localhost:9000/users
Command to call the GET API. This responds with the entry you created with the above command.
$ curl --header "Content-Type: application/json" --request GET http://localhost:9000/users/1
To delete the user call this command.
curl --request DELETE http://localhost:9000/users/1
Repository
Please find the complete code at https://github.com/amritsingh/golang_tutorials/tree/main/revel/sampleapp