Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Last active February 1, 2016 14:43
Show Gist options
  • Save vitkarpov/75fa37f6438222e444af to your computer and use it in GitHub Desktop.
Save vitkarpov/75fa37f6438222e444af to your computer and use it in GitHub Desktop.
Simple microservice in Go

Let we have a simple problem to demonstare the way of thinking.

getting image stub (for instance, white noise) with the given sizes.

Let's define some requirements which seem make a program more flexible to use:

  • we should have a package to use the module inside another solid go program
  • it could be used via http api if we have a microservice architecture
  • it could be used via cli if we have a pipeline

Architecture could be like this:

  • we have a lorempixum package
  • we have a microservice in go which uses the lorempixum
  • we have a standalone program which uses the same package and could be a part of pipeline

Unix way! :)

Try within Internet:

$ curl lorempixum.herokuapp.com/jpeg/200/200 > white-noise-200x200.jpg

If we need it via cli, let's write a tiny program uses the lorempixum:

package main

import (
	"flag"
	"github.com/vitkarpov/lorempixum"
	"os"
)

func main() {
	w := flag.Int("width", 0);
	h := flag.Int("height", 0);
	flag.Parse();

	// get the pointer to the image
	img := lorempixum.GetImage(*w, *h)
	// stream it to the standart output
	lorempixum.StreamImage(os.Stdout, img, "jpeg")
}

now we can use it inside pipelines:

$ lorempixum -width=100 -height=100 > white-noise-100x100.jpg

If you need a junior golang developer — drop me a line: [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment