chore: remove unused resource route

This commit is contained in:
Steven 2024-07-28 23:54:53 +08:00
parent 51ed88d5aa
commit 2ad51a3d42
4 changed files with 0 additions and 61 deletions

1
go.mod
View File

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

2
go.sum
View File

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

View File

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

View File

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