diff --git a/Dockerfile b/Dockerfile index eab6d63..16e4afb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ No newline at end of file +RUN go get -d -v + +Run go build -o bins/genityapp + +CMD ["bins/./genityapp"] \ No newline at end of file diff --git a/cmd/genityapp/main.go b/cmd/genityapp/main.go deleted file mode 100644 index 22f7c51..0000000 --- a/cmd/genityapp/main.go +++ /dev/null @@ -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 -} diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..623733f --- /dev/null +++ b/cmd/main.go @@ -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)) + +} diff --git a/docker-compose.yml b/docker-compose.yml index bab9efc..208fade 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,6 @@ services: - genity_network web: build: "" - command: [ "go", "run", "main.go","api.go"] env_file: ./configs/web.env ports: - "5000:5000" diff --git a/api.go b/internal/app/api.go similarity index 95% rename from api.go rename to internal/app/api.go index ea57ef3..d5ce527 100644 --- a/api.go +++ b/internal/app/api.go @@ -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 } diff --git a/main.go b/internal/app/db.go similarity index 65% rename from main.go rename to internal/app/db.go index 133d0f4..9cf4753 100644 --- a/main.go +++ b/internal/app/db.go @@ -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" diff --git a/internal/app/genityapp/api.go b/internal/app/genityapp/api.go deleted file mode 100644 index 789e167..0000000 --- a/internal/app/genityapp/api.go +++ /dev/null @@ -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 -}