From da0643ee96bd860dd51ca2ec49cea27b0df4c8b5 Mon Sep 17 00:00:00 2001 From: Shah Ismail Date: Fri, 11 Sep 2020 12:13:41 +0300 Subject: [PATCH] Project dir restructing WIP 2 --- api.go | 100 ++++++++++++++++++++++++++++++++++ build/Dockerfile | 8 +++ configs/db.env | 3 + configs/web.env | 6 ++ internal/app/genityapp/api.go | 100 ++++++++++++++++++++++++++++++++++ main.go | 61 +++++++++++++++++++++ scripts/db/ddl_01.sql | 1 + scripts/db/dml_01.sql | 1 + 8 files changed, 280 insertions(+) create mode 100644 api.go create mode 100644 build/Dockerfile create mode 100644 configs/db.env create mode 100644 configs/web.env create mode 100644 internal/app/genityapp/api.go create mode 100644 main.go create mode 100644 scripts/db/ddl_01.sql create mode 100644 scripts/db/dml_01.sql diff --git a/api.go b/api.go new file mode 100644 index 0000000..4bcc3dc --- /dev/null +++ b/api.go @@ -0,0 +1,100 @@ +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 UserGetHandler(w http.ResponseWriter, req *http.Request) { + if req.Method == "GET" { + 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)) + } else { + fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.") + } +} + +func UserPostHandler(w http.ResponseWriter, req *http.Request) { + if req.Method == "POST" { + if err := req.ParseForm(); err != nil { + fmt.Fprintf(w, "ParseForm() err: %v", err) + return + } + title := req.FormValue("title") + sqlInsert := ` +INSERT INTO users (title, created_on) +VALUES ($1, $2) +RETURNING user_id` + if len(title) > 0 { + user_id := 0 + row := db.QueryRow(sqlInsert, title, time.Now()) + err := row.Scan(&user_id) + if err != nil { + panic(err) + } + fmt.Fprintf(w, "user creted") + } else { + fmt.Fprintf(w, "Invalid data") + } + } else { + fmt.Fprintf(w, "Sorry, only POST methods are supported for thus api.") + } +} + +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/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000..eab6d63 --- /dev/null +++ b/build/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/configs/db.env b/configs/db.env new file mode 100644 index 0000000..b81c7e3 --- /dev/null +++ b/configs/db.env @@ -0,0 +1,3 @@ +POSTGRES_DB=db_genity +POSTGRES_USER=root_user +POSTGRES_PASSWORD=shahzad12 \ No newline at end of file diff --git a/configs/web.env b/configs/web.env new file mode 100644 index 0000000..4625515 --- /dev/null +++ b/configs/web.env @@ -0,0 +1,6 @@ +COMPOSE_PROJECT_NAME=genitywebapp +dbhost=genityapp_db_1 +dbport=5432 +dbuser=root_user +dbpass=shahzad12 +dbname=db_genity \ No newline at end of file diff --git a/internal/app/genityapp/api.go b/internal/app/genityapp/api.go new file mode 100644 index 0000000..214c42e --- /dev/null +++ b/internal/app/genityapp/api.go @@ -0,0 +1,100 @@ +package genityapi + +import ( + "encoding/json" + "fmt" + "net/http" + "time" +) + +type User struct { + UserID int + Title string + CreatedOn time.Time +} + +type Users struct { + users []User +} + +func UserGetHandler(w http.ResponseWriter, req *http.Request) { + if req.Method == "GET" { + 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)) + } else { + fmt.Fprintf(w, "Sorry, only Get methods are supported for thus api.") + } +} + +func UserPostHandler(w http.ResponseWriter, req *http.Request) { + if req.Method == "POST" { + if err := req.ParseForm(); err != nil { + fmt.Fprintf(w, "ParseForm() err: %v", err) + return + } + title := req.FormValue("title") + sqlInsert := ` +INSERT INTO users (title, created_on) +VALUES ($1, $2) +RETURNING user_id` + if len(title) > 0 { + user_id := 0 + row := db.QueryRow(sqlInsert, title, time.Now()) + err := row.Scan(&user_id) + if err != nil { + panic(err) + } + fmt.Fprintf(w, "user creted") + } else { + fmt.Fprintf(w, "Invalid data") + } + } else { + fmt.Fprintf(w, "Sorry, only POST methods are supported for thus api.") + } +} + +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/main.go b/main.go new file mode 100644 index 0000000..10f60ef --- /dev/null +++ b/main.go @@ -0,0 +1,61 @@ +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("/get-data", UserGetHandler) + http.HandleFunc("/post-data", UserPostHandler) + + fmt.Println("Listening on :5000") + log.Fatal(http.ListenAndServe(":5000", 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] = "genitywebapp_db_1" + conf[dbport] = "5432" + conf[dbuser] = "root_user" + conf[dbpass] = "shahzad12" + conf[dbname] = "db_genity" + return conf +} diff --git a/scripts/db/ddl_01.sql b/scripts/db/ddl_01.sql new file mode 100644 index 0000000..e9b8a1d --- /dev/null +++ b/scripts/db/ddl_01.sql @@ -0,0 +1 @@ +CREATE TABLE users (user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL); \ No newline at end of file diff --git a/scripts/db/dml_01.sql b/scripts/db/dml_01.sql new file mode 100644 index 0000000..0ee150a --- /dev/null +++ b/scripts/db/dml_01.sql @@ -0,0 +1 @@ +INSERT INTO users (title,created_on) VALUES('shahzad','2013-06-01'); \ No newline at end of file