issue 4, 5 ,6 and 9 fixed
This commit is contained in:
parent
f0f58efe36
commit
7e9df9b052
|
@ -1,8 +1,12 @@
|
|||
FROM golang:alpine as builder
|
||||
|
||||
RUN mkdir -p /go/src/app
|
||||
WORKDIR /go/src/app
|
||||
WORKDIR /go/src/app/cmd
|
||||
|
||||
ADD . /go/src/app
|
||||
|
||||
RUN go get -d -v
|
||||
|
||||
Run go build -o bins/genityapp
|
||||
|
||||
CMD ["bins/./genityapp"]
|
|
@ -1,62 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"domain/genityapp/genityapi"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
22
cmd/main.go
Normal file
22
cmd/main.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"domain/genityapp/internal/app"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app.InitDb()
|
||||
|
||||
defer app.CloseDB()
|
||||
|
||||
http.HandleFunc("/get-data", app.UserGetHandler)
|
||||
http.HandleFunc("/post-data", app.UserPostHandler)
|
||||
|
||||
fmt.Println("Listening on :5000")
|
||||
log.Fatal(http.ListenAndServe(":5000", nil))
|
||||
|
||||
}
|
|
@ -14,7 +14,6 @@ services:
|
|||
- genity_network
|
||||
web:
|
||||
build: ""
|
||||
command: [ "go", "run", "main.go","api.go"]
|
||||
env_file: ./configs/web.env
|
||||
ports:
|
||||
- "5000:5000"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -77,7 +77,7 @@ VALUES ($1, $2, $3)
|
|||
RETURNING user_id`
|
||||
if len(title) > 0 {
|
||||
user_id := 0
|
||||
row := db.QueryRow(sqlInsert, title, time.Now().Format(time.RFC3339), uuid.String())
|
||||
row := Db.QueryRow(sqlInsert, title, time.Now().Format(time.RFC3339), uuid.String())
|
||||
err := row.Scan(&user_id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -102,7 +102,7 @@ func queryRepos(users *Users, userTitle string) error {
|
|||
WHERE title='` + userTitle + `';
|
||||
`
|
||||
|
||||
rows, err := db.Query(query)
|
||||
rows, err := Db.Query(query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
|
@ -1,15 +1,10 @@
|
|||
package main
|
||||
package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
const (
|
||||
dbhost = "DBHOST"
|
||||
dbport = "DBPORT"
|
||||
|
@ -18,19 +13,9 @@ const (
|
|||
dbname = "DBNAME"
|
||||
)
|
||||
|
||||
func main() {
|
||||
initDb()
|
||||
defer db.Close()
|
||||
var Db *sql.DB
|
||||
|
||||
http.HandleFunc("/get-data", UserGetHandler)
|
||||
http.HandleFunc("/post-data", UserPostHandler)
|
||||
|
||||
fmt.Println("Listening on :5000")
|
||||
log.Fatal(http.ListenAndServe(":5000", nil))
|
||||
|
||||
}
|
||||
|
||||
func initDb() {
|
||||
func InitDb() {
|
||||
config := dbConfig()
|
||||
var err error
|
||||
psqlInfo := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
|
||||
|
@ -38,17 +23,21 @@ func initDb() {
|
|||
config[dbhost], config[dbport], config[dbname])
|
||||
|
||||
fmt.Println(psqlInfo)
|
||||
db, err = sql.Open("postgres", psqlInfo)
|
||||
Db, err = sql.Open("postgres", psqlInfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = db.Ping()
|
||||
err = Db.Ping()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("Successfully connected!")
|
||||
}
|
||||
|
||||
func CloseDB() {
|
||||
Db.Close()
|
||||
}
|
||||
|
||||
func dbConfig() map[string]string {
|
||||
conf := make(map[string]string)
|
||||
conf[dbhost] = "genitywebapp_db_1"
|
|
@ -1,128 +0,0 @@
|
|||
package genityapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
uuid "github.com/nu7hatch/gouuid"
|
||||
"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" {
|
||||
|
||||
title, ok := req.URL.Query()["title"]
|
||||
|
||||
if !ok || len(title[0]) < 1 {
|
||||
http.Error(w, "Url Param 'title' is missing", 400)
|
||||
return
|
||||
}
|
||||
|
||||
// Query()["key"] will return an array of items,
|
||||
// we only want the single item.
|
||||
userTitle := title[0]
|
||||
|
||||
users := Users{}
|
||||
|
||||
err := queryRepos(&users, userTitle)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
if len(users.users) > 0 {
|
||||
|
||||
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, "User does not exist")
|
||||
}
|
||||
} 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
|
||||
}
|
||||
|
||||
uuid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
||||
}
|
||||
|
||||
title := req.FormValue("title")
|
||||
sqlInsert := `
|
||||
INSERT INTO users (title, created_on,uuid)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING user_id`
|
||||
if len(title) > 0 {
|
||||
user_id := 0
|
||||
row := db.QueryRow(sqlInsert, title, time.Now().Format(time.RFC3339), uuid.String())
|
||||
err := row.Scan(&user_id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Fprintf(w, "user created.")
|
||||
} else {
|
||||
fmt.Fprintf(w, "Invalid data")
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "Sorry, only POST methods are supported for thus api.")
|
||||
}
|
||||
}
|
||||
|
||||
func queryRepos(users *Users, userTitle string) error {
|
||||
query := `
|
||||
SELECT
|
||||
user_id,
|
||||
title,
|
||||
created_on
|
||||
|
||||
FROM users
|
||||
WHERE title='` + userTitle + `';
|
||||
`
|
||||
|
||||
rows, err := db.Query(query)
|
||||
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