diff --git a/.gitignore b/.gitignore index 95464b0..fd256b1 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..eab6d63 --- /dev/null +++ b/Dockerfile @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 7f5e2f4..179ea9c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ -# golang-postgres-docker -Just basic golang and postgres docker poc. \ No newline at end of file + +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` + + diff --git a/api.go b/api.go new file mode 100644 index 0000000..87ff945 --- /dev/null +++ b/api.go @@ -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 +} diff --git a/db_dump b/db_dump new file mode 100644 index 0000000..9f9dd07 --- /dev/null +++ b/db_dump @@ -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'); + + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..22eb0de --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3b7747b --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module genity-code-review + +go 1.14 + +require ( + github.com/lib/pq v1.8.0 +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..6bf198a --- /dev/null +++ b/main.go @@ -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 +}