Project dir restructing WIP 2

This commit is contained in:
Şhah İSMAİL 2020-09-11 12:13:41 +03:00
parent c20c3adf6d
commit da0643ee96
Signed by: shah
GPG key ID: B2B1222785F81AA8
8 changed files with 280 additions and 0 deletions

100
api.go Normal file
View file

@ -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
}

8
build/Dockerfile Normal file
View 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

3
configs/db.env Normal file
View file

@ -0,0 +1,3 @@
POSTGRES_DB=db_genity
POSTGRES_USER=root_user
POSTGRES_PASSWORD=shahzad12

6
configs/web.env Normal file
View file

@ -0,0 +1,6 @@
COMPOSE_PROJECT_NAME=genitywebapp
dbhost=genityapp_db_1
dbport=5432
dbuser=root_user
dbpass=shahzad12
dbname=db_genity

View file

@ -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
}

61
main.go Normal file
View file

@ -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
}

1
scripts/db/ddl_01.sql Normal file
View file

@ -0,0 +1 @@
CREATE TABLE users (user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);

1
scripts/db/dml_01.sql Normal file
View file

@ -0,0 +1 @@
INSERT INTO users (title,created_on) VALUES('shahzad','2013-06-01');