From 0d06c377913e80797b144221651126c9aa4dd0a4 Mon Sep 17 00:00:00 2001 From: Anca Iordache Date: Thu, 5 Mar 2020 17:39:16 +0100 Subject: [PATCH] add nginx-golang-postgres application sample Signed-off-by: Anca Iordache --- .../nginx-golang-postgres/backend/Dockerfile | 10 +++ samples/nginx-golang-postgres/backend/go.mod | 10 +++ samples/nginx-golang-postgres/backend/main.go | 88 +++++++++++++++++++ samples/nginx-golang-postgres/db/password.txt | 1 + .../nginx-golang-postgres/docker-compose.yaml | 29 ++++++ .../nginx-golang-postgres/proxy/Dockerfile | 2 + samples/nginx-golang-postgres/proxy/conf | 8 ++ 7 files changed, 148 insertions(+) create mode 100755 samples/nginx-golang-postgres/backend/Dockerfile create mode 100644 samples/nginx-golang-postgres/backend/go.mod create mode 100755 samples/nginx-golang-postgres/backend/main.go create mode 100644 samples/nginx-golang-postgres/db/password.txt create mode 100644 samples/nginx-golang-postgres/docker-compose.yaml create mode 100755 samples/nginx-golang-postgres/proxy/Dockerfile create mode 100755 samples/nginx-golang-postgres/proxy/conf diff --git a/samples/nginx-golang-postgres/backend/Dockerfile b/samples/nginx-golang-postgres/backend/Dockerfile new file mode 100755 index 0000000..0c7d0af --- /dev/null +++ b/samples/nginx-golang-postgres/backend/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:1.13-alpine AS build +WORKDIR /go/src/github.com/org/repo +COPY . . + +RUN go build -o server . + +FROM alpine:3.7 +EXPOSE 8000 +COPY --from=build /go/src/github.com/org/repo/server /server +CMD ["/server"] diff --git a/samples/nginx-golang-postgres/backend/go.mod b/samples/nginx-golang-postgres/backend/go.mod new file mode 100644 index 0000000..84d3a71 --- /dev/null +++ b/samples/nginx-golang-postgres/backend/go.mod @@ -0,0 +1,10 @@ +module github.com/org/repo + +go 1.13 + +require ( + github.com/gorilla/context v1.1.1 + github.com/gorilla/handlers v1.3.0 + github.com/gorilla/mux v1.6.2 + github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2 +) diff --git a/samples/nginx-golang-postgres/backend/main.go b/samples/nginx-golang-postgres/backend/main.go new file mode 100755 index 0000000..7ed5f37 --- /dev/null +++ b/samples/nginx-golang-postgres/backend/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "database/sql" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "time" + + "github.com/gorilla/handlers" + "github.com/gorilla/mux" + _ "github.com/lib/pq" +) + +func connect() (*sql.DB, error) { + bin, err := ioutil.ReadFile("/run/secrets/db-password") + if err != nil { + return nil, err + } + return sql.Open("postgres", fmt.Sprintf("postgres://postgres:%s@db:5432/example?sslmode=disable", string(bin))) +} + +func blogHandler(w http.ResponseWriter, r *http.Request) { + db, err := connect() + if err != nil { + w.WriteHeader(500) + return + } + defer db.Close() + + rows, err := db.Query("SELECT title FROM blog") + if err != nil { + w.WriteHeader(500) + return + } + var titles []string + for rows.Next() { + var title string + err = rows.Scan(&title) + titles = append(titles, title) + } + json.NewEncoder(w).Encode(titles) +} + +func main() { + log.Print("Prepare db...") + if err := prepare(); err != nil { + log.Fatal(err) + } + + log.Print("Listening 8000") + r := mux.NewRouter() + r.HandleFunc("/", blogHandler) + log.Fatal(http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stdout, r))) +} + +func prepare() error { + db, err := connect() + if err != nil { + return err + } + defer db.Close() + + for i := 0; i < 60; i++ { + if err := db.Ping(); err == nil { + break + } + time.Sleep(time.Second) + } + + if _, err := db.Exec("DROP TABLE IF EXISTS blog"); err != nil { + return err + } + + if _, err := db.Exec("CREATE TABLE IF NOT EXISTS blog (id SERIAL, title VARCHAR)"); err != nil { + return err + } + + for i := 0; i < 5; i++ { + if _, err := db.Exec("INSERT INTO blog (title) VALUES ($1);", fmt.Sprintf("Blog post #%d", i)); err != nil { + return err + } + } + return nil +} diff --git a/samples/nginx-golang-postgres/db/password.txt b/samples/nginx-golang-postgres/db/password.txt new file mode 100644 index 0000000..7ccefef --- /dev/null +++ b/samples/nginx-golang-postgres/db/password.txt @@ -0,0 +1 @@ +db-frknz \ No newline at end of file diff --git a/samples/nginx-golang-postgres/docker-compose.yaml b/samples/nginx-golang-postgres/docker-compose.yaml new file mode 100644 index 0000000..0d66498 --- /dev/null +++ b/samples/nginx-golang-postgres/docker-compose.yaml @@ -0,0 +1,29 @@ +version: "3.7" +services: + backend: + build: backend + secrets: + - db-password + depends_on: + - db + db: + environment: + POSTGRES_DB: example + POSTGRES_PASSWORD_FILE: /run/secrets/db-password + image: postgres + restart: always + secrets: + - db-password + volumes: + - db-data:/var/lib/postgresql/data + proxy: + build: proxy + ports: + - 80:80 + depends_on: + - backend +volumes: + db-data: {} +secrets: + db-password: + file: db/password.txt diff --git a/samples/nginx-golang-postgres/proxy/Dockerfile b/samples/nginx-golang-postgres/proxy/Dockerfile new file mode 100755 index 0000000..f5760d0 --- /dev/null +++ b/samples/nginx-golang-postgres/proxy/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx:1.13-alpine +COPY conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/samples/nginx-golang-postgres/proxy/conf b/samples/nginx-golang-postgres/proxy/conf new file mode 100755 index 0000000..f6d2195 --- /dev/null +++ b/samples/nginx-golang-postgres/proxy/conf @@ -0,0 +1,8 @@ +server { + listen 80; + server_name localhost; + location / { + proxy_pass http://backend:8000; + } + +}