Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/golang.org/x/net-0.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsinghvi committed Nov 18, 2023
2 parents f5b50c8 + 6b5a9a3 commit d03a27f
Show file tree
Hide file tree
Showing 26 changed files with 1,876 additions and 153 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose/postgres
31 changes: 31 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ci

on:
push:
branches:
- "main"
- "master"

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to ghcr
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GH_TOKEN}}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ Temporary Items

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

.env
.env
docker-compose/
__debug_bin*
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
############################
# STEP 1 build executable binary
############################
FROM golang:alpine AS builder
# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache 'git=~2'

# Install dependencies
ENV GO111MODULE=on
WORKDIR $GOPATH/src/packages/goginapp/
COPY . .

# Fetch dependencies.
# Using go get.
RUN go get -d -v

# Build the binary.
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/main .

############################
# STEP 2 build a small image
############################
FROM alpine
LABEL org.opencontainers.image.source="https://github.com/harshsinghvi/golang-postgres-kubernetes"

WORKDIR /

# Copy our static executable.
COPY --from=builder /go/main /go/main
# COPY public /go/public

ENV PORT 8080
ENV GIN_MODE debug
EXPOSE 8080

WORKDIR /go

# Run the Go Gin binary.
ENTRYPOINT ["/go/main"]
314 changes: 310 additions & 4 deletions README.md

Large diffs are not rendered by default.

134 changes: 134 additions & 0 deletions controllers/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package controllers

import (
"fmt"
"github.com/gin-gonic/gin"
guuid "github.com/google/uuid"
"harshsinghvi/golang-postgres-kubernetes/database"
"harshsinghvi/golang-postgres-kubernetes/models"
"harshsinghvi/golang-postgres-kubernetes/utils"
"log"
"net/http"
"time"
)

func GetAllTodos(c *gin.Context) {
var pag models.Pagination
var err error

var todos []models.Todo
var searchString = c.Query("search")
var pageString = c.Query("page")
pag.ParseString(pageString)

querry := database.Connection.Model(&todos).Order("created_at DESC")

if searchString != "" {
querry = querry.Where(fmt.Sprintf("text like '%%%s%%'", searchString))
}

if pag.TotalRecords, err = querry.Count(); err != nil {
utils.InternalServerError(c, "Error while getting all todos, Reason:", err)
return
}

if pag.CurrentPage != -1 {
querry = querry.Limit(10).Offset(10 * (pag.CurrentPage))
}

if err := querry.Select(); err != nil {
utils.InternalServerError(c, "Error while getting all todos, Reason:", err)
return
}

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "All Todos",
"data": todos,
"pagination": pag.Validate(),
})
}

func GetSingleTodo(c *gin.Context) {
todoId := c.Param("id")
todo := &models.Todo{ID: todoId}
if err := database.Connection.Select(todo); err != nil {
utils.InternalServerError(c, "Error while getting a single todo, Reason:", err)
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 {
utils.InternalServerError(c, "Error while inserting new todo into db, Reason:", insertError)
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)

querry := database.Connection.Model(&models.Todo{}).Set("completed = ?", todo.Completed).Set("updated_at = ?", time.Now())
if todo.Text != "" {
querry = querry.Set("text = ?", todo.Text)
}

res, err := querry.Where("id = ?", todoId).Update()

if err != nil {
utils.InternalServerError(c, "Error while editing todo, Reason:", err)
}

if res.RowsAffected() == 0 {
log.Printf("Error while update todo, Reason: \n")
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "Todo not found",
})
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}
if err := database.Connection.Delete(todo); err != nil {
utils.InternalServerError(c, "Error while deleting a single todo, Reason:", err)
return
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "Todo deleted successfully",
})
}
95 changes: 95 additions & 0 deletions controllers_old/controllers_old.go
Original file line number Diff line number Diff line change
@@ -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"})
}
Loading

0 comments on commit d03a27f

Please sign in to comment.