How to Create a Web Service Using Render and Go | by John Vester | Feb, 2022

Render delivers on Zero DevOps’ promise

  • How do you remember such things?
  • Why do you even remember that memory?

To make things fun, consider an example where Mattel is asking to create a RESTful API that emulates the Magic 8-Ball game invented in 1946:

  • Get /answer – Randomly displays one of the 20 available answers
  • Get /answers Displays a list of all 20 available answers

While visiting the download page on the Go website, I noticed that the installation process was very similar to getting started with the Java programming language. The installer used a wizard-like design where I only had to click buttons to cycle through the process. Once done, I used the following terminal command to verify successful installation:

╭─me@johnjvester ~ 
╰─$ go version
go version go1.17.6 darwin/amd64
$ go get -u github.com/gin-gonic/gin
import "github.com/gin-gonic/gin"

The first thing I want to do is create the data side of the API. In this example, the following JSON structure will form the response body when an answer is given:

{
"id" : number,
"response" : string
}
type response struct {
ID int `json:"id"`
Response string `json:"response"`
}
var responses = []response{
{ID: 1, Response: "It is certain."},
{ID: 2, Response: "It is decidedly so."},
{ID: 3, Response: "Without a doubt."},
{ID: 4, Response: "Yes definitely."},
{ID: 5, Response: "You may rely on it."},
{ID: 6, Response: "As I see it, yes."},
{ID: 7, Response: "Most likely."},
{ID: 8, Response: "Outlook good."},
{ID: 9, Response: "Yes."},
{ID: 10, Response: "Signs point to yes."},
{ID: 11, Response: "Reply hazy, try again."},
{ID: 12, Response: "Ask again later."},
{ID: 13, Response: "Better not tell you now."},
{ID: 14, Response: "Cannot predict now."},
{ID: 15, Response: "Concentrate and ask again."},
{ID: 16, Response: "Don't count on it."},
{ID: 17, Response: "My reply is no."},
{ID: 18, Response: "My sources say no."},
{ID: 19, Response: "Outlook not so good."},
{ID: 20, Response: "Very doubtful."},
}
func getAllAnswers(c *gin.Context) {
c.IndentedJSON(http.StatusOK, responses)
}
func getRandomAnswer(c *gin.Context) {
rand.Seed(time.Now().Unix())
c.IndentedJSON(http.StatusOK, responses[rand.Intn(len(responses))])
}
func main() {
router := gin.Default()
router.GET("/answers", getAllAnswers)
router.GET("/answer", getRandomAnswer)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
if err := router.Run(":" + port); err != nil {
log.Panicf("error: %s", err)
}
}
╭─me@johnjvester ~/projects/jvc/go/magic-eight-ball 
╰─$ go run .
[GIN-debug] GET    /answers        --> main.getAllAnswers (3 handlers)
[GIN-debug] GET /answer --> main.getRandomAnswer (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
╭─me@johnjvester ~ 
╰─$ curl http://localhost:8080/answer
{
"id": 6,
"response": "As I see it, yes."
}%

Getting started with Render was quick, easy, and free. I simply chose to use my GitLab credentials, which made things easy when selecting projects to publish.

{
"id": 5,
"response": "You may rely on it."
}

The introduction to this article talks about my ability to preserve and retrieve historical events after several years (if not decades). The fact that this entire experience took less than an hour of my time – from start to finish – is definitely worth becoming another one of those events.

Leave a Comment