topic/cleaning #4
4
Makefile
4
Makefile
|
@ -1,5 +1,3 @@
|
|||
export NAME=genityapp
|
||||
|
||||
build:
|
||||
docker-compose build
|
||||
|
||||
|
@ -8,3 +6,5 @@ run: build
|
|||
|
||||
build-binary:
|
||||
GOOS=linux GOARCH=amd64 go build
|
||||
|
||||
.PHONY: build-binary run build
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
_ "github.com/lib/pq"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"genityapi"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
|
3
db.env
3
db.env
|
@ -1,3 +0,0 @@
|
|||
POSTGRES_DB=db_genity
|
||||
POSTGRES_USER=root_user
|
||||
POSTGRES_PASSWORD=shahzad12
|
|
@ -1 +0,0 @@
|
|||
CREATE TABLE users (user_id serial PRIMARY KEY, title VARCHAR ( 50 ) UNIQUE NOT NULL, created_on TIMESTAMP NOT NULL);
|
|
@ -1 +0,0 @@
|
|||
INSERT INTO users (title,created_on) VALUES('shahzad','2013-06-01');
|
|
@ -1,13 +1,12 @@
|
|||
version: '3'
|
||||
version: "3.8"
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
env_file:
|
||||
- ./db.env
|
||||
env_file: ./configs/db.env
|
||||
volumes:
|
||||
- ./db_postgres:/var/lib/postgresql/data
|
||||
- ./ddl_01.sql:/docker-entrypoint-initdb.d/10_schema.sql
|
||||
- ./dml_01.sql:/docker-entrypoint-initdb.d/20_insert.sql
|
||||
- ./scripts/db/ddl_01.sql:/docker-entrypoint-initdb.d/10_schema.sql
|
||||
- ./scripts/db/dml_01.sql:/docker-entrypoint-initdb.d/20_insert.sql
|
||||
expose:
|
||||
- "5432"
|
||||
restart: always
|
||||
|
@ -16,8 +15,7 @@ services:
|
|||
web:
|
||||
build: ""
|
||||
command: [ "go", "run", "main.go","api.go"]
|
||||
env_file:
|
||||
- ./web.env
|
||||
env_file: ./configs/web.env
|
||||
ports:
|
||||
- "5000:5000"
|
||||
restart: always
|
||||
|
|
100
genityapp/api.go
100
genityapp/api.go
|
@ -1,100 +0,0 @@
|
|||
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
|
||||
}
|
Loading…
Reference in a new issue