diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 445020db6..cf75df0bf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -471,7 +471,9 @@ jobs: VERSION: ${{ needs.get-release.outputs.version }} with: push: ${{ github.event.inputs.test == 'true' && 'false' || 'true' }} - build-args: VERSION=${{ env.VERSION }} + build-args: + - VERSION=${{ env.VERSION }} + - IN_DOCKER=true platforms: | linux/386 linux/amd64 diff --git a/Dockerfile b/Dockerfile index 997d5e8b7..a59b293c4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,10 @@ RUN go mod download COPY . . COPY --from=flutter /app/build/web ./cmd/web/dist ARG VERSION=dev -RUN CGO_ENABLED=0 go build -tags nosqlite,web -ldflags="-s -w -X github.com/GopeedLab/gopeed/pkg/base.Version=${VERSION}" -o dist/gopeed github.com/GopeedLab/gopeed/cmd/web +RUN CGO_ENABLED=0 go build -tags nosqlite,web -ldflags="-s -w \ + -X github.com/GopeedLab/gopeed/pkg/base.Version=${VERSION}" \ + -X github.com/GopeedLab/gopeed/pkg/base.InDocker=${IN_DOCKER}" \ + -o dist/gopeed github.com/GopeedLab/gopeed/cmd/web FROM alpine:3.14.2 LABEL maintainer="monkeyWie" diff --git a/cmd/server.go b/cmd/server.go index e0464cc65..6ee0bf2f2 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -3,6 +3,7 @@ package cmd import ( _ "embed" "fmt" + "github.com/GopeedLab/gopeed/pkg/base" "github.com/GopeedLab/gopeed/pkg/rest" "github.com/GopeedLab/gopeed/pkg/rest/model" "net/http" @@ -24,14 +25,20 @@ func Start(cfg *model.StartConfig) { panic(err) } if downloadCfg.FirstLoad { - // Set default download dir to user download dir - userDir, err := os.UserHomeDir() - if err == nil { - downloadDir := filepath.Join(userDir, "Downloads") + // Set default download dir, in docker, it will be ${exe}/Downloads, else it will be ${user}/Downloads + var downloadDir string + if base.InDocker { + downloadDir = filepath.Join(filepath.Dir(cfg.StorageDir), "Downloads") + } else { + userDir, err := os.UserHomeDir() + if err == nil { + downloadDir = filepath.Join(userDir, "Downloads") + } + } + if downloadDir != "" { downloadCfg.DownloadDir = downloadDir rest.Downloader.PutConfig(downloadCfg) } - } fmt.Printf("Server start success on http://%s\n", listener.Addr().String()) if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed { diff --git a/pkg/base/version.go b/pkg/base/info.go similarity index 92% rename from pkg/base/version.go rename to pkg/base/info.go index 46b10e3b4..379a3c6c3 100644 --- a/pkg/base/version.go +++ b/pkg/base/info.go @@ -2,6 +2,7 @@ package base // Version is the build version, set at build time, using `go build -ldflags "-X github.com/GopeedLab/gopeed/pkg/base.Version=1.0.0"`. var Version string +var InDocker bool func init() { if Version == "" {