beego

Leveraging Nexus when Building a Golang Application

For this example, I’m creating a very simple Golang app

package main

import (
        "github.com/beego/beego/v2/server/web"
)

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

type MainController struct {
        web.Controller
}

func (c *MainController) Get() {
        response := map[string]string{
                "message": "Hello World Beego",
                "status":  "success",
        }
        c.Data["json"] = response
        c.ServeJSON()
}

And here’s the required go.mod file:

module hello-beego

go 1.24.7

require github.com/beego/beego/v2 v2.3.8

Next, we’ll set up our Nexus to store the required Golang libraries. First, we start by creating a repository named go-proxy. The remote storage location for this repository will be proxy.golang.org.

We also need to point our Golang build proxy to the Nexus repository by exporting the GOPROXY variable.

$ export GOPROXY= http://localhost:8081/repository/go-proxy/

For more detailed logs, we can pull all our libraries using the following command:

$ GODEBUG=mod=1,gocacheverify=1 go mod tidy -v -x

This will show whether we’re able to pull libraries from the Nexus repository. We can also see the libraries that are being pulled in our Nexus repository.

By following above steps, we can successfully leverage Nexus to manage our Golang dependencies, ensuring a more efficient and controlled build environment.

A Beego Golang Framework on top of Openshift 4

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