Simple REST API Go using Revel Framework and CockroachDB 🐞

Simple REST API Go using Revel Framework and CockroachDB 🐞

Hello guys, okey this article will simple explain about implementation Revel Framework REST API, you can see in repo revel in Github, first commit in 2011 with Ropfig with Go Play, Wow this is amazing I think, almost 9 years development. Well this article not talking about history revel framework but, in now how to use it in REST API

By The Why Database can using everything like Mysql, Postgresql, MongoDB or the other, but in example this article we will use CockroachDB, you never know about this DB ? Its okey no problem, actually this DB like Postgresql, you can check more detail CockroachDB this link

Start with Revel framework, first time you can install revel in your computer with:

  • go get github.com/revel/revel
  • go get github.com/revel/cmd/revel

If you already go get you can running command revel , and you can install your project using command in your terminal revel new your_project , this command will auto generate boilerplate like app, conf, message, public, test, utils , well you already create project using revel, you can running this project using revel run -a your_project , open your browser safari, chrome or the others and write localhost:9000 you can see in your browser IT Works! well your revel framework already running, but this case we will using REST API using revel.

Connect to CockroachDB

First time setting your go config to connect to CockroachDB, you can using gorm, or revel whatever what do you want, but in this example using gorm

No alt text provided for this image

You can see in this example connect to cockroachDB, this driver using postgres but default port different, change user, password, dbname base on your local config, Okey well you already connection with cockroachDB then you set your routing.

Well we will create one tables for example to get data, this example create table and insert data but if you want create the other its okey, whatever you want:

No alt text provided for this image

Create Routing

First time you can check file in your_project/conf/routes you see so many comment in this file but you can focus in code

GET     /                                       App.Index

This code for routing, you can add some route in this code, okey example you will create get all data from table student you can add this code

GET     /                                       App.Index
GET     /students                               App.GetStudents

GET for methos you will call, /students routing you can call this URL, App.GetStudents for call method in controllers. Then open directory app/controllers/app.go you can see default code you already have, add some code for your routes like this:

No alt text provided for this image

This function for get all data students, and struct for table student, Oh yeah this case query in controllers but you can code in Models, after query data will show in json, you can try in postman or insomnia whatever you like, URL like this localhost:9000/students with method GET you can get response like this:

No alt text provided for this image

Yeay, article already finish, you can adjust for your requirement, like update data, Get Detail data, Insert Data, Delete Data, if you confuse for how to coding CRUD you can see in my repo, this repo already have API Insert, update, detail data and delete data, lets check it out.




To view or add a comment, sign in

More articles by Abdul Aziz

Others also viewed

Explore content categories