Compare commits

...

2 commits

Author SHA1 Message Date
Şhah İSMAİL da0643ee96
Project dir restructing WIP 2 2020-09-11 12:13:41 +03:00
Şhah İSMAİL c20c3adf6d
Project dir restructing WIP 2020-09-11 12:13:09 +03:00
12 changed files with 181 additions and 12 deletions

View file

@ -1,5 +1,3 @@
export NAME=genityapp
build: build:
docker-compose build docker-compose build
@ -7,4 +5,6 @@ run: build
docker-compose up docker-compose up
build-binary: build-binary:
GOOS=linux GOARCH=amd64 go build GOOS=linux GOARCH=amd64 go build
.PHONY: build-binary run build

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

View file

@ -6,6 +6,8 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
"log" "log"
"net/http" "net/http"
"genityapi"
) )
var db *sql.DB var db *sql.DB

View file

@ -1,5 +1,5 @@
COMPOSE_PROJECT_NAME=genitywebapp COMPOSE_PROJECT_NAME=genitywebapp
dbhost=genityapp_db_1" dbhost=genityapp_db_1
dbport=5432 dbport=5432
dbuser=root_user dbuser=root_user
dbpass=shahzad12 dbpass=shahzad12

View file

@ -1,13 +1,12 @@
version: '3' version: "3.8"
services: services:
db: db:
image: postgres image: postgres
env_file: env_file: ./configs/db.env
- ./db.env
volumes: volumes:
- ./db_postgres:/var/lib/postgresql/data - ./db_postgres:/var/lib/postgresql/data
- ./ddl_01.sql:/docker-entrypoint-initdb.d/10_schema.sql - ./scripts/db/ddl_01.sql:/docker-entrypoint-initdb.d/10_schema.sql
- ./dml_01.sql:/docker-entrypoint-initdb.d/20_insert.sql - ./scripts/db/dml_01.sql:/docker-entrypoint-initdb.d/20_insert.sql
expose: expose:
- "5432" - "5432"
restart: always restart: always
@ -16,8 +15,7 @@ services:
web: web:
build: "" build: ""
command: [ "go", "run", "main.go","api.go"] command: [ "go", "run", "main.go","api.go"]
env_file: env_file: ./configs/web.env
- ./web.env
ports: ports:
- "5000:5000" - "5000:5000"
restart: always restart: always

2
go.mod
View file

@ -1,4 +1,4 @@
module genity-code-review module genityapi
go 1.14 go 1.14

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
}