From 68413a5371c7f1ecca5b5b7313ab0aac8060b39f Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 23 Dec 2023 17:04:52 +0800 Subject: [PATCH] chore: update frontend service --- go.mod | 4 ---- go.sum | 5 ----- server/frontend/frontend.go | 17 +++++++++-------- server/server.go | 8 +------- 4 files changed, 10 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 53341bfc..ac3df717 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,6 @@ require ( github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.17.0 github.com/stretchr/testify v1.8.4 - github.com/swaggo/echo-swagger v1.4.1 github.com/swaggo/swag v1.16.2 go.uber.org/zap v1.26.0 golang.org/x/crypto v0.17.0 @@ -43,7 +42,6 @@ require ( github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/ghodss/yaml v1.0.0 // indirect github.com/go-openapi/jsonpointer v0.20.0 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/spec v0.20.9 // indirect @@ -60,12 +58,10 @@ require ( github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/stoewer/go-strcase v1.3.0 // indirect - github.com/swaggo/files/v2 v2.0.0 // indirect golang.org/x/image v0.13.0 // indirect golang.org/x/tools v0.14.0 // indirect google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect lukechampine.com/uint128 v1.3.0 // indirect modernc.org/cc/v3 v3.41.0 // indirect modernc.org/ccgo/v3 v3.16.15 // indirect diff --git a/go.sum b/go.sum index 222d458c..5ffe9cfb 100644 --- a/go.sum +++ b/go.sum @@ -162,7 +162,6 @@ github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7z github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= @@ -549,10 +548,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= -github.com/swaggo/echo-swagger v1.4.1 h1:Yf0uPaJWp1uRtDloZALyLnvdBeoEL5Kc7DtnjzO/TUk= -github.com/swaggo/echo-swagger v1.4.1/go.mod h1:C8bSi+9yH2FLZsnhqMZLIZddpUxZdBYuNHbtaS1Hljc= -github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw= -github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM= github.com/swaggo/swag v1.16.2 h1:28Pp+8DkQoV+HLzLx8RGJZXNGKbFqnuvSbAAtoxiY04= github.com/swaggo/swag v1.16.2/go.mod h1:6YzXnDcpr0767iOejs318CwYkCQqyGer6BizOg03f+E= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go index 7202b55e..561d7f97 100644 --- a/server/frontend/frontend.go +++ b/server/frontend/frontend.go @@ -35,17 +35,18 @@ func (s *FrontendService) Serve(e *echo.Echo) { // Use echo static middleware to serve the built dist folder. // refer: https://github.com/labstack/echo/blob/master/middleware/static.go e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ - Root: "dist", - Skipper: defaultAPIRequestSkipper, - HTML5: true, + HTML5: true, + Filesystem: http.Dir("dist"), + Skipper: func(c echo.Context) bool { + return util.HasPrefixes(c.Path(), "/api", "/memos.api.v2", "/robots.txt", "/sitemap.xml", "/m/:memoID") + }, })) s.registerRoutes(e) } func (s *FrontendService) registerRoutes(e *echo.Echo) { - rawIndexHTMLBytes, _ := os.ReadFile("dist/index.html") - rawIndexHTML := string(rawIndexHTMLBytes) + rawIndexHTML := getRawIndexHTML() e.GET("/robots.txt", func(c echo.Context) error { ctx := c.Request().Context() @@ -173,7 +174,7 @@ func generateMemoMetadata(memo *store.Memo, creator *store.User) string { return strings.Join(metadataList, "\n") } -func defaultAPIRequestSkipper(c echo.Context) bool { - path := c.Request().URL.Path - return util.HasPrefixes(path, "/api", "/memos.api.v2") +func getRawIndexHTML() string { + bytes, _ := os.ReadFile("dist/index.html") + return string(bytes) } diff --git a/server/server.go b/server/server.go index 60c8e964..9aabb27e 100644 --- a/server/server.go +++ b/server/server.go @@ -11,7 +11,6 @@ import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/pkg/errors" - echoSwagger "github.com/swaggo/echo-swagger" apiv1 "github.com/usememos/memos/api/v1" apiv2 "github.com/usememos/memos/api/v2" @@ -74,15 +73,10 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store } s.ID = serverID - // Serve frontend. + // Register frontend service. frontendService := frontend.NewFrontendService(profile, store) frontendService.Serve(e) - // Serve swagger in dev/demo mode. - if profile.Mode == "dev" || profile.Mode == "demo" { - e.GET("/api/*", echoSwagger.WrapHandler) - } - secret := "usememos" if profile.Mode == "prod" { secret, err = s.getSystemSecretSessionName(ctx)