From c840eb29324a0a427599b317eb6b71e432a3237b Mon Sep 17 00:00:00 2001 From: Harsh Singhvi Date: Fri, 17 Nov 2023 03:01:58 +0530 Subject: [PATCH] Update Routes and directory structure --- .vscode/launch.json | 15 +++ controllers/controllers.go | 127 +++++++++++++++++++++++ controllers_old/controllers_old.go | 95 ++++++++++++++++++ database/database.go | 146 +++++---------------------- go.mod | 8 +- go.sum | 55 ++++++++++ main.go | 156 +++++++---------------------- 7 files changed, 356 insertions(+), 246 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 controllers/controllers.go create mode 100644 controllers_old/controllers_old.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5ac2bb4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "./main.go" + } + ] +} \ No newline at end of file diff --git a/controllers/controllers.go b/controllers/controllers.go new file mode 100644 index 0000000..c874245 --- /dev/null +++ b/controllers/controllers.go @@ -0,0 +1,127 @@ +package controllers + +import ( + "github.com/gin-gonic/gin" + // "github.com/go-pg/pg/v9" + guuid "github.com/google/uuid" + "log" + "net/http" + "time" + + "harshsinghvi/golang-postgres-kubernetes/database" + "harshsinghvi/golang-postgres-kubernetes/models" +) + +// var (connection *pg.DB )// = *database.GetDatabase() + +// init() { +// connection = = *database.GetDatabase() +// } + +// connection := *database.GetDatabase() + +func GetAllTodos(c *gin.Context) { + + var todos []models.Todo + err := database.Connection.Model(&todos).Select() + if err != nil { + log.Printf("Error while getting all todos, Reason: %v\n", err) + c.JSON(http.StatusInternalServerError, gin.H{ + "status": http.StatusInternalServerError, + "message": "Something went wrong", + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "status": http.StatusOK, + "message": "All Todos", + "data": todos, + }) +} + +func GetSingleTodo(c *gin.Context) { + todoId := c.Param("id") + todo := &models.Todo{ID: todoId} + err := database.Connection.Select(todo) + if err != nil { + log.Printf("Error while getting a single todo, Reason: %v\n", err) + c.JSON(http.StatusNotFound, gin.H{ + "status": http.StatusNotFound, + "message": "Todo not found", + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "status": http.StatusOK, + "message": "Single Todo", + "data": todo, + }) +} + +func CreateTodo(c *gin.Context) { + var todo models.Todo + c.BindJSON(&todo) + + text := todo.Text + id := guuid.New().String() + + insertError := database.Connection.Insert(&models.Todo{ + ID: id, + Text: text, + Completed: false, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + }) + + if insertError != nil { + log.Printf("Error while inserting new todo into db, Reason: %v\n", insertError) + c.JSON(http.StatusInternalServerError, gin.H{ + "status": http.StatusInternalServerError, + "message": "Something went wrong", + }) + return + } + + c.JSON(http.StatusCreated, gin.H{ + "status": http.StatusCreated, + "message": "Todo created Successfully", + }) +} + +func EditTodo(c *gin.Context) { + todoId := c.Param("id") + var todo models.Todo + c.BindJSON(&todo) + completed := todo.Completed + _, err := database.Connection.Model(&models.Todo{}).Set("completed = ?", completed).Where("id = ?", todoId).Update() + if err != nil { + log.Printf("Error, Reason: %v\n", err) + c.JSON(http.StatusInternalServerError, gin.H{ + "status": 500, + "message": "Something went wrong", + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "status": 200, + "message": "Todo Edited Successfully", + }) +} + +func DeleteTodo(c *gin.Context) { + todoId := c.Param("id") + todo := &models.Todo{ID: todoId} + err := database.Connection.Delete(todo) + if err != nil { + log.Printf("Error while deleting a single todo, Reason: %v\n", err) + c.JSON(http.StatusInternalServerError, gin.H{ + "status": http.StatusInternalServerError, + "message": "Something went wrong", + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "status": http.StatusOK, + "message": "Todo deleted successfully", + }) +} diff --git a/controllers_old/controllers_old.go b/controllers_old/controllers_old.go new file mode 100644 index 0000000..fd06cf8 --- /dev/null +++ b/controllers_old/controllers_old.go @@ -0,0 +1,95 @@ +package controllers_old + +import ( + "github.com/gin-gonic/gin" + "harshsinghvi/golang-postgres-kubernetes/models" + "net/http" +) + +var TODOS = []models.Todo{ + {ID: "1", Text: "Task 1", Completed: false}, + {ID: "2", Text: "Task 2", Completed: false}, + {ID: "3", Text: "Task 3", Completed: false}, +} + +func GetTodos(c *gin.Context) { + id := c.Query("id") + // completed := c.Query("completed") == "true" // TODO: implement this filter + + if id == "" { + c.IndentedJSON(http.StatusOK, TODOS) + return + } + + for _, a := range TODOS { + if a.ID == id { + c.IndentedJSON(http.StatusOK, a) + return + } + } + c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "not found"}) +} + +func PostTodos(c *gin.Context) { + var newTodo models.Todo + + // Call BindJSON to bind the received JSON to + if err := c.BindJSON(&newTodo); err != nil { + return + } + + if newTodo.Text == "" { + c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no text"}) + return + } + + for _, a := range TODOS { + if a.ID == newTodo.ID { + c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Duplicate ID"}) + return + } + } + // Add the new album to the slice. + TODOS = append(TODOS, newTodo) + c.IndentedJSON(http.StatusCreated, newTodo) +} + +func UpdateTodos(c *gin.Context) { + id := c.Param("id") + var updateTodo models.Todo + + // Call BindJSON to bind the received JSON to + if err := c.BindJSON(&updateTodo); err != nil { + return + } + + if updateTodo.ID == "" { + c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"}) + return + } + + for index, a := range TODOS { + if a.ID == id { + if updateTodo.Text != "" { + TODOS[index].Text = updateTodo.Text + } + TODOS[index].Completed = updateTodo.Completed + c.IndentedJSON(http.StatusOK, TODOS[index]) + return + } + } + c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "invalid ID"}) +} + +func DeleteTodos(c *gin.Context) { + id := c.Param("id") + + for index, a := range TODOS { + if a.ID == id { + TODOS = append(TODOS[:index], TODOS[index+1:]...) + c.IndentedJSON(http.StatusOK, TODOS) + return + } + } + c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"}) +} diff --git a/database/database.go b/database/database.go index 7c9b283..d3fbce4 100644 --- a/database/database.go +++ b/database/database.go @@ -1,29 +1,22 @@ package database import ( - . "harshsinghvi/golang-postgres-kubernetes/models" - "harshsinghvi/golang-postgres-kubernetes/utils" - "github.com/go-pg/pg/v9" orm "github.com/go-pg/pg/v9/orm" - guuid "github.com/google/uuid" + "harshsinghvi/golang-postgres-kubernetes/models" + "harshsinghvi/golang-postgres-kubernetes/utils" "log" - "os" - - "github.com/gin-gonic/gin" - "net/http" - "time" ) var database_ready = false -var connection *pg.DB +var Connection *pg.DB func IsDtabaseReady() bool { return database_ready } -func GetDatabase() *pg.DB { - return connection +func GetDatabase() **pg.DB { + return &Connection } func Connect() *pg.DB { @@ -39,14 +32,25 @@ func Connect() *pg.DB { Addr: DB_HOST + ":" + DB_PORT, Database: DB_NAME, } - connection = pg.Connect(opts) - if connection == nil { - log.Printf("Failed to connect") - os.Exit(100) + + Connection = pg.Connect(opts) + + if Connection == nil { + log.Printf("Failed to connect to database") + return nil + } + + ctx := Connection.Context() + var version string + _, err := Connection.QueryOneContext(ctx, pg.Scan(&version), "SELECT version()") + if err != nil { + log.Printf("Failed to connect to database") + return nil } + log.Printf("Connected to db") database_ready = true - return connection + return Connection } // Create User Table @@ -54,7 +58,8 @@ func CreateTodoTable() error { opts := &orm.CreateTableOptions{ IfNotExists: true, } - createError := connection.CreateTable(&Todo{}, opts) + + createError := Connection.CreateTable(&models.Todo{}, opts) if createError != nil { log.Printf("Error while creating todo table, Reason: %v\n", createError) return createError @@ -62,108 +67,3 @@ func CreateTodoTable() error { log.Printf("Todo table created") return nil } - -func GetAllTodos(c *gin.Context) { - var todos []Todo - err := connection.Model(&todos).Select() - if err != nil { - log.Printf("Error while getting all todos, Reason: %v\n", err) - c.JSON(http.StatusInternalServerError, gin.H{ - "status": http.StatusInternalServerError, - "message": "Something went wrong", - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "status": http.StatusOK, - "message": "All Todos", - "data": todos, - }) -} - -func GetSingleTodo(c *gin.Context) { - todoId := c.Param("id") - todo := &Todo{ID: todoId} - err := connection.Select(todo) - if err != nil { - log.Printf("Error while getting a single todo, Reason: %v\n", err) - c.JSON(http.StatusNotFound, gin.H{ - "status": http.StatusNotFound, - "message": "Todo not found", - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "status": http.StatusOK, - "message": "Single Todo", - "data": todo, - }) -} - -func CreateTodo(c *gin.Context) { - var todo Todo - c.BindJSON(&todo) - - text := todo.Text - id := guuid.New().String() - - insertError := connection.Insert(&Todo{ - ID: id, - Text: text, - Completed: false, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - }) - - if insertError != nil { - log.Printf("Error while inserting new todo into db, Reason: %v\n", insertError) - c.JSON(http.StatusInternalServerError, gin.H{ - "status": http.StatusInternalServerError, - "message": "Something went wrong", - }) - return - } - - c.JSON(http.StatusCreated, gin.H{ - "status": http.StatusCreated, - "message": "Todo created Successfully", - }) -} - -func EditTodo(c *gin.Context) { - todoId := c.Param("id") - var todo Todo - c.BindJSON(&todo) - completed := todo.Completed - _, err := connection.Model(&Todo{}).Set("completed = ?", completed).Where("id = ?", todoId).Update() - if err != nil { - log.Printf("Error, Reason: %v\n", err) - c.JSON(http.StatusInternalServerError, gin.H{ - "status": 500, - "message": "Something went wrong", - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "status": 200, - "message": "Todo Edited Successfully", - }) -} - -func DeleteTodo(c *gin.Context) { - todoId := c.Param("id") - todo := &Todo{ID: todoId} - err := connection.Delete(todo) - if err != nil { - log.Printf("Error while deleting a single todo, Reason: %v\n", err) - c.JSON(http.StatusInternalServerError, gin.H{ - "status": http.StatusInternalServerError, - "message": "Something went wrong", - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "status": http.StatusOK, - "message": "Todo deleted successfully", - }) -} diff --git a/go.mod b/go.mod index fe41666..b1ae86a 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.1 require ( github.com/gin-gonic/gin v1.9.1 + github.com/go-pg/pg/v10 v10.11.2 github.com/go-pg/pg/v9 v9.2.1 github.com/google/uuid v1.4.0 github.com/joho/godotenv v1.5.1 @@ -31,11 +32,14 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/segmentio/encoding v0.1.15 // indirect + github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect github.com/vmihailenco/bufpool v0.1.11 // indirect github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect - github.com/vmihailenco/tagparser v0.1.1 // indirect + github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect + github.com/vmihailenco/tagparser v0.1.2 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect golang.org/x/arch v0.3.0 // indirect golang.org/x/crypto v0.9.0 // indirect golang.org/x/net v0.10.0 // indirect @@ -44,5 +48,5 @@ require ( google.golang.org/appengine v1.6.6 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - mellium.im/sasl v0.2.1 // indirect + mellium.im/sasl v0.3.1 // indirect ) diff --git a/go.sum b/go.sum index 30285ae..59ec849 100644 --- a/go.sum +++ b/go.sum @@ -15,12 +15,15 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-pg/pg/v10 v10.11.2 h1:LPMK5KDe6P+EjuE/iv99gwGx0XGVOZug7Ijx0ekDiE8= +github.com/go-pg/pg/v10 v10.11.2/go.mod h1:ExJWndhDNNftBdw1Ow83xqpSf4WMSJK8urmXD5VXS1I= github.com/go-pg/pg/v9 v9.2.1 h1:4rWNJkj+aPuDFqgieTzNhHBuYaXREh3yaB9NlBerFys= github.com/go-pg/pg/v9 v9.2.1/go.mod h1:fG8qbL+ei4e/fCZLHK+Z+/7b9B+pliZtbpaucG4/YNQ= github.com/go-pg/zerochecker v0.2.0 h1:pp7f72c3DobMWOb2ErtZsnrPaSvHd2W4o9//8HtF4mU= @@ -46,6 +49,7 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -82,9 +86,16 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -103,6 +114,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= +github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= @@ -111,21 +124,32 @@ github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6cz github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ= github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvCazn8G65U= github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4= +github.com/vmihailenco/msgpack/v5 v5.3.4 h1:qMKAwOV+meBw2Y8k9cVwAy7qErtYCwBzZ2ellBfvnqc= +github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY= github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= +github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vbd1qPqc= +github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI= +github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= +github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -133,26 +157,49 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -160,6 +207,9 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -187,11 +237,14 @@ google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -199,4 +252,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= mellium.im/sasl v0.2.1 h1:nspKSRg7/SyO0cRGY71OkfHab8tf9kCts6a6oTDut0w= mellium.im/sasl v0.2.1/go.mod h1:ROaEDLQNuf9vjKqE1SrAfnsobm2YKXT1gnN1uDp1PjQ= +mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo= +mellium.im/sasl v0.3.1/go.mod h1:xm59PUYpZHhgQ9ZqoJ5QaCqzWMi8IeS49dhp6plPCzw= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/main.go b/main.go index f2f7f0a..4299df8 100644 --- a/main.go +++ b/main.go @@ -1,143 +1,57 @@ package main import ( - // "fmt" - "net/http" - "github.com/gin-gonic/gin" - models "harshsinghvi/golang-postgres-kubernetes/models" - "harshsinghvi/golang-postgres-kubernetes/database" "log" + "net/http" + "github.com/gin-gonic/gin" "github.com/joho/godotenv" + "harshsinghvi/golang-postgres-kubernetes/database" + "harshsinghvi/golang-postgres-kubernetes/controllers" + "harshsinghvi/golang-postgres-kubernetes/controllers_old" ) - -var TODOS = []models.Todo{ - {ID: "1", Text: "Task 1", Completed: false}, - {ID: "2", Text: "Task 2", Completed: false}, - {ID: "3", Text: "Task 3", Completed: false}, -} - -func getTodos(c *gin.Context) { - id := c.Query("id") - // completed := c.Query("completed") == "true" // TODO: implement this filter - - if id == "" { - c.IndentedJSON(http.StatusOK, TODOS) - return - } - - for _, a := range TODOS { - if a.ID == id { - c.IndentedJSON(http.StatusOK, a) - return - } - } - - c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "not found"}) - +func healthHandler(c *gin.Context) { + c.IndentedJSON(http.StatusOK, gin.H{"message": "OK"}) } - -func postTodos(c *gin.Context) { - var newTodo models.Todo - - // Call BindJSON to bind the received JSON to - if err := c.BindJSON(&newTodo); err != nil { - return - } - - if newTodo.Text == "" { - c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no text"}) - return - } - - for _, a := range TODOS { - if(a.ID == newTodo.ID){ - c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Duplicate ID"}) - return - } - } - // Add the new album to the slice. - TODOS = append(TODOS, newTodo) - c.IndentedJSON(http.StatusCreated, newTodo) -} - -func updateTodos(c *gin.Context){ - id := c.Param("id") - var updateTodo models.Todo - - // Call BindJSON to bind the received JSON to - if err := c.BindJSON(&updateTodo); err != nil { - return - } - - if updateTodo.ID == "" { - c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"}) - return - } - - for index, a := range TODOS { - if a.ID == id { - if updateTodo.Text != "" { - TODOS[index].Text = updateTodo.Text - } - TODOS[index].Completed = updateTodo.Completed - c.IndentedJSON(http.StatusOK, TODOS[index]) - return - } - } - - c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "invalid ID"}) -} - -func deleteTodos(c *gin.Context){ - id := c.Param("id") - - for index, a := range TODOS { - if a.ID == id{ - TODOS = append(TODOS[:index], TODOS[index+1:]...) - c.IndentedJSON(http.StatusOK, TODOS) - return - } - } - - c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"}) -} - -func healthHandler(c *gin.Context){ - c.IndentedJSON(http.StatusOK, gin.H {"message": "OK"}) -} -func readinessHandler(c *gin.Context){ +func readinessHandler(c *gin.Context) { if !database.IsDtabaseReady() { - c.IndentedJSON(http.StatusServiceUnavailable, gin.H {"message": "server not ready"}) + c.IndentedJSON(http.StatusServiceUnavailable, gin.H{"message": "server not ready"}) return } - c.IndentedJSON(http.StatusOK, gin.H {"message": "OK"}) + c.IndentedJSON(http.StatusOK, gin.H{"message": "OK"}) } func main() { err := godotenv.Load() if err != nil { - log.Printf("Error loading .env file") + log.Printf("Error loading .env file") } - database.Connect(); - database.CreateTodoTable(); - - router := gin.Default() - router.GET("/v1/todo", getTodos) - router.POST("v1/todo", postTodos) - router.PUT("v1/todo/:id", updateTodos) - router.DELETE("v1/todo/:id", deleteTodos) + database.Connect() + database.CreateTodoTable() + + router := gin.Default() + api := router.Group("/api") + { + v1 := api.Group("/v1") + { + v1.GET("/todo", controllers_old.GetTodos) + v1.POST("/todo", controllers_old.PostTodos) + v1.PUT("/todo/:id", controllers_old.UpdateTodos) + v1.DELETE("/todo/:id", controllers_old.DeleteTodos) + } - router.GET("/v2/todo", database.GetAllTodos) - router.GET("/v2/todos", database.GetAllTodos) - router.GET("/v2/todo/:id", database.GetSingleTodo) - router.POST("v2/todo", database.CreateTodo) - router.PUT("v2/todo/:id", database.EditTodo) - router.DELETE("v2/todo/:id", database.DeleteTodo) - - router.GET("/health", healthHandler) - router.GET("/readiness", readinessHandler) + v2 := api.Group("/v2") + { + v2.GET("/todo", controllers.GetAllTodos) + v2.GET("/todo/:id", controllers.GetSingleTodo) + v2.POST("/todo", controllers.CreateTodo) + v2.PUT("/todo/:id", controllers.EditTodo) + v2.DELETE("/todo/:id", controllers.DeleteTodo) + } + } + router.GET("/health", healthHandler) + router.GET("/readiness", readinessHandler) router.Run(":8080") }