From 080d49ce598879c46395e1749b07416ab4221211 Mon Sep 17 00:00:00 2001 From: DilankaHer Date: Tue, 4 Nov 2025 00:43:23 +0700 Subject: [PATCH] handle browser back button with nav link --- .gitignore | 8 +- Dockerfile | 39 +++++ go.mod | 4 +- internal/api/project_handler.go | 6 +- internal/store/database.go | 4 +- my_website.db | Bin 0 -> 8192 bytes views/index.html | 257 ++++++++++++++++++-------------- views/test.html | 2 +- 8 files changed, 203 insertions(+), 117 deletions(-) create mode 100644 Dockerfile create mode 100644 my_website.db diff --git a/.gitignore b/.gitignore index ebe43b9..5c3b10b 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,10 @@ go.work.sum # Editor/IDE # .idea/ -# .vscode/ +.vscode/ + +# Hot reload +*.toml +/tmp + + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..54c64ee --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# ------------------------------------------------------ +# 1. Build Stage +# ------------------------------------------------------ +FROM golang:1.25-alpine AS builder + +# Install necessary build tools for SQLite (CGO) +RUN apk add --no-cache gcc musl-dev sqlite-dev + +WORKDIR /app + +# Cache dependencies +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source files +COPY . . + +# Build with CGO enabled for SQLite +RUN CGO_ENABLED=1 GOOS=linux go build -o server main.go + +# ------------------------------------------------------ +# 2. Runtime Stage +# ------------------------------------------------------ +FROM alpine:3.20 + +# Install SQLite (if your app interacts directly with .db file) +RUN apk add --no-cache sqlite + +WORKDIR /app + +# Copy binary and required files +COPY --from=builder /app/server . +COPY --from=builder /app/css ./css +COPY --from=builder /app/views ./views +COPY --from=builder /app/my_website.db ./ + +EXPOSE 8080 + +CMD ["./server"] diff --git a/go.mod b/go.mod index 6598b8f..7e3f7c7 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module duhweb -go 1.24.5 +go 1.25.3 require github.com/go-chi/chi/v5 v5.2.3 -require github.com/mattn/go-sqlite3 v1.14.32 // indirect +require github.com/mattn/go-sqlite3 v1.14.32 diff --git a/internal/api/project_handler.go b/internal/api/project_handler.go index 767cd2a..0dff92e 100644 --- a/internal/api/project_handler.go +++ b/internal/api/project_handler.go @@ -8,7 +8,7 @@ import ( ) type ProjectHandler struct { - Templates *template.Template + Templates *template.Template ProjectStore *store.SQLiteProjectStore } @@ -21,7 +21,7 @@ func (h *ProjectHandler) Render(w http.ResponseWriter, tmpl string, data interfa func NewProjectHandler(db *sql.DB) *ProjectHandler { return &ProjectHandler{ - Templates: template.Must(template.ParseGlob("views/*.html")), + Templates: template.Must(template.ParseGlob("views/*.html")), ProjectStore: store.NewSQLiteProjectStore(db), } } @@ -41,4 +41,4 @@ func (h *ProjectHandler) ProjectsPage(w http.ResponseWriter, r *http.Request) { return } h.Render(w, "projects", projects) -} \ No newline at end of file +} diff --git a/internal/store/database.go b/internal/store/database.go index f463a40..ee83af9 100644 --- a/internal/store/database.go +++ b/internal/store/database.go @@ -8,7 +8,7 @@ import ( ) func Open() (*sql.DB, error) { - db, err := sql.Open("sqlite3", "C:/Users/dilan/Documents/Go/my_website_db.db") + db, err := sql.Open("sqlite3", "./my_website.db") if err != nil { return nil, fmt.Errorf("db open: %w", err) } @@ -19,4 +19,4 @@ func Open() (*sql.DB, error) { fmt.Println("Connected to Database...") return db, nil -} \ No newline at end of file +} diff --git a/my_website.db b/my_website.db new file mode 100644 index 0000000000000000000000000000000000000000..57a3f81c1bd5e140dedfce79d6e72f25253a6380 GIT binary patch literal 8192 zcmeI$&r8EF6bJC6_~Rt==4tqj4wl03ELbIs;U*e;a0ii^Wg2ap(gxXeNB^Y#cebtt zZ=Pk}N0OI3a>?iR@^C-3-q6yOS>oxO9W&0^B@tsRXlI>%g4gR_yKho+{wo3&etsR* zw|H;G>Vbd&1Rwwb2tWV=5P$##AOHaf?3BQIpC6v|`+WW6lb6&KrF%EpS6vRSm5gFZ zadb76)K%%sE@*Na%du2+rzW#V&FMzYhvcnK4aM>?9@4^8y0nG2E^jNT%|F_$H~Q_l z@`>IS2HW0z!+M*Y;>Tkl009U<00Izz00bZa0SG_<0{=rm@&k5yAqMT=fEfL5``xnp cGZ3Su`47!1$%@p3+GS%Wlu5J~Gnc=*Zz{)4wEzGB literal 0 HcmV?d00001 diff --git a/views/index.html b/views/index.html index 5baff0d..57182e0 100644 --- a/views/index.html +++ b/views/index.html @@ -1,120 +1,161 @@ {{ block "index" . }} - - - - My CV Website - - - - - + + + + My CV Website + + + + - -
- -
+ + +
+ +
- - - -
- {{ template "about" . }} -
- - - - - - - - + +
{{ template "about" . }}
+ + + + + + -{{ end }} - -{{ block "about" . }} +{{ end }} {{ block "about" . }} -
-

Hi, I'm Your Name

-

- A [Your Role] who loves building [something about what you do]. -

- -
+
+

+ Hi, I'm Your Name +

+

+ A [Your Role] who loves building + [something about what you do]. +

+ +
- -
-

About Me

-

- I’m a [Your Profession] with experience in [key skills]. I enjoy solving problems, learning new technologies, and building applications that make an impact. - Currently exploring Go, htmx, and modern web tools. -

-
- {{ end }} - -{{ block "projects" . }} -

Projects

- {{ range . }} - {{ template "project" . }} - {{ end}} + +
+

About Me

+

+ I’m a [Your Profession] with experience in [key skills]. I enjoy solving + problems, learning new technologies, and building applications that make an + impact. Currently exploring + Go, + htmx, and modern web tools. +

+
+{{ end }} {{ block "projects" . }} +

Projects

+{{ range . }} {{ template "project" . }} {{ end}} {{ end }} {{ block "project" . +}} +
+ {{ .Title }} - + {{ .Description }} +
{{ end }} - -{{ block "project" . }} -
- {{ .Title }} - {{ .Description }} -
-{{ end }} - - - \ No newline at end of file diff --git a/views/test.html b/views/test.html index 924e5bf..5515c99 100644 --- a/views/test.html +++ b/views/test.html @@ -1,4 +1,4 @@ -{{ block "index" . }} +{{ block "test" . }}