Created
March 5, 2016 11:00
-
-
Save matisojka/8712942c28a143c25967 to your computer and use it in GitHub Desktop.
Tiny Go microservice plumbing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker build -t go-micro . | |
docker run --rm -p 8080:8080 go-micro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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