Skip to content

Commit d84bb17

Browse files
committed
create http server
1 parent b0f7692 commit d84bb17

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
main
3+
.DS_Store

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:latest as build-env
2+
3+
ENV GO111MODULE=on
4+
ENV BUILDPATH=github.com/kplcloud/world
5+
#ENV GOPROXY=goproxy.io
6+
ENV GOPATH=/go
7+
RUN mkdir -p /go/src/${BUILDPATH}
8+
COPY ./ /go/src/${BUILDPATH}
9+
RUN cd /go/src/${BUILDPATH} && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go install -v
10+
11+
FROM alpine:latest
12+
13+
COPY --from=build-env /go/bin/world /go/bin/world
14+
WORKDIR /go/bin/
15+
CMD ["/go/bin/world"]

main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"io"
6+
"os"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
12+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
13+
resp, err := http.Get("http://hello:8080")
14+
if err != nil {
15+
io.WriteString(w, err.Error())
16+
return
17+
}
18+
defer resp.Body.Close()
19+
20+
body, err := ioutil.ReadAll(resp.Body)
21+
if err != nil {
22+
io.WriteString(w, err.Error())
23+
return
24+
}
25+
26+
io.WriteString(w, "name: "+os.Getenv("HOSTNAME")+" ---> "+string(body))
27+
})
28+
29+
http.HandleFunc("/name", func(writer http.ResponseWriter, request *http.Request) {
30+
io.WriteString(writer, "this HOSTNAME is: " + os.Getenv("HOSTNAME"))
31+
})
32+
33+
if err := http.ListenAndServe(":8080", nil); err != nil {
34+
panic(err)
35+
}
36+
}

0 commit comments

Comments
 (0)