add nginx-golang-mysql application sample

Signed-off-by: Anca Iordache <anca.iordache@docker.com>
This commit is contained in:
Anca Iordache 2020-03-05 17:01:59 +01:00
parent 0bf5504d67
commit 8bb4e23e28
7 changed files with 148 additions and 0 deletions

View File

@ -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"]

View File

@ -0,0 +1,10 @@
module github.com/org/repo
go 1.13
require (
github.com/go-sql-driver/mysql v1.3.0
github.com/gorilla/context v1.1.1
github.com/gorilla/handlers v1.3.0
github.com/gorilla/mux v1.6.2
)

View File

@ -0,0 +1,88 @@
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func connect() (*sql.DB, error) {
bin, err := ioutil.ReadFile("/run/secrets/db-password")
if err != nil {
return nil, err
}
return sql.Open("mysql", fmt.Sprintf("root:%s@tcp(db:3306)/example", 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 int NOT NULL AUTO_INCREMENT, title varchar(255), PRIMARY KEY (id))"); err != nil {
return err
}
for i := 0; i < 5; i++ {
if _, err := db.Exec("INSERT INTO blog (title) VALUES (?);", fmt.Sprintf("Blog post #%d", i)); err != nil {
return err
}
}
return nil
}

View File

@ -0,0 +1 @@
db-q5n2g

View File

@ -0,0 +1,29 @@
version: "3.7"
services:
backend:
build: backend
secrets:
- db-password
depends_on:
- db
db:
environment:
MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password
image: mysql:5.7
restart: always
secrets:
- db-password
volumes:
- db-data:/var/lib/mysql
proxy:
build: proxy
ports:
- 80:80
depends_on:
- backend
volumes:
db-data: {}
secrets:
db-password:
file: db/password.txt

View File

@ -0,0 +1,2 @@
FROM nginx:1.13-alpine
COPY conf /etc/nginx/conf.d/default.conf

View File

@ -0,0 +1,8 @@
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend:8000;
}
}