fix: serve ui via fallback and enhance makefile

This commit is contained in:
shenlan 2025-08-02 22:42:39 +08:00
parent 055204eb68
commit 3e2f9a7af1
2 changed files with 51 additions and 12 deletions

View File

@ -2,9 +2,11 @@ package main
import (
"context"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
@ -25,10 +27,24 @@ func main() {
}
}
uiFS, err := fs.Sub(ui.Assets, "dist")
if err != nil {
log.Fatalf("ui assets: %v", err)
}
r := server.New(
api.RegisterRoutes,
func(r *gin.Engine) { markmind.RegisterRoutes(r, conn) },
func(r *gin.Engine) { r.StaticFS("/", http.FS(ui.Assets)) },
func(r *gin.Engine) {
fileServer := http.FileServer(http.FS(uiFS))
r.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.AbortWithStatus(http.StatusNotFound)
return
}
fileServer.ServeHTTP(c.Writer, c.Request)
})
},
)
r.Run() // listen and serve on 0.0.0.0:8080

View File

@ -1,27 +1,50 @@
APP_NAME := xcontrol-server
MAIN_FILE := ../cmd/api/main.go
MODULE := xcontrol
PORT := 3001
.PHONY: all build run clean init help
.PHONY: all build run clean init help dev
.RECIPEPREFIX := >
all: build
init:
go mod tidy
> @echo ">>> 初始化 Go 依赖环境"
> @if curl -s --max-time 5 https://goproxy.cn >/dev/null; then \
> echo "使用国内镜像: goproxy.cn"; \
> go env -w GOPROXY=https://goproxy.cn,direct; \
> else \
> echo "国内镜像不可用,使用默认: proxy.golang.org"; \
> go env -w GOPROXY=https://proxy.golang.org,direct; \
> fi
> go mod tidy
build:
go build -o $(APP_NAME) $(MAIN_FILE)
> @echo ">>> 编译 $(APP_NAME)"
> go build -o $(APP_NAME) $(MAIN_FILE)
run:
go run $(MAIN_FILE)
> @echo ">>> 运行 $(APP_NAME) on port $(PORT)"
> PORT=$(PORT) go run $(MAIN_FILE)
dev:
> @echo ">>> 开发模式运行 $(APP_NAME) (热重载) on port $(PORT)"
> @if ! command -v air >/dev/null; then \
> echo "未检测到 air请先运行: go install github.com/cosmtrek/air@latest"; \
> exit 1; \
> fi
> PORT=$(PORT) air -c .air.toml
clean:
rm -f $(APP_NAME)
> @echo ">>> 清理构建产物"
> rm -f $(APP_NAME)
help:
@echo " XControl Server Makefile"
@echo ""
@echo "make build 编译 server 可执行文件"
@echo "make run 运行 server"
@echo "make init 初始化依赖"
@echo "make clean 清理构建产物"
> @echo " XControl Server Makefile"
> @echo ""
> @echo "make build 编译 server 可执行文件"
> @echo "make run 运行 server (默认端口: $(PORT))"
> @echo "make dev 开发模式运行 (热重载, 默认端口: $(PORT))"
> @echo "make init 初始化依赖(自动选择国内/默认 Go 模块代理)"
> @echo "make clean 清理构建产物"