dokcer file, docker compose, psql, api, json
This commit is contained in:
parent
2b5135af8a
commit
a3536ef159
8
Dockerfile
Normal file
8
Dockerfile
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
FROM golang:alpine as builder
|
||||||
|
|
||||||
|
RUN mkdir -p /go/src/app
|
||||||
|
WORKDIR /go/src/app
|
||||||
|
|
||||||
|
ADD . /go/src/app
|
||||||
|
|
||||||
|
RUN go get -d -v
|
14
README.md
14
README.md
|
@ -1,3 +1,15 @@
|
||||||
# golang-postgres-docker
|
|
||||||
|
|
||||||
Just basic golang and postgres docker poc.
|
Just basic golang and postgres docker poc.
|
||||||
|
|
||||||
|
To build docker compose
|
||||||
|
`docker-compose up --build`
|
||||||
|
|
||||||
|
To run docker compose
|
||||||
|
`docker-compose up -d`
|
||||||
|
|
||||||
|
|
||||||
|
To connect with psql using following command
|
||||||
|
`docker exec -it genity-code-review_db_1 psql -U root_user -W db_genity`
|
||||||
|
|
||||||
|
|
||||||
|
|
70
api.go
Normal file
70
api.go
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
UserID int
|
||||||
|
Title string
|
||||||
|
CreatedOn time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type Users struct {
|
||||||
|
users []User
|
||||||
|
}
|
||||||
|
|
||||||
|
func UserHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
users := Users{}
|
||||||
|
|
||||||
|
err := queryRepos(&users)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
out, err := json.Marshal(users.users)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
func queryRepos(users *Users) error {
|
||||||
|
rows, err := db.Query(`
|
||||||
|
SELECT
|
||||||
|
user_id,
|
||||||
|
title,
|
||||||
|
created_on
|
||||||
|
|
||||||
|
FROM users;
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
user := User{}
|
||||||
|
err = rows.Scan(
|
||||||
|
&user.UserID,
|
||||||
|
&user.Title,
|
||||||
|
&user.CreatedOn,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
users.users = append(users.users, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rows.Err()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
4
db_dump
Normal file
4
db_dump
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
CREATE TABLE users ( user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);
|
||||||
|
INSERT INTO users (title,created_on) VALUES('hemani','2013-06-01');
|
||||||
|
|
||||||
|
|
30
docker-compose.yml
Normal file
30
docker-compose.yml
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
image: postgres
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: db_genity
|
||||||
|
POSTGRES_USER: root_user
|
||||||
|
POSTGRES_PASSWORD: shahzad12
|
||||||
|
volumes:
|
||||||
|
- db_postgres:/var/lib/postgresql/data
|
||||||
|
ports:
|
||||||
|
- "5436:5432"
|
||||||
|
networks:
|
||||||
|
- genity_network
|
||||||
|
app:
|
||||||
|
build: ""
|
||||||
|
command: [ "go", "run", "main.go","api.go"]
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
links:
|
||||||
|
- db
|
||||||
|
networks:
|
||||||
|
- genity_network
|
||||||
|
volumes:
|
||||||
|
db_postgres:
|
||||||
|
networks:
|
||||||
|
genity_network:
|
||||||
|
driver: bridge
|
7
go.mod
Normal file
7
go.mod
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
module genity-code-review
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/lib/pq v1.8.0
|
||||||
|
)
|
28
go.sum
Normal file
28
go.sum
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
|
||||||
|
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||||
|
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
|
||||||
|
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
|
||||||
|
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
|
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||||
|
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||||
|
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||||
|
github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=
|
||||||
|
github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg=
|
||||||
|
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
65
main.go
Normal file
65
main.go
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
|
const (
|
||||||
|
dbhost = "DBHOST"
|
||||||
|
dbport = "DBPORT"
|
||||||
|
dbuser = "DBUSER"
|
||||||
|
dbpass = "DBPASS"
|
||||||
|
dbname = "DBNAME"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
initDb()
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
http.HandleFunc("/api/users", UserHandler)
|
||||||
|
|
||||||
|
fmt.Println("Listening on :8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func initDb() {
|
||||||
|
config := dbConfig()
|
||||||
|
var err error
|
||||||
|
psqlInfo := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
|
||||||
|
config[dbuser], config[dbpass],
|
||||||
|
config[dbhost], config[dbport], config[dbname])
|
||||||
|
|
||||||
|
fmt.Println(psqlInfo)
|
||||||
|
db, err = sql.Open("postgres", psqlInfo)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = db.Ping()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Successfully connected!")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func dbConfig() map[string]string {
|
||||||
|
conf := make(map[string]string)
|
||||||
|
conf[dbhost] = "genity-code-review_db_1"
|
||||||
|
conf[dbport] = "5432"
|
||||||
|
conf[dbuser] = "root_user"
|
||||||
|
conf[dbpass] = "shahzad12"
|
||||||
|
conf[dbname] = "db_genity"
|
||||||
|
return conf
|
||||||
|
}
|
Loading…
Reference in a new issue