diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 00ffd37..05e1138 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,47 +1,31 @@ # syntax=docker/dockerfile:1 -# Comments are provided throughout this file to help you get started. -# If you need more help, visit the Dockerfile reference guide at -# https://docs.docker.com/engine/reference/builder/ +ARG GO_VERSION=1.21.4 -################################################################################ # Create a stage for building the application. -ARG GO_VERSION=1.21.4 FROM golang:${GO_VERSION} AS build WORKDIR /src -# Download dependencies as a separate step to take advantage of Docker's caching. -# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. -# Leverage bind mounts to go.sum and go.mod to avoid having to copy them into -# the container. RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=bind,source=go.sum,target=go.sum \ --mount=type=bind,source=go.mod,target=go.mod \ go mod download -x -# Build the application. -# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds. -# Leverage a bind mount to the current directory to avoid having to copy the -# source code into the container. RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=bind,target=. \ CGO_ENABLED=0 go build -o /bin/server . +# Copy the 'public' folder from the local directory into the container. +FROM build AS copy-public +WORKDIR /app + +COPY public /app/public + ################################################################################ # Create a new stage for running the application that contains the minimal -# runtime dependencies for the application. This often uses a different base -# image from the build stage where the necessary files are copied from the build -# stage. -# -# The example below uses the alpine image as the foundation for running the app. -# By specifying the "latest" tag, it will also use whatever happens to be the -# most recent version of that image when you build your Dockerfile. If -# reproducability is important, consider using a versioned tag -# (e.g., alpine:3.17.2) or SHA (e.g., alpine@sha256:c41ab5c992deb4fe7e5da09f67a8804a46bd0592bfdf0b1847dde0e0889d2bff). +# runtime dependencies for the application. FROM alpine:latest AS final -# Install any runtime dependencies that are needed to run your application. -# Leverage a cache mount to /var/cache/apk/ to speed up subsequent builds. RUN --mount=type=cache,target=/var/cache/apk \ apk --update add \ ca-certificates \ @@ -49,8 +33,6 @@ RUN --mount=type=cache,target=/var/cache/apk \ && \ update-ca-certificates -# Create a non-privileged user that the app will run under. -# See https://docs.docker.com/go/dockerfile-user-best-practices/ ARG UID=10001 RUN adduser \ --disabled-password \ @@ -62,11 +44,17 @@ RUN adduser \ appuser USER appuser -# Copy the executable from the "build" stage. -COPY --from=build /bin/server /bin/ -COPY --from=build /src/public /bin/public +# Create a directory for the application and set it as the working directory. +WORKDIR /app + +# Copy the executable from the "build" stage into the current directory. +COPY --from=build /bin/server . + +# Copy the 'public' folder from the "copy-public" stage into the current directory. +COPY --from=copy-public /app/public ./public + # Expose the port that the application listens on. EXPOSE 5454 # What the container should run when it is started. -ENTRYPOINT [ "/bin/server" ] +ENTRYPOINT [ "/app/server" ] diff --git a/main.go b/main.go index edfb9ef..37f5869 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { routes.NotFoundHandler(w, r, fsys) return } - routes.AnimeHandler(w, r,id, fsys) + routes.AnimeHandler(w, r, id, fsys) })) router.Handle("/anime/{id}/episode/{episode}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -69,7 +69,7 @@ func main() { routes.NotFoundHandler(w, r, fsys) return } - routes.EpisodeHandler(w, r,id, episode, fsys) + routes.EpisodeHandler(w, r, id, episode, fsys) })) router.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) { @@ -81,5 +81,8 @@ func main() { Handler: router, } log.Println("Listening on port " + port) - server.ListenAndServe() + err := server.ListenAndServe() + if err != nil { + log.Fatal(err) + } }