From 2ad51a3d42befeee65550dc63c5232cc1ebf8c58 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 28 Jul 2024 23:54:53 +0800 Subject: [PATCH] chore: remove unused resource route --- go.mod | 1 - go.sum | 2 -- server/server.go | 6 ---- server/service/resource/resource.go | 52 ----------------------------- 4 files changed, 61 deletions(-) delete mode 100644 server/service/resource/resource.go diff --git a/go.mod b/go.mod index a56dc9f..53dd200 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,6 @@ require ( require ( github.com/golang-jwt/jwt/v5 v5.2.1 github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 - github.com/h2non/filetype v1.1.3 github.com/improbable-eng/grpc-web v0.15.0 github.com/joho/godotenv v1.5.1 github.com/lib/pq v1.10.9 diff --git a/go.sum b/go.sum index c00781b..ce18179 100644 --- a/go.sum +++ b/go.sum @@ -146,8 +146,6 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= -github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= -github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= diff --git a/server/server.go b/server/server.go index f1788de..9dad631 100644 --- a/server/server.go +++ b/server/server.go @@ -18,7 +18,6 @@ import ( apiv1 "github.com/yourselfhosted/slash/server/route/api/v1" "github.com/yourselfhosted/slash/server/route/frontend" "github.com/yourselfhosted/slash/server/service/license" - "github.com/yourselfhosted/slash/server/service/resource" "github.com/yourselfhosted/slash/store" ) @@ -70,17 +69,12 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store return c.String(http.StatusOK, "Service ready.") }) - rootGroup := e.Group("") s.apiV1Service = apiv1.NewAPIV1Service(secret, profile, store, licenseService, s.Profile.Port+1) // Register gRPC gateway as api v1. if err := s.apiV1Service.RegisterGateway(ctx, e); err != nil { return nil, errors.Wrap(err, "failed to register gRPC gateway") } - // Register resource service. - resourceService := resource.NewResourceService(profile, store) - resourceService.Register(rootGroup) - return s, nil } diff --git a/server/service/resource/resource.go b/server/service/resource/resource.go deleted file mode 100644 index af76972..0000000 --- a/server/service/resource/resource.go +++ /dev/null @@ -1,52 +0,0 @@ -package resource - -import ( - "bytes" - "fmt" - "net/http" - "os" - - "github.com/h2non/filetype" - "github.com/labstack/echo/v4" - - "github.com/yourselfhosted/slash/server/profile" - "github.com/yourselfhosted/slash/store" -) - -const ( - resourceRelativePath = "resources" -) - -type ResourceService struct { - Profile *profile.Profile - Store *store.Store -} - -func NewResourceService(profile *profile.Profile, store *store.Store) *ResourceService { - return &ResourceService{ - Profile: profile, - Store: store, - } -} - -// Register registers the resource service to the echo server. -func (*ResourceService) Register(g *echo.Group) { - g.GET("/resources/:id", func(c echo.Context) error { - resourceID := c.Param("resourceId") - resourcePath := fmt.Sprintf("%s/%s", resourceRelativePath, resourceID) - buf, err := os.ReadFile(resourcePath) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to read the local resource: %s", resourcePath)).SetInternal(err) - } - - kind, err := filetype.Match(buf) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to match the local resource: %s", resourcePath)).SetInternal(err) - } - resourceMimeType := kind.MIME.Value - c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable") - c.Response().Writer.Header().Set(echo.HeaderContentSecurityPolicy, "default-src 'self'") - c.Response().Writer.Header().Set("Content-Disposition", fmt.Sprintf(`filename="%s"`, resourceID)) - return c.Stream(http.StatusOK, resourceMimeType, bytes.NewReader(buf)) - }) -}