Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat: use go embed

boojack46d7ecca88db
diff --git a/.gitignore b/.gitignore
index bdf7a6038..0f5553016 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,7 +17,7 @@ tmp
.air
# Frontend asset
-dist
+web/dist
# Dev database
data
diff --git a/Dockerfile b/Dockerfile
index cd6de1751..460e44c84 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -15,6 +15,7 @@ RUN apk update
RUN apk --no-cache add gcc musl-dev
COPY . .
+COPY --from=frontend /frontend-build/dist ./server/dist
RUN go build \
-o memos \
@@ -25,7 +26,6 @@ FROM alpine:3.16.0 AS monolithic
WORKDIR /usr/local/memos
COPY --from=backend /backend-build/memos /usr/local/memos/
-COPY --from=frontend /frontend-build/dist /usr/local/memos/web/dist
# Directory to store the data, which can be referenced as the mounting point.
RUN mkdir -p /var/opt/memos
diff --git a/server/dist/index.html b/server/dist/index.html
new file mode 100644
index 000000000..041977322
--- /dev/null
+++ b/server/dist/index.html
@@ -0,0 +1,13 @@
+<!-- THIS FILE IS A PLACEHOLDER AND SHOULD NOT BE CHANGED -->
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <title>Memos</title>
+ </head>
+ <body>
+ <p>No frontend embeded.</p>
+ </body>
+</html>
diff --git a/server/embed_frontend.go b/server/embed_frontend.go
new file mode 100644
index 000000000..3efc4e955
--- /dev/null
+++ b/server/embed_frontend.go
@@ -0,0 +1,37 @@
+package server
+
+import (
+ "embed"
+ "io/fs"
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+)
+
+//go:embed dist
+var embeddedFiles embed.FS
+
+//go:embed dist/index.html
+var indexContent string
+
+func getFileSystem() http.FileSystem {
+ fs, err := fs.Sub(embeddedFiles, "dist")
+ if err != nil {
+ panic(err)
+ }
+
+ return http.FS(fs)
+}
+
+func embedFrontend(e *echo.Echo) {
+ // Catch-all route to return index.html, this is to prevent 404 when accessing non-root url.
+ // See https://stackoverflow.com/questions/27928372/react-router-urls-dont-work-when-refreshing-or-writing-manually
+ e.GET("/*", func(c echo.Context) error {
+ return c.HTML(http.StatusOK, indexContent)
+ })
+
+ assetHandler := http.FileServer(getFileSystem())
+ e.GET("/assets/*", echo.WrapHandler(assetHandler))
+ e.GET("/icons/*", echo.WrapHandler(assetHandler))
+ e.GET("/favicon.svg", echo.WrapHandler(assetHandler))
+}
diff --git a/server/server.go b/server/server.go
index d99617a7a..6b72d965a 100644
--- a/server/server.go
+++ b/server/server.go
@@ -38,12 +38,7 @@ func NewServer(profile *profile.Profile) *Server {
Timeout: 30 * time.Second,
}))
- e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
- Skipper: middleware.DefaultSkipper,
- Root: "web/dist",
- Browse: true,
- HTML5: true,
- }))
+ embedFrontend(e)
// In dev mode, set the const secret key to make signin session persistence.
secret := []byte("usememos")
diff --git a/web/index.html b/web/index.html
index 665904046..c7714cfe3 100644
--- a/web/index.html
+++ b/web/index.html
@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
- <link rel="icon" href="/logo.svg" sizes="64x64" type="image/*" />
+ <link rel="icon" href="/favicon.svg" sizes="64x64" type="image/*" />
<meta name="theme-color" content="#f6f5f4" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<title>Memos</title>
diff --git a/web/package.json b/web/package.json
index 3d3c34261..b3a202dc9 100644
--- a/web/package.json
+++ b/web/package.json
@@ -18,6 +18,7 @@
},
"devDependencies": {
"@types/lodash-es": "^4.17.5",
+ "@types/node": "^18.0.3",
"@types/qs": "^6.9.7",
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.4",
diff --git a/web/public/logo.svg b/web/public/favicon.svg
similarity index 100%
rename from web/public/logo.svg
rename to web/public/favicon.svg
diff --git a/web/vite.config.ts b/web/vite.config.ts
index 314b4280e..d89f16f30 100644
--- a/web/vite.config.ts
+++ b/web/vite.config.ts
@@ -1,5 +1,6 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
+import { resolve } from "path";
// https://vitejs.dev/config/
export default defineConfig({
@@ -17,4 +18,9 @@ export default defineConfig({
},
},
},
+ resolve: {
+ alias: {
+ "@/": `${resolve(__dirname, "src")}/`,
+ },
+ },
});
diff --git a/web/yarn.lock b/web/yarn.lock
index f80e741b9..8bba99a19 100644
--- a/web/yarn.lock
+++ b/web/yarn.lock
@@ -374,6 +374,11 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==
+"@types/node@^18.0.3":
+ version "18.0.3"
+ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.3.tgz#463fc47f13ec0688a33aec75d078a0541a447199"
+ integrity sha512-HzNRZtp4eepNitP+BD6k2L6DROIDG4Q0fm4x+dwfsr6LGmROENnok75VGw40628xf+iR24WeMFcHuuBDUAzzsQ==
+
"@types/prop-types@*":
version "15.7.5"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"