include views and assets in binary

This commit is contained in:
Arya Kiran 2023-09-10 18:15:46 +05:30
parent e584565db7
commit 9e7d11104a
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
7 changed files with 32 additions and 15 deletions

View file

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

View file

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

10
public/embed.go Normal file
View file

@ -0,0 +1,10 @@
package public
import "embed"
//go:embed *
var files embed.FS
func GetFiles() embed.FS {
return files
}

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -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")

10
views/embed.go Normal file
View file

@ -0,0 +1,10 @@
package views
import "embed"
//go:embed *
var files embed.FS
func GetFiles() embed.FS {
return files
}

View file

@ -19,7 +19,7 @@
<body>
<header>
<a href="/"><img src="/mozhi.png" alt="Mozhi Logo" class="navlogo" /></a>
<a href="/"><img src="/assets/mozhi.png" alt="Mozhi Logo" class="navlogo" /></a>
<nav>
<a href="/about">About</a>
<a href="/api/swagger">API</a>