Compare commits
3 commits
2b5135af8a
...
4db6f0f6c4
Author | SHA1 | Date | |
---|---|---|---|
Şhah İSMAİL | 4db6f0f6c4 | ||
4b10214a53 | |||
a3536ef159 |
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -22,7 +22,8 @@
|
|||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
@ -57,4 +58,5 @@ Temporary Items
|
|||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
go.sum
|
||||
|
||||
|
|
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
|
16
README.md
16
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
|
||||
)
|
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