From 94e87c5b9746602b43324215bf5dacf91aa664da Mon Sep 17 00:00:00 2001 From: garder500 Date: Fri, 1 Dec 2023 00:03:15 +0100 Subject: [PATCH] Build everything good --- Dockerfile | 46 +++++++++++++++++++------------ internal/replace.go | 17 +++++++----- main.go | 66 ++++++++++++++++++++++++++++++--------------- public/meta.html | 3 ++- routes/episode.go | 3 +-- routes/favicon.go | 2 ++ routes/fiche.go | 3 +-- routes/history.go | 3 +-- routes/latest.go | 7 +++-- routes/master.go | 5 ++-- routes/notFound.go | 6 ++--- routes/search.go | 9 +++---- routes/sitemap.go | 17 ------------ 13 files changed, 104 insertions(+), 83 deletions(-) delete mode 100644 routes/sitemap.go diff --git a/Dockerfile b/Dockerfile index 05e1138..afb1ea6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,31 +1,47 @@ # syntax=docker/dockerfile:1 -ARG GO_VERSION=1.21.4 +# 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/ +################################################################################ # 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. +# 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). 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 \ @@ -33,6 +49,8 @@ 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 \ @@ -44,17 +62,11 @@ RUN adduser \ appuser USER appuser -# 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 +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ # Expose the port that the application listens on. EXPOSE 5454 # What the container should run when it is started. -ENTRYPOINT [ "/app/server" ] +ENTRYPOINT [ "/bin/server" ] diff --git a/internal/replace.go b/internal/replace.go index a6d69d7..4f80939 100644 --- a/internal/replace.go +++ b/internal/replace.go @@ -1,17 +1,22 @@ package internal import ( - "io/fs" + "embed" "strings" ) -func ReplaceHtml(title, description, image, tags string, fsys fs.FS) []byte { - html, err := fs.ReadFile(fsys, "index.html") +type HtmlAndMeta struct { + Html embed.FS + Meta embed.FS + NotFound embed.FS +} + +func ReplaceHtml(title, description, image, tags string, metas HtmlAndMeta) []byte { + html, err := metas.Html.ReadFile("public/index.html") if err != nil { return []byte("No html file found") } - - meta, err := fs.ReadFile(fsys, "meta.html") + meta, err := metas.Meta.ReadFile("public/meta.html") if err != nil { return []byte("") } @@ -25,4 +30,4 @@ func ReplaceHtml(title, description, image, tags string, fsys fs.FS) []byte { htmlStr := string(html) htmlStr = strings.ReplaceAll(htmlStr, "{{meta}}", metaStr) return []byte(htmlStr) -} \ No newline at end of file +} diff --git a/main.go b/main.go index 37f5869..1d5edef 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "embed" + "gazes_ssr/internal" "gazes_ssr/routes" "io/fs" "log" @@ -14,66 +16,80 @@ var fsys fs.FS var static http.Handler var port string = "5454" +//go:embed public/assets +var content embed.FS + +//go:embed public/favicon.ico +var favicon embed.FS + +//go:embed public/sitemap.xml +var sitemap embed.FS + +//go:embed public/index.html +var f embed.FS + +//go:embed public/meta.html +var metaf embed.FS + +//go:embed public/404.html +var notfound embed.FS + func init() { log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) - workingDir, err := os.Getwd() - if err != nil { - log.Fatal(err) - } if os.Getenv("PORT") != "" { port = os.Getenv("PORT") } - fsys = os.DirFS(workingDir + "/public") - static = http.FileServer(http.Dir("./public/assets/")) + staticFs := fs.FS(content) + assets, err := fs.Sub(staticFs, "public/assets") + if err != nil { + log.Fatal(err) + } + static = http.FileServer(http.FS(assets)) } func main() { router := mux.NewRouter() router.PathPrefix("/assets").Handler(http.StripPrefix("/assets/", static)) - router.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { - routes.FaviconHandler(w, r, fsys) - }) - router.HandleFunc("/sitemap", func(w http.ResponseWriter, r *http.Request) { - routes.SitemapHandler(w, r, fsys) - }) + router.Handle("/favicon.ico", http.FileServer(http.FS(favicon))) + router.Handle("/sitemap.xml", http.FileServer(http.FS(sitemap))) router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - routes.NotFoundHandler(w, r, fsys) + routes.NotFoundHandler(w, r, giveHtmlAndMeta()) }) router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - routes.MasterHandler(w, r, fsys) + routes.MasterHandler(w, r, giveHtmlAndMeta()) }) router.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { - routes.SearchHandler(w, r, fsys) + routes.SearchHandler(w, r, giveHtmlAndMeta()) }) router.HandleFunc("/latest", func(w http.ResponseWriter, r *http.Request) { - routes.LatestHandler(w, r, fsys) + routes.LatestHandler(w, r, giveHtmlAndMeta()) }) router.Handle("/anime/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] if id == "" { - routes.NotFoundHandler(w, r, fsys) + routes.NotFoundHandler(w, r, giveHtmlAndMeta()) return } - routes.AnimeHandler(w, r, id, fsys) + routes.AnimeHandler(w, r, id, giveHtmlAndMeta()) })) router.Handle("/anime/{id}/episode/{episode}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] episode := mux.Vars(r)["episode"] if id == "" || episode == "" { - routes.NotFoundHandler(w, r, fsys) + routes.NotFoundHandler(w, r, giveHtmlAndMeta()) return } - routes.EpisodeHandler(w, r, id, episode, fsys) + routes.EpisodeHandler(w, r, id, episode, giveHtmlAndMeta()) })) router.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) { - routes.HistoryHandler(w, r, fsys) + routes.HistoryHandler(w, r, giveHtmlAndMeta()) }) server := &http.Server{ @@ -86,3 +102,11 @@ func main() { log.Fatal(err) } } + +func giveHtmlAndMeta() internal.HtmlAndMeta { + return internal.HtmlAndMeta{ + Html: f, + Meta: metaf, + NotFound: notfound, + } +} diff --git a/public/meta.html b/public/meta.html index 9da8c7f..e93a22a 100644 --- a/public/meta.html +++ b/public/meta.html @@ -19,4 +19,5 @@ - \ No newline at end of file + +{{title}} \ No newline at end of file diff --git a/routes/episode.go b/routes/episode.go index 5262a1e..bd04b88 100644 --- a/routes/episode.go +++ b/routes/episode.go @@ -3,13 +3,12 @@ package routes import ( "errors" "gazes_ssr/internal" - "io/fs" "log" "net/http" "strconv" ) -func EpisodeHandler(w http.ResponseWriter, r *http.Request, id, ep string, fsys fs.FS) { +func EpisodeHandler(w http.ResponseWriter, r *http.Request, id, ep string, fsys internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") // before returning the html, we need to replace the {{meta}} with the actual metadata diff --git a/routes/favicon.go b/routes/favicon.go index 7d1ef74..0ffef5e 100644 --- a/routes/favicon.go +++ b/routes/favicon.go @@ -3,7 +3,9 @@ package routes import ( "io/fs" "net/http" + "embed" ) +var ico embed.FS func FaviconHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { w.Header().Set("Content-Type", "image/x-icon") diff --git a/routes/fiche.go b/routes/fiche.go index 1a7bfa5..1fe20e2 100644 --- a/routes/fiche.go +++ b/routes/fiche.go @@ -3,11 +3,10 @@ package routes import ( "encoding/json" "gazes_ssr/internal" - "io/fs" "net/http" ) -func AnimeHandler(w http.ResponseWriter, r *http.Request, id string, fsys fs.FS) { +func AnimeHandler(w http.ResponseWriter, r *http.Request, id string, fsys internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") response, err := getAnime(id) if err != nil { diff --git a/routes/history.go b/routes/history.go index c50bed5..ce3cb88 100644 --- a/routes/history.go +++ b/routes/history.go @@ -2,11 +2,10 @@ package routes import ( "gazes_ssr/internal" - "io/fs" "net/http" ) -func HistoryHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { +func HistoryHandler(w http.ResponseWriter, r *http.Request, fsys internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") name := "Gazes - Historique" diff --git a/routes/latest.go b/routes/latest.go index 0f3c670..62a82df 100644 --- a/routes/latest.go +++ b/routes/latest.go @@ -1,12 +1,11 @@ package routes import ( - "io/fs" - "net/http" "gazes_ssr/internal" + "net/http" ) -func LatestHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { +func LatestHandler(w http.ResponseWriter, r *http.Request, fsys internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") name := "Gazes - Dernières sorties" @@ -15,4 +14,4 @@ func LatestHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { keywords := "Animes, Streaming, Anime, Manga, Scan, Scans, Scans VF, Scans VOSTFR, Scans FR, Naruto, One Piece, Bleach, Fairy Tail, Dragon Ball Super, Dragon Ball Z, Dragon Ball GT, Dragon Ball, Dragon Ball Kai, Dragon Ball Z Kai, Dragon Ball Z Kai The Final Chapters, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR, Dragon Ball Z Kai VF, Dragon Ball Z Kai VOSTFR, Dragon Ball Z Kai FR, Dragon Ball Super VF, Dragon Ball Super VOSTFR, Dragon Ball Super FR, Dragon Ball GT VF, Dragon Ball GT VOSTFR, Dragon Ball GT FR, Dragon Ball VF, Dragon Ball VOSTFR, Dragon Ball FR, Dragon Ball Z VF, Dragon Ball Z VOSTFR, Dragon Ball Z FR, Dragon Ball Z Kai VF, Dragon Ball Z Kai VOSTFR, Dragon Ball Z Kai FR, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR" w.Write(internal.ReplaceHtml(name, description, image, keywords, fsys)) -} \ No newline at end of file +} diff --git a/routes/master.go b/routes/master.go index fa197d2..449bbb1 100644 --- a/routes/master.go +++ b/routes/master.go @@ -2,15 +2,14 @@ package routes import ( "gazes_ssr/internal" - "io/fs" "net/http" ) -func MasterHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { +func MasterHandler(w http.ResponseWriter, r *http.Request, metas internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") name := "Gazes - Home" image := "https://gazes.fr/favicon.ico" description := "Gazes le seul site web qui vous permet de voir vos animé préféré en streaming gratuitement et sans publicité !" keywords := "Animes, Streaming, Anime, Manga, Scan, Scans, Scans VF, Scans VOSTFR, Scans FR, Naruto, One Piece, Bleach, Fairy Tail, Dragon Ball Super, Dragon Ball Z, Dragon Ball GT, Dragon Ball, Dragon Ball Kai, Dragon Ball Z Kai, Dragon Ball Z Kai The Final Chapters, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR, Dragon Ball Z Kai VF, Dragon Ball Z Kai VOSTFR, Dragon Ball Z Kai FR, Dragon Ball Super VF, Dragon Ball Super VOSTFR, Dragon Ball Super FR, Dragon Ball GT VF, Dragon Ball GT VOSTFR, Dragon Ball GT FR, Dragon Ball VF, Dragon Ball VOSTFR, Dragon Ball FR, Dragon Ball Z VF, Dragon Ball Z VOSTFR, Dragon Ball Z FR, Dragon Ball Z Kai VF, Dragon Ball Z Kai VOSTFR, Dragon Ball Z Kai FR, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR" - w.Write(internal.ReplaceHtml(name, description, image, keywords, fsys)) + w.Write(internal.ReplaceHtml(name, description, image, keywords, metas)) } diff --git a/routes/notFound.go b/routes/notFound.go index 389f961..8fdf69c 100644 --- a/routes/notFound.go +++ b/routes/notFound.go @@ -1,15 +1,15 @@ package routes import ( - "io/fs" + "gazes_ssr/internal" "net/http" ) -func NotFoundHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { +func NotFoundHandler(w http.ResponseWriter, r *http.Request, metas internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") // before returning the html, we need to replace the {{meta}} with the actual metadata - html, err := fs.ReadFile(fsys, "404.html") + html, err := metas.NotFound.ReadFile("public/404.html") if err != nil { http.Error(w, "Unable to retrieve file", http.StatusInternalServerError) return diff --git a/routes/search.go b/routes/search.go index e23d852..5d22e3e 100644 --- a/routes/search.go +++ b/routes/search.go @@ -1,18 +1,17 @@ package routes import ( - "io/fs" - "net/http" "gazes_ssr/internal" + "net/http" ) -func SearchHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { +func SearchHandler(w http.ResponseWriter, r *http.Request, metas internal.HtmlAndMeta) { w.Header().Set("Content-Type", "text/html") name := "Gazes - Search" image := "https://gazes.fr/favicon.ico" description := "Gazes le seul site web qui vous permet de voir vos animé préféré en streaming gratuitement et sans publicité !" keywords := "Animes, Streaming, Anime, Manga, Scan, Scans, Scans VF, Scans VOSTFR, Scans FR, Naruto, One Piece, Bleach, Fairy Tail, Dragon Ball Super, Dragon Ball Z, Dragon Ball GT, Dragon Ball, Dragon Ball Kai, Dragon Ball Z Kai, Dragon Ball Z Kai The Final Chapters, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR, Dragon Ball Z Kai VF, Dragon Ball Z Kai VOSTFR, Dragon Ball Z Kai FR, Dragon Ball Super VF, Dragon Ball Super VOSTFR, Dragon Ball Super FR, Dragon Ball GT VF, Dragon Ball GT VOSTFR, Dragon Ball GT FR, Dragon Ball VF, Dragon Ball VOSTFR, Dragon Ball FR, Dragon Ball Z VF, Dragon Ball Z VOSTFR, Dragon Ball Z FR, Dragon Ball Z Kai VF, Dragon Ball Z Kai VOSTFR, Dragon Ball Z Kai FR, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR, Dragon Ball Z Kai The Final Chapters VF, Dragon Ball Z Kai The Final Chapters VOSTFR, Dragon Ball Z Kai The Final Chapters FR" - w.Write(internal.ReplaceHtml(name, description, image, keywords, fsys)) + w.Write(internal.ReplaceHtml(name, description, image, keywords, metas)) -} \ No newline at end of file +} diff --git a/routes/sitemap.go b/routes/sitemap.go deleted file mode 100644 index 20de2ab..0000000 --- a/routes/sitemap.go +++ /dev/null @@ -1,17 +0,0 @@ -package routes - -import ( - "io/fs" - "net/http" -) - -func SitemapHandler(w http.ResponseWriter, r *http.Request, fsys fs.FS) { - w.Header().Set("Content-Type", "application/xml") - - sitemap, err := fs.ReadFile(fsys, "sitemap.xml") - if err != nil { - http.Error(w, "Unable to retrieve file", http.StatusInternalServerError) - return - } - w.Write([]byte(sitemap)) -} \ No newline at end of file