add nginx-golang-postgres application sample
Signed-off-by: Anca Iordache <anca.iordache@docker.com>master
parent
a410c8bc62
commit
0d06c37791
@ -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"]
|
@ -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
|
||||
)
|
@ -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
|
||||
}
|
@ -0,0 +1 @@
|
||||
db-frknz
|
@ -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
|
@ -0,0 +1,2 @@
|
||||
FROM nginx:1.13-alpine
|
||||
COPY conf /etc/nginx/conf.d/default.conf
|
@ -0,0 +1,8 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
location / {
|
||||
proxy_pass http://backend:8000;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue