How to use SSL with OCSP with GIN Framework in Go

The GIN framework comes up with the SSL middleware autotls which requests the SSL certificate on request basis. However, it lacks the cool feature of OCSP

However, the CertMagic which generates SSL certificate during initializations – so no surprises here and does support OCSP.


app := gin.Default()
...

// Assuming SSL_DOMAIN has csv domains like varunbatra.com,www.varunbatra.com
domains := os.Getenv("SSL_DOMAIN")
domainsMap := strings.Split(domains, ",")
err := certmagic.HTTPS(domainsMap, app)
if err != nil {
	log.Fatal(err)
}

Containerize your first Go Lang App using Docker

Does size matter?

It does – in production. Most orchestration solutions involve a smoother transitions from one docker image to other. A simple up command, if downloading only ~10MB of file, obviously it will save few seconds – but not just that, what about limited size we have in our production servers? This normally leads to the regular cleanups which is usually a manual and tedious tasks. So, for a scalability point of view, a multi-stage build approach is preferred over usual.

The Multi stage build Approach

Most of the approaches are quite poor as it ends up with a large docker image. New approach involves having a two steps approach as following:

The Build:

You build Go in this system and use that to build your application. This will result in an insane size of docker image ~ from 200MB to 300MB

Copy Build

You copy the build from first step and this ensures size of final docker image is less. I bet you will have no more than 10MB image size.

Example:

# The Build
FROM golang:alpine AS the-build
ADD . /src
RUN cd /src && go build -o app

# Copy Build
FROM alpine
WORKDIR /app
COPY --from=the-build /src/app /app/
ENTRYPOINT ./app

Here you are naming the first step of the build as ‘the-build’ Docker supports a partial build on multi-build process. So if you would like to build till ‘the-build’ you can do it with:

docker build --target the-build -t github/go-app:latest

If you omit –target – you can build the whole system in a single command as following:

docker build -t github/go-app:latest

Why struct isn’t getting any data on unmarshal in Go?

Among first few problems, a go lang programmer will probably be struggling with a blank struct. Structs have blank values when one of the following things are wrong:

1. Invalid Naming Conventions:

First letter of fields in struct should be capital. For example


//Invalid name 	
type Person struct {
    name string
    age  int
}

//Valid Name
type Person struct {
    Name string
    Age  int
}

2. Invalid Matches:

Consider this is the json which we can’t change.


{
 FirstName: "John Doe",
 Age: "30",
}

 


//Results in blank data 	
type Person struct {
    FirstName string //This will match first_name but we have FirstName
    Age  int //This will match age but we have Age
}

//The fix
type Person struct {
    FirstName string `json:"FirstName"`
    Age  int `json:"Age"`
}