Create a REST service using Go Language and BeeGo Framework.
Golang
Go is an Open Source programming language developed by Google Inc.
To install go latest:
wget https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz
Unzip in /usr/local directory:
sudo tar -C /usr/local -xzf go1.9.1.linux-amd64.tar.gz
tar -C ./ -xzf go1.9.1.linux-amd64.tar.gz
Then, modify /etc/profile file, add this line:
export PATH=$PATH:/usr/local/go/bin
also, add these lines in /home/$USER/.profile file:
export GOPATH=$HOME/golangProjects
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Installing some libraries or dependencies for Golang
Web Frameworks
BeeGo
BeeGo is a Web Framework to develop using Golang Web Applications or RESTful services, you can find more information pressing here.
You can install on local writting this on terminal:
go get github.com/astaxie/beego
go get github.com/beego/bee
Then, move the golangProjects/bin/bee binary to go/bin directory.
REST Service
REST(REpresentational State Transfer) is an architecture that use HTTP protocol which provide an API with methods like GET, POST, PUT, DELETE, etc.
We use BeeGo framework and mgo driver to create a REST service on Golang using MongoDB:
We use BeeGo framework and mgo driver to create a REST service on Golang using MongoDB:
- First, once installed go and beego framework binaries, creates a directory and write in shell:
bee api myapi
The tree directory is like: - Second, you should download the database drivers, in my case is MongoDB.
DataBases Drivers
mgo
Is the MongoDB Driver for Golang. For more information or Doc press here.
To install, write on terminal:
To install, write on terminal:
go get gopkg.in/mgo.v2
go get gopkg.in/check.v1
-
I use a Singleton Pattern to connect to database, like this:
type singletonMongoDBSession struct { session *mgo.Session errDial error } var instance *singletonMongoDBSession var once sync.Once func GetSessErrMongoDBSession(dialInfo string) (*mgo.Session, error){ var instance *singletonMongoDBSession if dialInfo == "Dial"{ instance = NewMongoDBSession() } else if dialInfo == "DialWithInfo"{ instance = NewMongoDBSessionInfo() } return instance.session, instance.errDial } func NewMongoDBSession() (*singletonMongoDBSession) { once.Do(func() { sess, err := mgo.Dial(urldb) instance = &singletonMongoDBSession{session: sess, errDial: err} }) return instance }
Then, my model is like:type Login struct{ Username string `json:"username"` Password string `json:"password"` } var login Login func init(){} func VerifyLogin(object Login) (ObjectId bool, err error) { objlogin := loginRequest(object) if objlogin == (Login{}) { fmt.Printf("\nNo se encontro Usuario !\n\n") return false, nil } else { return true, nil } }
I modify some files and add directories to make more understandable how you can separates service or information.You can see my restapi-BeeGo example, just pressing there :D .
Another Packages for develop in Golang
Communitacion Protocols
MQTT Paho Golang
Is the MQTT library for Golang. For more information press here.
To install, write on terminal:
To install, write on terminal:
go get github.com/eclipse/paho.mqtt.golang
To configure Mosquitto service, you can check my tuto pressing here.
Mail Service
GoMail
Is the SMTP library for Golang. For more information press here.
To install, write on terminal:
To install, write on terminal:
go get gopkg.in/gomail.v2
Source : https://jenazad.github.io/frameworks/Create-a-REST-service-using-Go-Language-and-BeeGo-Framework
Posting Komentar untuk "Create a REST service using Go Language and BeeGo Framework."