Skip to content
This repository has been archived by the owner on Jul 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from gazes-media/main
Browse files Browse the repository at this point in the history
Build everything good
  • Loading branch information
exatombe authored Nov 30, 2023
2 parents 2aeecdf + 94e87c5 commit 21f20f8
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 83 deletions.
46 changes: 29 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
# 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 \
tzdata \
&& \
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 \
Expand All @@ -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" ]
17 changes: 11 additions & 6 deletions internal/replace.go
Original file line number Diff line number Diff line change
@@ -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("")
}
Expand All @@ -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)
}
}
66 changes: 45 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"embed"
"gazes_ssr/internal"
"gazes_ssr/routes"
"io/fs"
"log"
Expand All @@ -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{
Expand All @@ -86,3 +102,11 @@ func main() {
log.Fatal(err)
}
}

func giveHtmlAndMeta() internal.HtmlAndMeta {
return internal.HtmlAndMeta{
Html: f,
Meta: metaf,
NotFound: notfound,
}
}
3 changes: 2 additions & 1 deletion public/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
<meta name="twitter:url" content="https://gazes.fr" />
<meta name="twitter:creator" content="@Gazes" />
<meta name="twitter:domain" content="gazes.fr" />
<meta name="twitter:widgets:csp" content="on" />
<meta name="twitter:widgets:csp" content="on" />
<title>{{title}}</title>
3 changes: 1 addition & 2 deletions routes/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions routes/favicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions routes/fiche.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions routes/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions routes/latest.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))

}
}
5 changes: 2 additions & 3 deletions routes/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
6 changes: 3 additions & 3 deletions routes/notFound.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 4 additions & 5 deletions routes/search.go
Original file line number Diff line number Diff line change
@@ -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))

}
}
Loading

0 comments on commit 21f20f8

Please sign in to comment.