Golang has a lot of web frameworks and Beego is one it, it provides a rapid development of enterprise application in Go, including RESTful APIs, web apps and backend services.
The concept for this is pretty much simple, a json rest api showing helo world as response. Deployed as containerized image, on top of Kubernetes. So lets start with some Go files,
package main
import (
_ "HelloBeego/routers"
"github.com/beego/beego/v2/server/web"
)
func main() {
web.Run()
}
A router file,
package routers
import (
"HelloBeego/controllers"
"github.com/beego/beego/v2/server/web"
)
func init() {
web.Router("/", &controllers.HelloController{})
}
and a controller,
package controllers
import "github.com/beego/beego/v2/server/web"
type HelloController struct {
web.Controller
}
type helloResponse struct {
Greeting string `json:"greeting"`
}
func (u *HelloController) Get() {
user := helloResponse{}
user.Greeting = "Hello World Edwin"
u.Data["json"] = user
u.ServeJSON()
}
The tricky part is creating a multi-stage build by using Docker to build this and deploy by using a different images.
FROM registry.access.redhat.com/ubi8/go-toolset USER root RUN mkdir /build/ ADD . /build/ RUN cd /build/ go build FROM registry.access.redhat.com/ubi8/ubi-minimal COPY --from=0 /build/HelloBeego /usr/local/bin/app CMD app EXPOSE 8080
Run docker build command, deploy the containerized image into Openshift 4 and create Routes in order to make the application accessible. The sample commands are in below,
$ oc new-app . --strategy=docker $ oc create route edge --service=beego-rest-hello-world
Code can be accessed in below Github repo,
https://github.com/edwin/beego-rest-hello-world