Skip to content

Commit 47fe19a

Browse files
committed
complete todo-app with K8s deployment and configurations
0 parents  commit 47fe19a

27 files changed

Lines changed: 2377 additions & 0 deletions

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
node_modules
2+
3+
# Output
4+
.output
5+
.vercel
6+
.netlify
7+
.wrangler
8+
/.svelte-kit
9+
/build
10+
11+
# OS
12+
.DS_Store
13+
Thumbs.db
14+
15+
# Env
16+
.env
17+
.env.*
18+
!.env.example
19+
!.env.test
20+
21+
# Vite
22+
vite.config.js.timestamp-*
23+
vite.config.ts.timestamp-*

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

docker-compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Use version 3.8 of the Docker Compose file format
2+
version: '3.8'
3+
4+
# Define the services (containers) for our application
5+
services:
6+
# The Go backend service
7+
backend:
8+
build:
9+
context: ./go-todo-backend # The directory containing the backend's Dockerfile
10+
dockerfile: Dockerfile
11+
image: docker0y4sh/todo-backend:latest
12+
ports:
13+
# Map port 8080 on the host to port 8080 in the container
14+
- "8080:8080"
15+
restart: always
16+
17+
# The Svelte frontend service
18+
frontend:
19+
build:
20+
context: ./go-todo-frontend # The directory containing the frontend's Dockerfile
21+
dockerfile: Dockerfile
22+
image: docker0y4sh/todo-frontend:latest
23+
ports:
24+
# Map port 5173 on the host to port 80 in the container (where Nginx is running)
25+
- "5173:80"
26+
depends_on:
27+
- backend # This tells Docker to start the backend before the frontend
28+
restart: always

go-todo-backend/Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# --- Stage 1: The Builder ---
2+
# Use the official Go image to build our application.
3+
# This image contains all the necessary compilers and tools.
4+
FROM golang:1.24-alpine AS builder
5+
6+
# Set the working directory inside the container
7+
WORKDIR /app
8+
9+
# Copy the dependency files first. This leverages Docker's layer caching.
10+
# If these files don't change, Docker won't re-download dependencies.
11+
COPY go.mod go.sum ./
12+
RUN go mod download
13+
14+
# Copy the rest of the source code
15+
COPY . .
16+
17+
# Build the application.
18+
# CGO_ENABLED=0 creates a statically linked binary (no external dependencies).
19+
# -o /go-todo-app specifies the output path for the compiled binary.
20+
RUN CGO_ENABLED=0 GOOS=linux go build -o /go-todo-app ./main.go
21+
22+
# --- Stage 2: The Final Image ---
23+
# Use a minimal base image like Alpine Linux.
24+
# This image is much smaller than the Go image, leading to a tiny final image.
25+
FROM alpine:latest
26+
27+
# Set the working directory
28+
WORKDIR /root/
29+
30+
# Copy ONLY the compiled binary from the 'builder' stage.
31+
COPY --from=builder /go-todo-app .
32+
33+
# Expose port 8080 to the outside world.
34+
EXPOSE 8080
35+
36+
# Command to run the application when the container starts.
37+
CMD ["./go-todo-app"]

go-todo-backend/go.mod

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module github.com/yashpal2104/go-todo-backend
2+
3+
go 1.24.4
4+
5+
require (
6+
github.com/gin-contrib/cors v1.7.6
7+
github.com/gin-gonic/gin v1.10.1
8+
)
9+
10+
require (
11+
github.com/bytedance/sonic v1.13.3 // indirect
12+
github.com/bytedance/sonic/loader v0.2.4 // indirect
13+
github.com/cloudwego/base64x v0.1.5 // indirect
14+
github.com/gabriel-vasile/mimetype v1.4.9 // indirect
15+
github.com/gin-contrib/sse v1.1.0 // indirect
16+
github.com/go-playground/locales v0.14.1 // indirect
17+
github.com/go-playground/universal-translator v0.18.1 // indirect
18+
github.com/go-playground/validator/v10 v10.26.0 // indirect
19+
github.com/goccy/go-json v0.10.5 // indirect
20+
github.com/json-iterator/go v1.1.12 // indirect
21+
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
22+
github.com/kr/text v0.2.0 // indirect
23+
github.com/leodido/go-urn v1.4.0 // indirect
24+
github.com/mattn/go-isatty v0.0.20 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
26+
github.com/modern-go/reflect2 v1.0.2 // indirect
27+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
28+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
29+
github.com/ugorji/go/codec v1.3.0 // indirect
30+
golang.org/x/arch v0.18.0 // indirect
31+
golang.org/x/crypto v0.39.0 // indirect
32+
golang.org/x/net v0.41.0 // indirect
33+
golang.org/x/sys v0.33.0 // indirect
34+
golang.org/x/text v0.26.0 // indirect
35+
google.golang.org/protobuf v1.36.6 // indirect
36+
gopkg.in/yaml.v3 v3.0.1 // indirect
37+
)

go-todo-backend/go.sum

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
github.com/bytedance/sonic v1.13.3 h1:MS8gmaH16Gtirygw7jV91pDCN33NyMrPbN7qiYhEsF0=
2+
github.com/bytedance/sonic v1.13.3/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
3+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
4+
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
5+
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
6+
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
7+
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
8+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
12+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13+
github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY=
14+
github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
15+
github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQY=
16+
github.com/gin-contrib/cors v1.7.6/go.mod h1:Ulcl+xN4jel9t1Ry8vqph23a60FwH9xVLd+3ykmTjOk=
17+
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
18+
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
19+
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
20+
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
21+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
22+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
23+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
24+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
25+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
26+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
27+
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
28+
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
29+
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
30+
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
31+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
32+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
33+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
34+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
35+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
36+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
37+
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
38+
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
39+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
40+
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
41+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
42+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
43+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
44+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
45+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
46+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
47+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
48+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
49+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
50+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
51+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
52+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
53+
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
54+
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
55+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
56+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57+
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
58+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
59+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
60+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
61+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
62+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
63+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
64+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
65+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
66+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
67+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
68+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
69+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
70+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
71+
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
72+
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
73+
golang.org/x/arch v0.18.0 h1:WN9poc33zL4AzGxqf8VtpKUnGvMi8O9lhNyBMF/85qc=
74+
golang.org/x/arch v0.18.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
75+
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
76+
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
77+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
78+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
79+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
80+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
81+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
82+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
83+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
84+
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
85+
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
86+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
87+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
88+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
89+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
90+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
91+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
92+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=

go-todo-backend/main.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// go-todo-backend/main.go
2+
3+
package main
4+
5+
import (
6+
"net/http"
7+
"strconv"
8+
"sync"
9+
10+
"github.com/gin-contrib/cors"
11+
"github.com/gin-gonic/gin"
12+
)
13+
14+
// Task represents the structure of our task/note object.
15+
type Task struct {
16+
ID int `json:"id"`
17+
Title string `json:"title"`
18+
Completed bool `json:"completed"`
19+
}
20+
21+
// --- In-Memory "Database" ---
22+
// For this simple example, we'll store tasks in memory.
23+
// In a real application, we would use a database like PostgreSQL or SQLite.
24+
var (
25+
tasks = make(map[int]Task)
26+
nextTaskID = 1
27+
lock = sync.Mutex{} // A mutex to handle concurrent requests safely
28+
)
29+
30+
func main() {
31+
// Initialize Gin router
32+
router := gin.Default()
33+
34+
// --- CORS Middleware ---
35+
// This is crucial for allowing the frontend (running on a different port)
36+
// to communicate with this backend.
37+
config := cors.DefaultConfig()
38+
config.AllowOrigins = []string{"http://localhost:5173"} // The default Svelte dev server port
39+
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
40+
router.Use(cors.New(config))
41+
42+
// --- API Routes ---
43+
api := router.Group("/api")
44+
{
45+
api.GET("/tasks", getTasks)
46+
api.POST("/tasks", addTask)
47+
api.PUT("/tasks/:id", updateTask)
48+
api.DELETE("/tasks/:id", deleteTask)
49+
}
50+
51+
// Pre-populate with some data for demonstration
52+
tasks[nextTaskID] = Task{ID: nextTaskID, Title: "Learn Go & Gin", Completed: true}
53+
nextTaskID++
54+
tasks[nextTaskID] = Task{ID: nextTaskID, Title: "Build a Svelte Frontend", Completed: false}
55+
nextTaskID++
56+
57+
// Start the server
58+
router.Run("0.0.0.0:8080") // Listen and serve on http://localhost:8080
59+
}
60+
61+
// --- Route Handlers ---
62+
63+
// getTasks responds with the list of all tasks as JSON.
64+
func getTasks(c *gin.Context) {
65+
lock.Lock()
66+
defer lock.Unlock()
67+
68+
// Convert map to slice for consistent JSON output
69+
taskList := make([]Task, 0, len(tasks))
70+
for _, task := range tasks {
71+
taskList = append(taskList, task)
72+
}
73+
74+
c.JSON(http.StatusOK, taskList)
75+
}
76+
77+
// addTask adds a new task from the JSON received in the request body.
78+
func addTask(c *gin.Context) {
79+
var newTask Task
80+
81+
// Bind the received JSON to the newTask struct
82+
if err := c.ShouldBindJSON(&newTask); err != nil {
83+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
84+
return
85+
}
86+
87+
lock.Lock()
88+
defer lock.Unlock()
89+
90+
newTask.ID = nextTaskID
91+
newTask.Completed = false // New tasks are always incomplete
92+
tasks[newTask.ID] = newTask
93+
nextTaskID++
94+
95+
c.JSON(http.StatusCreated, newTask)
96+
}
97+
98+
// updateTask updates an existing task's status (e.g., toggles 'completed').
99+
func updateTask(c *gin.Context) {
100+
id, err := strconv.Atoi(c.Param("id"))
101+
if err != nil {
102+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
103+
return
104+
}
105+
106+
var updatedTask Task
107+
if err := c.ShouldBindJSON(&updatedTask); err != nil {
108+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
109+
return
110+
}
111+
112+
lock.Lock()
113+
defer lock.Unlock()
114+
115+
if _, ok := tasks[id]; ok {
116+
tasks[id] = updatedTask // Replace the whole task
117+
c.JSON(http.StatusOK, updatedTask)
118+
} else {
119+
c.JSON(http.StatusNotFound, gin.H{"error": "Task not found"})
120+
}
121+
}
122+
123+
// deleteTask removes a task by its ID.
124+
func deleteTask(c *gin.Context) {
125+
id, err := strconv.Atoi(c.Param("id"))
126+
if err != nil {
127+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
128+
return
129+
}
130+
131+
lock.Lock()
132+
defer lock.Unlock()
133+
134+
if _, ok := tasks[id]; ok {
135+
delete(tasks, id)
136+
c.Status(http.StatusNoContent) // Success, no content to return
137+
} else {
138+
c.JSON(http.StatusNotFound, gin.H{"error": "Task not found"})
139+
}
140+
}

go-todo-frontend/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# --- Stage 1: The Builder ---
2+
FROM node:20-alpine as builder
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy package.json and package-lock.json for dependency installation
8+
COPY package*.json ./
9+
RUN npm install
10+
11+
# Copy the rest of the application source code
12+
COPY . .
13+
14+
# Run the build script defined in package.json
15+
RUN npm run build
16+
17+
# --- Stage 2: The Final Image ---
18+
FROM nginx:stable-alpine
19+
COPY --from=builder /app/build /usr/share/nginx/html
20+
COPY nginx.conf /etc/nginx/conf.d/default.conf
21+
EXPOSE 80
22+
CMD ["nginx", "-g", "daemon off;"]

0 commit comments

Comments
 (0)