1 min read

Go: Serve HTTP JSON API for in-memory objects (DevLog #002)

Review the source code for MemUI's HTTP JSON API Server. Includes using net/http package, JSON Encode, and step-by-step tutorial.
Go: Serve HTTP JSON API for in-memory objects (DevLog #002)
Create HTTP Server with a JSON Endpoint using Go-lang (GoMemUI Project)

In yesterday's DevLog, we reviewed the source code for MemUI's HTTP JSON API Server. At the end of this page, you can find the recording of this video. I also published the step-by-step guide explaining each part in detail.

In this video:

  • Serve an HTTP server with a standard "net/http" package;
  • Index API to list all internal types;
  • Explore API to list all objects for a selected type;
  • Marshal to JSON;

Demo of the final result:

0:00
/
Explore JSON endpoint for go-memui

API Changes

We plan to add a new ServerHTTP method to the MemUI type so clients can execute it and serve a JSON API:

d1 := Department{Name: "IT"}
d2 := Department{Name: "HR"}

p1 := Person{"John", 30, &d1}
p2 := Person{"Mary", 25, &d2}

mui.Register(&p1, &p2, &d1, &d2)

mui.ServeHTTP()
Changes in MemUI API

Which starts the following HTTP server:

// Index page with all registered object types:
curl localhost:8080/

// Explore page with all registered objects for a selected type:
curl localhost:8080/explore?type=*main.Department
Expected HTTP Server

Please note that here *main.Department is the name of the pointer type.

YouTube Video

DevLog #2 2022 — Go: Create HTTP Server with a JSON endpoint
📚
Check the following course section for a step-by-step guide to creating HTTP Server with a JSON API.

👉🏼 Extended version of the YouTube Video

About DevLogs

DevLog series is my attempt to post a new coding video every day! It is like Daily Standup in s Scrum team where I share a) What I worked on Yesterday? b) What's a plan for today? and c) Are there any blockers or learning points? Learn more: All DevLogs.