mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
feat: add resource service
This commit is contained in:
parent
96ab5b226d
commit
b9e5e7f2af
1
go.mod
1
go.mod
@ -70,6 +70,7 @@ require (
|
||||
require (
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2
|
||||
github.com/h2non/filetype v1.1.3
|
||||
github.com/mssola/useragent v1.0.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
go.deanishe.net/favicon v0.1.0
|
||||
|
2
go.sum
2
go.sum
@ -171,6 +171,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
|
||||
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2 h1:dygLcbEBA+t/P7ck6a8AkXv6juQ4cK0RHBoh32jxhHM=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2/go.mod h1:Ap9RLCIJVtgQg1/BBgVEfypOAySvvlcpcVQkSzJCH4Y=
|
||||
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/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
|
55
server/resource.go
Normal file
55
server/resource.go
Normal file
@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/boojack/slash/server/profile"
|
||||
"github.com/boojack/slash/store"
|
||||
"github.com/h2non/filetype"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
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 (s *ResourceService) Register(g *echo.Group) {
|
||||
g.GET("/resources/:id", func(c echo.Context) error {
|
||||
ctx := c.Request().Context()
|
||||
resourceID := c.Param("resourceId")
|
||||
resourceRelativePathSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
|
||||
Key: store.WorkspaceResourceRelativePath,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Failed to workspace resource relative path setting").SetInternal(err)
|
||||
}
|
||||
resourceRelativePath := resourceRelativePathSetting.Value
|
||||
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))
|
||||
})
|
||||
}
|
@ -76,6 +76,10 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
|
||||
return nil, fmt.Errorf("failed to register gRPC gateway: %w", err)
|
||||
}
|
||||
|
||||
// Register resource service.
|
||||
resourceService := NewResourceService(profile, store)
|
||||
resourceService.Register(rootGroup)
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
@ -8,10 +8,12 @@ import (
|
||||
type WorkspaceSettingKey string
|
||||
|
||||
const (
|
||||
// WorkspaceDisallowSignUp is the key type for disallow sign up in workspace level.
|
||||
WorkspaceDisallowSignUp WorkspaceSettingKey = "disallow-signup"
|
||||
// WorkspaceSecretSessionName is the key type for secret session name.
|
||||
WorkspaceSecretSessionName WorkspaceSettingKey = "secret-session-name"
|
||||
// WorkspaceDisallowSignUp is the key type for disallow sign up in workspace level.
|
||||
WorkspaceDisallowSignUp WorkspaceSettingKey = "disallow-signup"
|
||||
// WorkspaceResourceRelativePath is the key type for resource relative path.
|
||||
WorkspaceResourceRelativePath WorkspaceSettingKey = "resource-relative-path"
|
||||
)
|
||||
|
||||
// String returns the string format of WorkspaceSettingKey type.
|
||||
|
Loading…
x
Reference in New Issue
Block a user