Skip to content

Commit d03a27f

Browse files
committed
Merge branch 'master' into dependabot/go_modules/golang.org/x/net-0.17.0
2 parents f5b50c8 + 6b5a9a3 commit d03a27f

File tree

26 files changed

+1876
-153
lines changed

26 files changed

+1876
-153
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker-compose/postgres

.github/workflows/docker-image.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
- "master"
8+
9+
jobs:
10+
docker:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
- name: Set up QEMU
16+
uses: docker/setup-qemu-action@v3
17+
- name: Set up Docker Buildx
18+
uses: docker/setup-buildx-action@v3
19+
- name: Login to ghcr
20+
uses: docker/login-action@v3
21+
with:
22+
registry: ghcr.io
23+
username: ${{github.actor}}
24+
password: ${{secrets.GH_TOKEN}}
25+
- name: Build and push
26+
uses: docker/build-push-action@v5
27+
with:
28+
context: .
29+
platforms: linux/amd64,linux/arm64
30+
push: true
31+
tags: ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@ Temporary Items
5959

6060
# End of https://www.toptal.com/developers/gitignore/api/go,macos
6161

62-
.env
62+
.env
63+
docker-compose/
64+
__debug_bin*

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "./main.go"
13+
}
14+
]
15+
}

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
############################
2+
# STEP 1 build executable binary
3+
############################
4+
FROM golang:alpine AS builder
5+
# Install git.
6+
# Git is required for fetching the dependencies.
7+
RUN apk update && apk add --no-cache 'git=~2'
8+
9+
# Install dependencies
10+
ENV GO111MODULE=on
11+
WORKDIR $GOPATH/src/packages/goginapp/
12+
COPY . .
13+
14+
# Fetch dependencies.
15+
# Using go get.
16+
RUN go get -d -v
17+
18+
# Build the binary.
19+
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/main .
20+
21+
############################
22+
# STEP 2 build a small image
23+
############################
24+
FROM alpine
25+
LABEL org.opencontainers.image.source="https://github.com/harshsinghvi/golang-postgres-kubernetes"
26+
27+
WORKDIR /
28+
29+
# Copy our static executable.
30+
COPY --from=builder /go/main /go/main
31+
# COPY public /go/public
32+
33+
ENV PORT 8080
34+
ENV GIN_MODE debug
35+
EXPOSE 8080
36+
37+
WORKDIR /go
38+
39+
# Run the Go Gin binary.
40+
ENTRYPOINT ["/go/main"]

README.md

Lines changed: 310 additions & 4 deletions
Large diffs are not rendered by default.

controllers/controllers.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"github.com/gin-gonic/gin"
6+
guuid "github.com/google/uuid"
7+
"harshsinghvi/golang-postgres-kubernetes/database"
8+
"harshsinghvi/golang-postgres-kubernetes/models"
9+
"harshsinghvi/golang-postgres-kubernetes/utils"
10+
"log"
11+
"net/http"
12+
"time"
13+
)
14+
15+
func GetAllTodos(c *gin.Context) {
16+
var pag models.Pagination
17+
var err error
18+
19+
var todos []models.Todo
20+
var searchString = c.Query("search")
21+
var pageString = c.Query("page")
22+
pag.ParseString(pageString)
23+
24+
querry := database.Connection.Model(&todos).Order("created_at DESC")
25+
26+
if searchString != "" {
27+
querry = querry.Where(fmt.Sprintf("text like '%%%s%%'", searchString))
28+
}
29+
30+
if pag.TotalRecords, err = querry.Count(); err != nil {
31+
utils.InternalServerError(c, "Error while getting all todos, Reason:", err)
32+
return
33+
}
34+
35+
if pag.CurrentPage != -1 {
36+
querry = querry.Limit(10).Offset(10 * (pag.CurrentPage))
37+
}
38+
39+
if err := querry.Select(); err != nil {
40+
utils.InternalServerError(c, "Error while getting all todos, Reason:", err)
41+
return
42+
}
43+
44+
c.JSON(http.StatusOK, gin.H{
45+
"status": http.StatusOK,
46+
"message": "All Todos",
47+
"data": todos,
48+
"pagination": pag.Validate(),
49+
})
50+
}
51+
52+
func GetSingleTodo(c *gin.Context) {
53+
todoId := c.Param("id")
54+
todo := &models.Todo{ID: todoId}
55+
if err := database.Connection.Select(todo); err != nil {
56+
utils.InternalServerError(c, "Error while getting a single todo, Reason:", err)
57+
return
58+
}
59+
c.JSON(http.StatusOK, gin.H{
60+
"status": http.StatusOK,
61+
"message": "Single Todo",
62+
"data": todo,
63+
})
64+
}
65+
66+
func CreateTodo(c *gin.Context) {
67+
var todo models.Todo
68+
c.BindJSON(&todo)
69+
70+
text := todo.Text
71+
id := guuid.New().String()
72+
73+
insertError := database.Connection.Insert(&models.Todo{
74+
ID: id,
75+
Text: text,
76+
Completed: false,
77+
CreatedAt: time.Now(),
78+
UpdatedAt: time.Now(),
79+
})
80+
81+
if insertError != nil {
82+
utils.InternalServerError(c, "Error while inserting new todo into db, Reason:", insertError)
83+
return
84+
}
85+
86+
c.JSON(http.StatusCreated, gin.H{
87+
"status": http.StatusCreated,
88+
"message": "Todo created Successfully",
89+
})
90+
}
91+
92+
func EditTodo(c *gin.Context) {
93+
todoId := c.Param("id")
94+
var todo models.Todo
95+
c.BindJSON(&todo)
96+
97+
querry := database.Connection.Model(&models.Todo{}).Set("completed = ?", todo.Completed).Set("updated_at = ?", time.Now())
98+
if todo.Text != "" {
99+
querry = querry.Set("text = ?", todo.Text)
100+
}
101+
102+
res, err := querry.Where("id = ?", todoId).Update()
103+
104+
if err != nil {
105+
utils.InternalServerError(c, "Error while editing todo, Reason:", err)
106+
}
107+
108+
if res.RowsAffected() == 0 {
109+
log.Printf("Error while update todo, Reason: \n")
110+
c.JSON(http.StatusNotFound, gin.H{
111+
"status": http.StatusNotFound,
112+
"message": "Todo not found",
113+
})
114+
return
115+
}
116+
117+
c.JSON(http.StatusOK, gin.H{
118+
"status": 200,
119+
"message": "Todo Edited Successfully",
120+
})
121+
}
122+
123+
func DeleteTodo(c *gin.Context) {
124+
todoId := c.Param("id")
125+
todo := &models.Todo{ID: todoId}
126+
if err := database.Connection.Delete(todo); err != nil {
127+
utils.InternalServerError(c, "Error while deleting a single todo, Reason:", err)
128+
return
129+
}
130+
c.JSON(http.StatusOK, gin.H{
131+
"status": http.StatusOK,
132+
"message": "Todo deleted successfully",
133+
})
134+
}

controllers_old/controllers_old.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package controllers_old
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"harshsinghvi/golang-postgres-kubernetes/models"
6+
"net/http"
7+
)
8+
9+
var TODOS = []models.Todo{
10+
{ID: "1", Text: "Task 1", Completed: false},
11+
{ID: "2", Text: "Task 2", Completed: false},
12+
{ID: "3", Text: "Task 3", Completed: false},
13+
}
14+
15+
func GetTodos(c *gin.Context) {
16+
id := c.Query("id")
17+
// completed := c.Query("completed") == "true" // TODO: implement this filter
18+
19+
if id == "" {
20+
c.IndentedJSON(http.StatusOK, TODOS)
21+
return
22+
}
23+
24+
for _, a := range TODOS {
25+
if a.ID == id {
26+
c.IndentedJSON(http.StatusOK, a)
27+
return
28+
}
29+
}
30+
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "not found"})
31+
}
32+
33+
func PostTodos(c *gin.Context) {
34+
var newTodo models.Todo
35+
36+
// Call BindJSON to bind the received JSON to
37+
if err := c.BindJSON(&newTodo); err != nil {
38+
return
39+
}
40+
41+
if newTodo.Text == "" {
42+
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no text"})
43+
return
44+
}
45+
46+
for _, a := range TODOS {
47+
if a.ID == newTodo.ID {
48+
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Duplicate ID"})
49+
return
50+
}
51+
}
52+
// Add the new album to the slice.
53+
TODOS = append(TODOS, newTodo)
54+
c.IndentedJSON(http.StatusCreated, newTodo)
55+
}
56+
57+
func UpdateTodos(c *gin.Context) {
58+
id := c.Param("id")
59+
var updateTodo models.Todo
60+
61+
// Call BindJSON to bind the received JSON to
62+
if err := c.BindJSON(&updateTodo); err != nil {
63+
return
64+
}
65+
66+
if updateTodo.ID == "" {
67+
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"})
68+
return
69+
}
70+
71+
for index, a := range TODOS {
72+
if a.ID == id {
73+
if updateTodo.Text != "" {
74+
TODOS[index].Text = updateTodo.Text
75+
}
76+
TODOS[index].Completed = updateTodo.Completed
77+
c.IndentedJSON(http.StatusOK, TODOS[index])
78+
return
79+
}
80+
}
81+
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "invalid ID"})
82+
}
83+
84+
func DeleteTodos(c *gin.Context) {
85+
id := c.Param("id")
86+
87+
for index, a := range TODOS {
88+
if a.ID == id {
89+
TODOS = append(TODOS[:index], TODOS[index+1:]...)
90+
c.IndentedJSON(http.StatusOK, TODOS)
91+
return
92+
}
93+
}
94+
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "no ID"})
95+
}

0 commit comments

Comments
 (0)