Dockerfile

This commit is contained in:
Brieuc Dubois 2023-12-22 22:23:01 +01:00
parent 12d1cff06a
commit c24c0b4ed3
6 changed files with 32 additions and 11 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.sqlite *.sqlite
bin/

View File

@ -1,10 +1,23 @@
# Dockerfile FROM golang:latest as builder
FROM golang:latest
WORKDIR /app WORKDIR /app
COPY go.mod go.sum .
RUN go mod download
COPY . . 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"]

BIN
bin/trakr

Binary file not shown.

View File

@ -57,7 +57,6 @@ func pixelHandler(w http.ResponseWriter, r *http.Request) {
} }
uuid := pathParts[2] uuid := pathParts[2]
// Get the pixel ID from the database
pixelID, err := getPixelIDFromUUID(uuid) pixelID, err := getPixelIDFromUUID(uuid)
if err != nil { if err != nil {
log.Println("Error getting pixel ID:", err) 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) log.Println("Error updating stats:", err)
} }
// Serve the pixel image
w.Header().Set("Content-Type", "image/gif") w.Header().Set("Content-Type", "image/gif")
w.Write(onePixelGIF) w.Write(onePixelGIF)
} }
@ -112,14 +110,12 @@ func dashboardHandler(w http.ResponseWriter, r *http.Request) {
stats = append(stats, s) stats = append(stats, s)
} }
// Handle any errors encountered during iteration
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
http.Error(w, "Error iterating data", http.StatusInternalServerError) http.Error(w, "Error iterating data", http.StatusInternalServerError)
log.Println("Error iterating data:", err) log.Println("Error iterating data:", err)
return return
} }
// Render the template
tmpl, err := template.ParseFiles("public/html/dashboard.html") tmpl, err := template.ParseFiles("public/html/dashboard.html")
if err != nil { if err != nil {
http.Error(w, "Error loading template", http.StatusInternalServerError) http.Error(w, "Error loading template", http.StatusInternalServerError)

View File

@ -17,16 +17,16 @@ var onePixelGIF = []byte{
} }
func Server() { func Server() {
db = initDB("data.sqlite") db = initDB(getenv("DATA", ".") + "/data.sqlite")
http.HandleFunc("/new", newPixelPageHandler) http.HandleFunc("/new", newPixelPageHandler)
http.HandleFunc("/create-pixel", createPixelHandler) http.HandleFunc("/create-pixel", createPixelHandler)
http.HandleFunc("/p/", pixelHandler) http.HandleFunc("/p/", pixelHandler)
http.HandleFunc("/dashboard", dashboardHandler) 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)) log.Fatal(http.ListenAndServe(addr, nil))
} }

11
server/utils.go Normal file
View File

@ -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
}