From 82e8aecf67f338c6ec43b3af085c2eecfdb7deed Mon Sep 17 00:00:00 2001 From: Amit Suthar Date: Thu, 3 Oct 2024 15:34:40 +0530 Subject: [PATCH] docker --- .dockerignore | 4 ++++ Dockerfile | 27 +++++++++++++++++++++++++++ README.md | 20 ++++++++++++++++++++ internal/server/server.go | 5 +---- main.go | 16 ++++++++++++++-- 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..72d8a19 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.env +tmp +tailwindcss \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..56e8e3e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM golang:1.23 + +WORKDIR /app + +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . + +RUN curl -L https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64 -o /usr/local/bin/tailwindcss + +RUN chmod +x /usr/local/bin/tailwindcss + +RUN go install github.com/a-h/templ/cmd/templ@latest + +RUN which templ && templ --version + +RUN templ generate + +RUN tailwindcss -i ./static/css/input.css -o ./static/css/style.css --minify + +RUN go build -o main ./main.go + +EXPOSE 8080 + +CMD ["./main"] \ No newline at end of file diff --git a/README.md b/README.md index 1593874..3a61eef 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,26 @@ air ## Installation Guide +1. Using Docker + +``` +docker pull amitsuthar69/gotth:1.0 +``` + +``` +docker run -p 8080:8080 amitsuthar69/gotth:1.0 +``` + +OR + +2. Build from source + +### Clone repository + +``` +git clone https://github.com/amitsuthar69/GoTTH.git +``` + ### Air for Live Reload ```bash diff --git a/internal/server/server.go b/internal/server/server.go index 81f2666..11a9d64 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -3,8 +3,6 @@ package server import ( "fmt" "net/http" - "os" - "strconv" "time" _ "github.com/joho/godotenv/autoload" @@ -18,8 +16,7 @@ type Server struct { db database.Service } -func NewServer() *http.Server { - port, _ := strconv.Atoi(os.Getenv("PORT")) +func NewServer(port int) *http.Server { NewServer := &Server{ port: port, diff --git a/main.go b/main.go index b9edd82..7544c8c 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,25 @@ package main import ( "fmt" "gotth/internal/server" + "log" + "os" + "strconv" ) func main() { + portStr := os.Getenv("PORT") + if portStr == "" { + portStr = "8080" // Default port + } + + port, err := strconv.Atoi(portStr) + if err != nil { + log.Fatalf("Failed to parse PORT: %v", err) + } - server := server.NewServer() + server := server.NewServer(port) - err := server.ListenAndServe() + err = server.ListenAndServe() if err != nil { panic(fmt.Sprintf("cannot start server: %s", err)) }