Merge pull request #213 from yokowu/feat-embedded-vsix

feat: 嵌入vsix
This commit is contained in:
Yoko
2025-08-06 17:54:05 +08:00
committed by GitHub
6 changed files with 18 additions and 17 deletions

View File

@@ -99,6 +99,10 @@ jobs:
GIT_COMMIT=$(git rev-parse HEAD)
echo "GIT_COMMIT=${GIT_COMMIT}" >> $GITHUB_OUTPUT
- name: Get VSIX
run: |
wget -O assets/vsix/monkeycode-${{ steps.get_version.outputs.VERSION }}.vsix https://release.baizhi.cloud/monkeycode/vsixs/monkeycode-${{ steps.get_version.outputs.VERSION }}.vsix
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
backend/assets

View File

@@ -26,6 +26,7 @@ FROM alpine:3.21 as binary
WORKDIR /app
ADD migration ./migration
ADD assets ./assets
COPY --from=builder /out/main /app/main

View File

@@ -50,8 +50,6 @@ func main() {
panic(err)
}
s.euse.SyncLatest()
svc := service.NewService(service.WithPprof())
svc.Add(s)
if err := svc.Run(); err != nil {

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"log/slog"
"net/http"
"strings"
"github.com/labstack/echo/v4"
@@ -13,6 +14,7 @@ import (
"github.com/chaitin/MonkeyCode/backend/domain"
"github.com/chaitin/MonkeyCode/backend/internal/middleware"
"github.com/chaitin/MonkeyCode/backend/internal/proxy"
"github.com/chaitin/MonkeyCode/backend/pkg/version"
)
type V1Handler struct {
@@ -72,18 +74,15 @@ func BadRequest(c *web.Context, msg string) error {
}
func (h *V1Handler) Version(c *web.Context) error {
v, err := h.euse.Latest(c.Request().Context())
if err != nil {
return err
}
v := strings.Trim(version.Version, "v")
s, err := h.uuse.GetSetting(c.Request().Context())
if err != nil {
return err
}
return c.JSON(http.StatusOK, domain.VersionInfo{
Version: v.Version,
URL: fmt.Sprintf("%s/api/v1/static/vsix/%s", h.config.GetBaseURL(c.Request(), s), v.Version),
Version: v,
URL: fmt.Sprintf("%s/api/v1/static/vsix/%s", h.config.GetBaseURL(c.Request(), s), v),
})
}

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"log/slog"
"net/http"
"strings"
"sync"
"time"
@@ -19,6 +20,7 @@ import (
"github.com/chaitin/MonkeyCode/backend/errcode"
"github.com/chaitin/MonkeyCode/backend/internal/middleware"
"github.com/chaitin/MonkeyCode/backend/pkg/session"
"github.com/chaitin/MonkeyCode/backend/pkg/version"
"github.com/chaitin/MonkeyCode/backend/pkg/vsix"
)
@@ -173,11 +175,6 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
return c.String(http.StatusTooManyRequests, "Too Many Requests")
}
v, err := h.euse.GetByVersion(c.Request().Context(), c.Param("version"))
if err != nil {
return err
}
s, err := h.usecase.GetSetting(c.Request().Context())
if err != nil {
return err
@@ -185,14 +182,15 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
host := c.Request().Host
h.logger.With("url", c.Request().URL).With("header", c.Request().Header).With("host", host).DebugContext(c.Request().Context(), "vsix download")
cacheKey := h.generateCacheKey(v.Version, h.cfg.GetBaseURL(c.Request(), s))
cacheKey := h.generateCacheKey(version.Version, h.cfg.GetBaseURL(c.Request(), s))
version := strings.Trim(version.Version, "v")
h.cacheMu.RLock()
if entry, exists := h.vsixCache[cacheKey]; exists {
if time.Since(entry.createdAt) < time.Hour {
h.cacheMu.RUnlock()
disposition := fmt.Sprintf("attachment; filename=monkeycode-%s.vsix", v.Version)
disposition := fmt.Sprintf("attachment; filename=monkeycode-%s.vsix", version)
c.Response().Header().Set("Content-Type", "application/octet-stream")
c.Response().Header().Set("Content-Disposition", disposition)
c.Response().Header().Set("Content-Length", fmt.Sprintf("%d", len(entry.data)))
@@ -204,7 +202,7 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
h.cacheMu.RUnlock()
var buf bytes.Buffer
if err := vsix.ChangeVsixEndpoint(v.Path, "extension/package.json", h.cfg.GetBaseURL(c.Request(), s), &buf); err != nil {
if err := vsix.ChangeVsixEndpoint(fmt.Sprintf("/app/assets/vsix/monkeycode-%s.vsix", version), "extension/package.json", h.cfg.GetBaseURL(c.Request(), s), &buf); err != nil {
return err
}
@@ -218,7 +216,7 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
go h.cleanExpiredCache()
disposition := fmt.Sprintf("attachment; filename=monkeycode-%s.vsix", v.Version)
disposition := fmt.Sprintf("attachment; filename=monkeycode-%s.vsix", version)
c.Response().Header().Set("Content-Type", "application/octet-stream")
c.Response().Header().Set("Content-Disposition", disposition)
c.Response().Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))