Joseph Jude

Introduction to Beego, a golang web application framework

2018.05.30 / code / golang /

Create a simple web application serving a json in beego, a golang web application framework.

Beego Framework

Beego is a comprehensive web application framework for Golang. Beego includes support for:

You can write a decent web-application using core Golang language. Yet, coding enterprise applications require above features. Instead of composing these from independent sources, you can use Beego which comes bundled with all of these.

If you are coming from Python ecosystem, Beego is like Django.

dep is the dependency management tool in Golang. We will use dep to install and upgrade the dependencies. Let us initialize our repository with dep init.

I wrote about dep in an earlier post. Read it to understand how to use dep.

It will create a vendor directory and two other files. Your directory should look like this:

├── Gopkg.lock
├── Gopkg.toml
├── vendor

Create a main.go file.

package main

func main() {
}

Now it is time to install beego. Install beego with dep.

dep ensure -add github.com/astaxie/beego

It will install beego under vendor directory. It is recommended to store this directory into your version control system.

.
├── Gopkg.lock
├── Gopkg.toml
├── main.go
└── vendor
    └── github.com
        └── astaxie
            └── beego

Your Gopkg.toml should look like this:

[prune]
  go-tests = true
  unused-packages = true

[[constraint]]
  name = "github.com/astaxie/beego"
  version = "1.9.2"

Now we can start writing a web-server. Modify the main.go file with the below content.

package main

import (
	"github.com/astaxie/beego"
)

func main() {
	beego.Run()
}

beego.Run() runs the web server with default configuration. You can execute this with go run main.go. You will see

http server Running on http://:8080

If you open http://localhost:8080 you will see an error message, like in the following image. What is happening?

Beego Not Found Error

The above code-snippet created a web-server but that server doesn’t know what to do with an incoming request. Let us code that part.

package main

import (
	"github.com/astaxie/beego"
)

// 1. Embed beego controller
type MainController struct {
	beego.Controller
}

// 2. Code a get method
func (this *MainController) Get() {
	this.Ctx.WriteString("hello world")
}

// 3. Register the router and bring up the server
func main() {
	beego.Router("/", &MainController{})
	beego.Run()
}

beego.Controller has methods for handling REST methods (get, post etc). We create a MainController by embedding (like inheritance in other languages) beego.Controller.

We overload only Get method. In this method we are just writing a string as an output.

We, then, register this router (for Get method) and bring up the server.

You can run this code by go run main.go. If you open the browser to http://localhost:8080, you will see a hello world.

Beego also supports serving json, xml as responses. Let us modify the above program to serve a json.

package main

import (
	"github.com/astaxie/beego"
)

type MainController struct {
	beego.Controller
}

func (this *MainController) Get() {
	this.Data["json"] = map[string]int{"apple": 5, "lettuce": 7}
	this.ServeJSON()
}

func main() {
	beego.Router("/", &MainController{})
	beego.Run()
}

You can open the browser to http://localhost:8080 or try curl http://localhost:8080. You will see the result as:

{
  "apple": 5,
  "lettuce": 7
}

This is only a tip of Beego framework. If you want to learn more, subscribe using the below form.

Links


Share this post on

Twitter | | |
Sign up for my weekly newsletter

I will send blog updates to this email. You can unsubscribe at any time using the link in those emails.