├── Dockerfile ├── README.md ├── Makefile ├── hello.go └── go-wrapper /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:onbuild 2 | 3 | RUN mkdir -p /go/src/app 4 | WORKDIR /go/src/app 5 | 6 | EXPOSE 8080 7 | 8 | CMD ["go-wrapper", "run"] 9 | 10 | ONBUILD COPY . /go/src/app 11 | ONBUILD RUN go-wrapper download 12 | ONBUILD RUN go-wrapper install 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sample Service Go 2 | 3 | **Sample & Simple HTTP Service in Golang** 4 | 5 | This sample 'nano' project aims to feed another one I am testing to build, deploy docker containers in a very easy way. 6 | 7 | # Running this _thing_ 8 | 9 | ## build 10 | 11 | make 12 | 13 | ## run 14 | 15 | make run 16 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: prebuild build 2 | 3 | prebuild: 4 | echo "fetch deps..." 5 | 6 | build: VERSION?=latest 7 | build: 8 | docker build -t include/sample-service-go:$(VERSION) . 9 | 10 | run: 11 | docker run -it --rm -p 8080:8080 include/sample-service-go 12 | 13 | test: 14 | echo "ok" 15 | 16 | clean: 17 | docker rmi -f $(shell docker images include/sample-service-go -aq) 18 | -------------------------------------------------------------------------------- /hello.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | func handler(w http.ResponseWriter, r *http.Request) { 10 | log.Printf("Got request with path %s", r.URL.Path[1:]) 11 | fmt.Fprint(w, "Hello from a Go web-server.") 12 | } 13 | 14 | func main() { 15 | log.Println("Started simple web server.") 16 | http.HandleFunc("/", handler) 17 | http.ListenAndServe(":8080", nil) 18 | } 19 | -------------------------------------------------------------------------------- /go-wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | usage() { 5 | base="$(basename "$0")" 6 | cat <&2 47 | exit 1 48 | fi 49 | # "shift" so that "$@" becomes the remaining arguments and can be passed along to other "go" subcommands easily 50 | cmd="$1" 51 | shift 52 | 53 | goDir="$(go list -e -f '{{.ImportComment}}' 2>/dev/null || true)" 54 | 55 | if [ -z "$goDir" -a -s .godir ]; then 56 | goDir="$(cat .godir)" 57 | fi 58 | 59 | dir="$(pwd -P)" 60 | if [ "$goDir" ]; then 61 | goPath="${GOPATH%%:*}" # this just grabs the first path listed in GOPATH, if there are multiple (which is the detection logic "go get" itself uses, too) 62 | goDirPath="$goPath/src/$goDir" 63 | mkdir -p "$(dirname "$goDirPath")" 64 | if [ ! -e "$goDirPath" ]; then 65 | ln -sfv "$dir" "$goDirPath" 66 | elif [ ! -L "$goDirPath" ]; then 67 | echo >&2 "error: $goDirPath already exists but is unexpectedly not a symlink!" 68 | exit 1 69 | fi 70 | goBin="$goPath/bin/$(basename "$goDir")" 71 | else 72 | goBin="$(basename "$dir")" # likely "app" 73 | fi 74 | 75 | case "$cmd" in 76 | download) 77 | set -- go get -v -d "$@" 78 | if [ "$goDir" ]; then set -- "$@" "$goDir"; fi 79 | set -x; exec "$@" 80 | ;; 81 | 82 | install) 83 | set -- go install -v "$@" 84 | if [ "$goDir" ]; then set -- "$@" "$goDir"; fi 85 | set -x; exec "$@" 86 | ;; 87 | 88 | run) 89 | set -x; exec "$goBin" "$@" 90 | ;; 91 | 92 | *) 93 | echo >&2 'error: unknown command:' "$cmd" 94 | usage >&2 95 | exit 1 96 | ;; 97 | esac 98 | --------------------------------------------------------------------------------