feat: 增加win打包

This commit is contained in:
fuzhongyun 2026-03-02 13:40:24 +08:00
parent c0680a6ae4
commit dbd72287d4
7 changed files with 67 additions and 61 deletions

View File

@ -1,11 +0,0 @@
.git
.github
.trae
deploy
**/*_test.go
**/*.log
**/*.tmp
**/*.swp
**/.DS_Store
**/node_modules
**/.vscode

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.trae
.vscode
__debug*
__debug*
dist

View File

@ -1,43 +0,0 @@
FROM golang:1.21-alpine AS builder
ARG APK_MIRROR=https://mirrors.aliyun.com/alpine
ARG GOPROXY=https://goproxy.cn,direct
RUN set -eux; \
printf '%s\n' \
"${APK_MIRROR}/v3.20/main" \
"${APK_MIRROR}/v3.20/community" > /etc/apk/repositories; \
apk add --no-cache ca-certificates tzdata git
WORKDIR /src
COPY go.mod go.sum ./
ENV GOPROXY=${GOPROXY}
RUN go mod download
COPY . .
RUN set -eux; \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags="-s -w" -o /out/qr-scanner .
FROM alpine:3.20
ARG APK_MIRROR=https://mirrors.aliyun.com/alpine
RUN set -eux; \
printf '%s\n' \
"${APK_MIRROR}/v3.20/main" \
"${APK_MIRROR}/v3.20/community" > /etc/apk/repositories; \
apk add --no-cache ca-certificates tzdata; \
addgroup -S app && adduser -S -G app app
WORKDIR /app
COPY --from=builder /out/qr-scanner /app/qr-scanner
COPY --from=builder /src/static /app/static
USER app
EXPOSE 8080
ENTRYPOINT ["/app/qr-scanner"]

View File

@ -14,7 +14,7 @@
环境变量:
- `QR_SCANNER_PORT`:端口(默认 8080
- `QR_SCANNER_PORT`:端口(默认 8001
- `QR_SCANNER_TEMP_DIR`:临时目录(默认 /tmp/qr-scanner
- `QR_SCANNER_RETENTION_MINUTES`:结果保留分钟数(默认 30
- `QR_SCANNER_DEFAULT_WORKERS`:默认并发(默认 4
@ -29,7 +29,7 @@ go run .
浏览器打开:
- `http://localhost:8080/`
- `http://localhost:8001/`
## APIv1.0

View File

@ -24,7 +24,7 @@ type Config struct {
func Default() Config {
return Config{
Host: "0.0.0.0",
Port: 8080,
Port: 8001,
TempDir: "/tmp/qr-scanner",
MaxUploadMB: 100,
MaxFiles: 10000,

19
deploy.sh Normal file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT_DIR"
OUT_DIR="$ROOT_DIR/dist"
mkdir -p "$OUT_DIR"
APP_NAME="qr-scanner"
OUT_EXE="$OUT_DIR/${APP_NAME}.exe"
echo "Building Windows exe -> $OUT_EXE"
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
go build -trimpath -ldflags "-s -w" -o "$OUT_EXE" .
echo "Done."
echo "Run on Windows: double-click ${APP_NAME}.exe (auto opens http://localhost:8001/)"

46
main.go
View File

@ -1,8 +1,14 @@
package main
import (
"embed"
"fmt"
"io/fs"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"time"
"github.com/gin-gonic/gin"
@ -12,6 +18,9 @@ import (
"qr-scanner/services"
)
//go:embed static/*
var staticFS embed.FS
func main() {
cfg := config.Default().WithEnv()
@ -35,8 +44,12 @@ func main() {
r.MaxMultipartMemory = cfg.MaxUploadMB * 1024 * 1024
}
r.GET("/", func(c *gin.Context) { c.File("./static/index.html") })
r.Static("/static", "./static")
sub, err := fs.Sub(staticFS, "static")
if err != nil {
panic(err)
}
r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusFound, "/static/index.html") })
r.StaticFS("/static", http.FS(sub))
api := r.Group("/api")
{
@ -59,7 +72,34 @@ func main() {
}()
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
if err := r.Run(addr); err != nil {
ln, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
openURL := fmt.Sprintf("http://%s:%d/", browserHost(cfg.Host), cfg.Port)
_ = openBrowser(openURL)
srv := &http.Server{Handler: r}
if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
panic(err)
}
}
func browserHost(listenHost string) string {
if listenHost == "" || listenHost == "0.0.0.0" || listenHost == "::" {
return "localhost"
}
return listenHost
}
func openBrowser(url string) error {
switch runtime.GOOS {
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
return exec.Command("open", url).Start()
default:
return exec.Command("xdg-open", url).Start()
}
}