diff --git a/.gitignore b/.gitignore index 9b1dffd..df2f92d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.sqlite +bin/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 019af81..a9df8b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,23 @@ -# Dockerfile -FROM golang:latest +FROM golang:latest as builder WORKDIR /app +COPY go.mod go.sum . + +RUN go mod download + COPY . . -RUN go build -o main . +RUN make build -CMD ["/app/main"] +FROM alpine:latest + +ENV DATA=/data + +EXPOSE 80 + +WORKDIR /app + +COPY --from=builder /app/bin/trakr . + +ENTRYPOINT ["/app/trakr"] diff --git a/bin/trakr b/bin/trakr deleted file mode 100755 index 1e329d0..0000000 Binary files a/bin/trakr and /dev/null differ diff --git a/server/handlers.go b/server/handlers.go index 33880ac..f6ca88d 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -57,7 +57,6 @@ func pixelHandler(w http.ResponseWriter, r *http.Request) { } uuid := pathParts[2] - // Get the pixel ID from the database pixelID, err := getPixelIDFromUUID(uuid) if err != nil { log.Println("Error getting pixel ID:", err) @@ -81,7 +80,6 @@ func pixelHandler(w http.ResponseWriter, r *http.Request) { log.Println("Error updating stats:", err) } - // Serve the pixel image w.Header().Set("Content-Type", "image/gif") w.Write(onePixelGIF) } @@ -112,14 +110,12 @@ func dashboardHandler(w http.ResponseWriter, r *http.Request) { stats = append(stats, s) } - // Handle any errors encountered during iteration if err = rows.Err(); err != nil { http.Error(w, "Error iterating data", http.StatusInternalServerError) log.Println("Error iterating data:", err) return } - // Render the template tmpl, err := template.ParseFiles("public/html/dashboard.html") if err != nil { http.Error(w, "Error loading template", http.StatusInternalServerError) diff --git a/server/server.go b/server/server.go index 8acbd17..37cab4a 100644 --- a/server/server.go +++ b/server/server.go @@ -17,16 +17,16 @@ var onePixelGIF = []byte{ } func Server() { - db = initDB("data.sqlite") + db = initDB(getenv("DATA", ".") + "/data.sqlite") http.HandleFunc("/new", newPixelPageHandler) http.HandleFunc("/create-pixel", createPixelHandler) http.HandleFunc("/p/", pixelHandler) http.HandleFunc("/dashboard", dashboardHandler) - addr := ":8080" + addr := ":80" - fmt.Printf("Server is up and running on http://localhost%s\n", addr) + fmt.Printf("Server is up and running on port %s\n", addr) log.Fatal(http.ListenAndServe(addr, nil)) } diff --git a/server/utils.go b/server/utils.go new file mode 100644 index 0000000..4921e85 --- /dev/null +++ b/server/utils.go @@ -0,0 +1,11 @@ +package server + +import "os" + +func getenv(key, fallback string) string { + value := os.Getenv(key) + if len(value) == 0 { + return fallback + } + return value +}