Skip to content

Instantly share code, notes, and snippets.

@matisojka
Created March 5, 2016 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matisojka/8712942c28a143c25967 to your computer and use it in GitHub Desktop.
Save matisojka/8712942c28a143c25967 to your computer and use it in GitHub Desktop.
Tiny Go microservice plumbing
FROM alpine:3.2
ENV GOROOT=/usr/lib/go \
GOPATH=/gopath \
GOBIN=/gopath/bin \
PATH=$PATH:$GOROOT/bin:$GOPATH/bin
WORKDIR /gopath/src/app
ADD . /gopath/src/app
RUN apk add -U git go && \
go get -v app && \
apk del git go && \
rm -rf /gopath/pkg && \
rm -rf /gopath/src && \
rm -rf /var/cache/apk/*
ENTRYPOINT ["/gopath/bin/app"]
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func helloHandler(res http.ResponseWriter, req *http.Request) {
res.Header().Set(
"Content-Type",
"text/html",
)
io.WriteString(
res,
`<doctype html>
<html>
<head>
<title>Hello Gopher</title>
</head>
<body>
Hello Gopher </br>
It is really awesome that both Docker and Kubernetes are written in Go!
</body>
</html>`,
)
}
func defaultHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Go web app powered by Docker")
}
func main() {
http.HandleFunc("/", defaultHandler)
http.HandleFunc("/hello", helloHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
return
}
}
docker build -t go-micro .
docker run --rm -p 8080:8080 go-micro
$ docker images | grep go-micro
go-micro latest afb298c5c754 17 minutes ago 11.43 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment