2.7.18

React.js frontends for your golang services.

In case you don't already know, Golang is really solid for middleware, b/c of its nonblocking i/o built into the language.  Heres how to setup a fronted.

1) Find a good JS dev, have em bootstrap a react.js app, now you have a directory

2)  With the Dockerfile, looking like this:

FROM node:carbon

WORKDIR /

ADD . /app/

# Copy your golang binary executable server app ...
COPY cn_crd_controller /cn_crd_controller

RUN cd /app/ && npm i && npm run build

RUN mv /app/build/ /views/

CMD ["./cn_crd_controller"]


3) Grab gin as middleware, and do something like this:

gin "github.com/gin-gonic/gin"

log "github.com/sirupsen/logrus"
)

func SetupHTTPServer(responder Responder) {

go func() {
router := gin.Default()
var err error
// Serve frontend static files
router.Use(static.Serve("/", static.LocalFile("/views", true)))
4) Add your real backend stuff (i.e. REST API stuff and so on) using standard gin stuff, like 


router.GET("/model", func(c *gin.Context) {
recordRequest("/model", "GET")
request := &GetModelRequest{Success: make(chan *Model)}
responder.GetModel(request) // tell the responder to write the model when it can.
model := <-request .success="" a="" channel="" div="" have="" is="" model.="" nbsp="" once="" success="" to="" we="" written="">
c.JSON(200, model)
})

THATS IT NOW YOU HAVE A FRONTEND !

1 comment: