Building Web Applications using Beego + Mysql+ORM
Tags: go, orm, mysql, beego
Published: 2021-04-29 09:53:47

# Beego:

Beego is a RESTful HTTP framework for the rapid development of Go applications including APIs, web apps and backend services with integrated Go-specific features such as interfaces and struct embedding.

M (models), V (views), C (controllers) each have top level folders. main.go is the entry point.

## Installing Beego

Run the below command in your gopath

```
go get -u github.com/beego/beego/v2
go get -u github.com/beego/bee/v2
```

Beego has in-built scaffolding support, via the command line tool bee

* Create new applications
* Run an application
* Test the application
* Create routes and more

## Create The Core Project

Once installed, from your $GOPATH directory, run the following command, which will scaffold the application, called sitepointgoapp:

```bee new sitepointgoapp```

Looking at the files, we have:
1.Our bootstrap file main.go
2.The core configuration file conf/app.conf
3.A default controller controllers/default.go
4.A default set of tests in tests/default_test.go
5.A default view template in views/index.tpl

Right, the basic app is ready, now run the below command 

``` bee run ```

This loads our new application.

Router settings:

```
beego.Router("/user/home", &controllers.UserController{}, "*:User")
beego.Router("/user/add", &controllers.UserController{}, "get,post:Add")
```

Here i've used the mysql with ORM. First we need to install the MySQL driver using golang by using the below command

```
go get -u github.com/go-sql-driver/mysql
```


The first one imports Beego’s ORM library ***"github.com/astaxie/beego/orm"***, the second one provides support for mysql, required because we’re using an mysql database. The third one imports the models we just created, giving them an alias of models.

create the table in MySQL for orm_test and run the below query to create the users table.

```
CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `client` varchar(200) NOT NULL,
  `url` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
```

In the main.go we need to register the driver along with database settings like below for mysql
	
```
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:@/orm_test?charset=utf8")
orm.RegisterModel(new(models.User))

```
Install ORM using

```go get github.com/beego/beego/v2/client/orm``` command. By using this line we need to insert the record in db using ORM

```
id, err := o.Insert(&user)
if err == nil {
	msg := fmt.Sprintf("User inserted with id:", id)
	beego.Debug(msg)
	flash.Notice(msg)
	flash.Store(&manage.Controller)
} else {
	msg := fmt.Sprintf("Couldn't insert new User. Reason: ", err)
	beego.Debug(msg)
	flash.Error(msg)
	flash.Store(&manage.Controller)
}
```

If we using the sqlite we need to install the GCC and then we need to setup the database setting same as like below in main.go file

```
orm.RegisterDriver("sqlite", orm.DR_Sqlite)
orm.RegisterDataBase("default", "sqlite3", "database/orm_test.db")
orm.RegisterModel(new(models.User))

```

Check the http://localhost:8086/user/home we have displayed the home page which we have done.

## Beego commands:

1.***bee pack*** The pack command is used to compress the project into a single file. The compressed file can be deployed by uploading and extracting the zip file to the server
```
______
| ___ \
| |_/ /  ___   ___   
| ___ \ / _ \ / _ \  
| |_/ /|  __/|  __/  
\____/  \___| \___| v1.12.0
2021/04/29 15:15:17 INFO     ▶ 0001 Packaging application on 'D:\Go-work\src\github.com\aws-learning\bee-go\sitepointgoapp'...
2021/04/29 15:15:17 INFO     ▶ 0002 Building application (sitepointgoapp)...
2021/04/29 15:15:17 INFO     ▶ 0003 Using: GOOS=windows GOARCH=amd64
2021/04/29 15:15:21 SUCCESS  ▶ 0004 Build Successful!
2021/04/29 15:15:21 INFO     ▶ 0005 Writing to output: D:\Go-work\src\github.com\aws-learning\bee-go\sitepointgoapp\sitepointgoapp.tar.gz
2021/04/29 15:15:21 INFO     ▶ 0006 Excluding relpath prefix: .
2021/04/29 15:15:21 INFO     ▶ 0007 Excluding relpath suffix: .go:.DS_Store:.tmp:go.mod:go.sum
2021/04/29 15:15:24 SUCCESS  ▶ 0008 Application packed!    
```

2.***bee bale*** - This command is currently only available to the developer team. It is used to compress all static files in to a single binary file so that they do not need to carry static files including js, css, images and views when publishing the project. Those files will be self-extracting with non-overwrite when the program starts

3.***bee version*** - This command displays the version of bee, beego, and go

4.***bee generate*** - generate the particular scaffoldname

## References

- [What is Beego](https://beego.me/docs/intro/)
- [Go: Building Web Applications with Beego](https://www.sitepoint.com/go-building-web-applications-beego/)
- [Go: Building Web Applications With Beego - Part 2](https://www.sitepoint.com/go-building-web-applications-beego-part-2/)
- [Code](https://github.com/krpmurugan/golearning/tree/master/bee-go)
- [Beego Commands](https://beego.me/docs/install/bee.md#:~:text=Command%20new,%3E%20under%20%24GOPATH%2Fsrc%20)