From 9e7d11104ac463e22a9592c0b8bb634171bf09ab Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Sun, 10 Sep 2023 18:15:46 +0530 Subject: [PATCH] include views and assets in binary --- Dockerfile | 2 -- README.md | 1 + public/embed.go | 10 ++++++++++ public/{assets => }/favicon.ico | Bin serve/serve.go | 22 ++++++++++------------ views/embed.go | 10 ++++++++++ views/header.html | 2 +- 7 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 public/embed.go rename public/{assets => }/favicon.ico (100%) create mode 100644 views/embed.go diff --git a/Dockerfile b/Dockerfile index 33532cd..c6f1a65 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,8 +14,6 @@ FROM alpine:3.16 as bin WORKDIR /app COPY --from=build /src/mozhi . -COPY --from=build /src/views ./views -COPY --from=build /src/public ./public EXPOSE 3000 diff --git a/README.md b/README.md index 2319ff3..7379870 100644 --- a/README.md +++ b/README.md @@ -82,3 +82,4 @@ Mozhi is the word in Tamil for language. Simple as that :P - [Midou36o](https://midou.dev): made the logo - [Missuo](https://github.com/missuo): making gDeepLX that does the hard part of making DeepL work - [SimplyTranslate](https://codeberg.org/simpleweb/simplytranslate): Inspiration and base code for the webui +- [Rimgo](https://codeberg.org/rimgo/rimgo): Code for embedding html in binary diff --git a/public/embed.go b/public/embed.go new file mode 100644 index 0000000..f8d4899 --- /dev/null +++ b/public/embed.go @@ -0,0 +1,10 @@ +package public + +import "embed" + +//go:embed * +var files embed.FS + +func GetFiles() embed.FS { + return files +} diff --git a/public/assets/favicon.ico b/public/favicon.ico similarity index 100% rename from public/assets/favicon.ico rename to public/favicon.ico diff --git a/serve/serve.go b/serve/serve.go index fcc5ef0..8173112 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -3,13 +3,17 @@ package serve import ( "html/template" "log" + "net/http" "os" "runtime" _ "codeberg.org/aryak/mozhi/docs" "codeberg.org/aryak/mozhi/pages" + "codeberg.org/aryak/mozhi/views" + "codeberg.org/aryak/mozhi/public" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" + "github.com/gofiber/fiber/v2/middleware/filesystem" //"github.com/gofiber/fiber/v2/middleware/limiter" // For debugging purposes // "github.com/gofiber/fiber/v2/middleware/logger" @@ -27,7 +31,8 @@ import ( // @license.url https://www.gnu.org/licenses/agpl-3.0.txt // @BasePath /api func Serve(port string) { - engine := html.New("./views", ".html") + views := http.FS(views.GetFiles()) + engine := html.NewFileSystem(views, ".html") engine.AddFunc( // Add unescape function. This is needed to render HTML from Markdown. @@ -67,12 +72,6 @@ func Serve(port string) { // }, //}) - staticConfig := fiber.Static{ - Compress: true, - // Cache-Control: max-age=31536000 - MaxAge: 31536000, - } - // add global headers app.Use(func(c *fiber.Ctx) error { c.Set("X-Frame-Options", "SAMEORIGIN") @@ -92,11 +91,10 @@ func Serve(port string) { text := c.Query("text") return c.Redirect("/?engine="+engine+"&from="+to+"&to="+from+"&text="+text+"&redirected=true", 301) }) - app.Static("/css", "./public/css", staticConfig) - app.Static("/robots.txt", "./public/robots.txt", staticConfig) - app.Static("/favicon.ico", "./public/assets/favicon.ico", staticConfig) - app.Static("/mozhi.svg", "./public/assets/mozhi.svg", staticConfig) - app.Static("/mozhi.png", "./public/assets/mozhi.png", staticConfig) + app.Use("/", filesystem.New(filesystem.Config{ + MaxAge: 2592000, + Root: http.FS(public.GetFiles()), + })) // app.Get("/about", pages.HandleAbout) api := app.Group("/api") diff --git a/views/embed.go b/views/embed.go new file mode 100644 index 0000000..87b4515 --- /dev/null +++ b/views/embed.go @@ -0,0 +1,10 @@ +package views + +import "embed" + +//go:embed * +var files embed.FS + +func GetFiles() embed.FS { + return files +} diff --git a/views/header.html b/views/header.html index 2977ce3..717123f 100644 --- a/views/header.html +++ b/views/header.html @@ -19,7 +19,7 @@
- +