From 6ab0a595e7613d285b524f5643e828373f881fb4 Mon Sep 17 00:00:00 2001 From: Emile Date: Thu, 27 Jun 2019 00:19:44 +0200 Subject: done --- example-service/Dockerfile | 10 ++++++++ example-service/go.mod | 3 +++ example-service/main.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 example-service/Dockerfile create mode 100644 example-service/go.mod create mode 100644 example-service/main.go (limited to 'example-service') diff --git a/example-service/Dockerfile b/example-service/Dockerfile new file mode 100644 index 0000000..8fa6104 --- /dev/null +++ b/example-service/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:latest + +WORKDIR /home +COPY *.go /home/ + +RUN go get github.com/gorilla/mux + +EXPOSE 3333 + +ENTRYPOINT go run . diff --git a/example-service/go.mod b/example-service/go.mod new file mode 100644 index 0000000..51a5da3 --- /dev/null +++ b/example-service/go.mod @@ -0,0 +1,3 @@ +module example-service + +go 1.12 diff --git a/example-service/main.go b/example-service/main.go new file mode 100644 index 0000000..bbdc35f --- /dev/null +++ b/example-service/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "net" + "log" + "os" + "fmt" +) + +const( + CONN_HOST = "localhost" + CONN_PORT = "3333" + CONN_TYPE = "tcp" +) + +func main() { + + // start a new listener + l, err := net.Listen(CONN_TYPE, CONN_HOST + ":" + CONN_PORT) + if err != nil { + log.Printf("Error listening: %v", err) + os.Exit(1) + } + + // close the listener when done + defer l.Close() + + for { + + // accept a connection + conn, err := l.Accept() + if err != nil { + log.Printf("Error accepting: %v", err) + os.Exit(1) + } + + // handle the connection in a new go thread + go handlerequest(conn) + + } +} + +func handlerequest(conn net.Conn) { + // creat a buffer storing the incomming data + buf := make([]byte, 1000) + + // read from the connection into the buffer + reqLen, err := conn.Read(buf) + if err != nil { + log.Printf("Error reading: %v", err) + os.Exit(1) + } + + // write back how many bytes were sent + conn.Write([]byte(fmt.Sprintf("Read %d bytes", reqLen))) + conn.Close() +} -- cgit 1.4.1