Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/clarenceb/jokes_api_golang

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/clarenceb/jokes_api_golang

  • v0.0.0-20141004131008-a388df171da3
  • Source
  • Go
  • Socket score

Version published
Created
Source

Jokes API example in Go lang

This is a basic example of a RESTful API which serves jokes from the internet and a commandline client to make requesting jokes easier.

This is my first ever Go program so its probably not going to be idiomatic Go lang and not a good example of a "real life" API.

The main purpose is for me to try out some of the Go concepts I have learnt about in a Tour of Go and a starting point for a Go lang Dojo at work.

The code will only use packages from the standard library.

Setup

Install Go compiler and tools: https://golang.org/doc/install

On Mac OS X, you can use Homebrew:

brew install go

Prepare your Go workspace and set your GOPATH:

$ mkdir -p $HOME/go/{src,pkg,bin}
$ export GOPATH=$HOME/go
$ echo "export GOPATH=$HOME/go" >> ~/.bashrc

Read more about Go lang workspaces.

Fetch this project into your Go workspace:

$ cd $GOPATH
$ go get github.com/clarenceb/jokes_api_golang

Using the Server

# Build the server
cd $GOPATH/src/github.com/clarenceb/jokes_api/server
go build

# Run the server (on port localhost:8080)
./server

# Test server with curl:
curl -H "Accept: application/json" http://localhost:8080/joke
# => {"joke":"Chuck Norris finished World of Warcraft."}

Using the Client

# Build the client
cd $GOPATH/src/github.com/clarenceb/jokes_api/client
go build

# View usage
./client --help

# Fetch one joke
./client
# => 1: Chuck Norris finished World of Warcraft.

# Fetch multiple jokes
./client -n 2
# => 1: No one has ever spoken during review of Chuck Norris' code and lived to tell about it.
# => 2: Chuck Norris once ate four 30lb bowling balls without chewing.

# Fetch multiple jokes concurrently
./client -n 2 -c
# => 2: Chuck Norris once ate four 30lb bowling balls without chewing.
# => 1: No one has ever spoken during review of Chuck Norris' code and lived to tell about it.

# You can time synchronous vs. concurrent requests like so:
time ./client -n 10
# => real   0m3.681s
time ./client -n 10 -c
# => real   0m0.846s

About jokes_api/server

Demonstrates:

  • HTTP RESTful API
  • JSON encoding / decoding of messages
  • Basic logging for debugging / troubleshooting
  • Error handling
  • TODO: Testing

About jokes_api/client

Demonstrates:

  • Command-line tool
  • Parsing command-line arguments
  • Making concurrent HTTP client requests (via goroutines and channels)
  • Error handling
  • Packages and splitting code over multiple files
  • TODO: Testing

Potential Improvements

  • Add some unit testing with the testing package and HTTP testing with net/http/httptest package
  • Try a BDD-style framework to describe the API, e.g. Gingko or GoConvey
  • Add some additional API endpoints (e.g. request specific joke, save/list/retrieve/delete favourite jokes for a user)
  • Add other joke APIs - not just the Internet Chuck Norris Database

Further Learning

Credits

License

The MIT License (MIT)

Copyright (c) 2014 Clarence Bakirtzidis

(see LICENSE file for details)

FAQs

Package last updated on 04 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc