Basic API with Golang
Go and Rust are new languages compared with other big names; C/C++ or Java. Personally, Go lang and Rust lang doesn’t compete each other. I belive they will go together since their purposes are different. Go is standing for the simplist approach. It can be easily picked by C/C++ or Java developers. Rust chooses a strict way in terms of managing low level control. Rust will be adapted in more critical situation and if they want to drop the last performance. Oneday I want to try Golang. Today is the day. In this posting, I’d like to show how I build a “hello” api server with Golang. The code is here.
Basic mechanism of HTTP web server
A server is responding to user’s request. We’ve faced 404 errors that means the request not found with the given address. That’s quite common error that many cool companies are working on in terms of design.
Errors starts with “4” (4xx) represents usually user’s fault and starts with “5” (5xx) means server side faults. There are two kinds of server we can make.
Static Server
A server only replies to the fixed format/template of the response. It doesn’t involve with processing and keep the state of user request. This blog is created by jekyll static site generator. The blog is on top of the well known theme; minimal-mistakes
Dynamic Server
The dynamic server accepts request and processes. Sometimes it keeps the information. The infromation we keep is called as a “state”. Usually, we use Databases for keeping the state.
In terms of networking each other, we use HTTP protocol. It’s a promise of communication method.
Handler and Router
Let’s imagine whe we type http://127.0.0.1:8080/hello
url on the browser.
The server instance will accept the request and find the request based on the url. In this case, server (http://127.0.0.1:8080) makes a decision to respond hello
path. The address we can use is the routing and each routing has it’s own handler function. The server developer can make various features based on the routing.
To implement routing in action, we have multiple ways with golang. This article nicely covered about the routing topic.
- Regex table - Loop through pre-compiled regexes and pass matches using the request context
- Regex switch - A switch statement with cases that call a regex-based match() helper which scans path parameters into variables
- Pattern matcher - Similar to the above, but using a simple pattern matching function instead of regexes
- Split switch - Split the path on / and then switch on the contents of the path segments
- Shift path - Axel Wagner’s hierarchical ShiftPath technique
- Library, Chi
- Library, Gorilla
- Libaray, Pat
HTTP request and response headers
In the HTTP protocol, it’s already defined what we need to fill in for the request and response headers. Which type of request it should be, authentication credentials, control for caching, or to get information about the user agent or referrer.
Marshaling data structures to JSON
“Marshaling” in this context means converting memory representation (byte stream) to the JSON object.
TLDR; Code
So far, we’ve built up necessary details for building a simple server. I used “gorilla” for routing function.
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
type Person struct {
Name string `json:"name"`
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", rootHandler) // handler
http.ListenAndServe(":80", r) // start server with a router
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
var p Person
err := json.NewDecoder(r.Body).Decode(&p) // marshaling
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "Hey "+p.Name) // response
}
The prerequisit to run this code is to install the golang within your local device. Then, create a go file with this code; main.go
and try go run main.go
.
Using the Postman, you can test your server with this body. {"name": "your name"}
Here I only have the root routing. The example response is here.
Leave a comment