From 300a5717a75cbace51943e6faad6031ad1c5b145 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Mon, 16 May 2022 20:44:31 +0800 Subject: [PATCH] admin added --- .github/workflows/release.yml | 8 +++++++- .gitignore | 2 +- admin/.env | 1 + admin/.env.development | 2 -- admin/src/components.d.ts | 12 ++++++++++++ admin/src/utils/request.ts | 1 - helper/Dockerfile-release | 8 ++++++++ main.go | 24 +++++++++++++++--------- 8 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 admin/.env delete mode 100644 admin/.env.development diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b698e42..a4cc206 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,7 +10,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Check out code - uses: actions/checkout@v2 + uses: actions/checkout@v3 + uses: actions/setup-node@v3 + with: + node-version: 14 + - run: cd admin + - run: yarn + - run: VITE_ADMIN_VERSION=${GITHUB_REF#refs/*/} yarn build - name: Validates GO releaser config uses: docker://goreleaser/goreleaser:v1.7.0 diff --git a/.gitignore b/.gitignore index f0476cb..135fe3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ conf.yml *.out *.log -dist +# dist .idea/** .vscode default.etcd diff --git a/admin/.env b/admin/.env new file mode 100644 index 0000000..fde6533 --- /dev/null +++ b/admin/.env @@ -0,0 +1 @@ +VITE_ADMIN_VERSION="v0.0.0-dev" diff --git a/admin/.env.development b/admin/.env.development deleted file mode 100644 index cd123c3..0000000 --- a/admin/.env.development +++ /dev/null @@ -1,2 +0,0 @@ -VITE_PROXY=[["/api", "http://localhost:36789"]] -VITE_ADMIN_VERSION="v0.0.0-dev" diff --git a/admin/src/components.d.ts b/admin/src/components.d.ts index 10b07fd..1c6807f 100644 --- a/admin/src/components.d.ts +++ b/admin/src/components.d.ts @@ -5,6 +5,18 @@ import '@vue/runtime-core' declare module '@vue/runtime-core' { export interface GlobalComponents { + ABreadcrumb: typeof import('ant-design-vue/es')['Breadcrumb'] + ABreadcrumbItem: typeof import('ant-design-vue/es')['BreadcrumbItem'] + AButton: typeof import('ant-design-vue/es')['Button'] + ALayout: typeof import('ant-design-vue/es')['Layout'] + ALayoutContent: typeof import('ant-design-vue/es')['LayoutContent'] + ALayoutHeader: typeof import('ant-design-vue/es')['LayoutHeader'] + ALayoutSider: typeof import('ant-design-vue/es')['LayoutSider'] + AMenu: typeof import('ant-design-vue/es')['Menu'] + AMenuItem: typeof import('ant-design-vue/es')['MenuItem'] + ASubMenu: typeof import('ant-design-vue/es')['SubMenu'] + ATable: typeof import('ant-design-vue/es')['Table'] + ATag: typeof import('ant-design-vue/es')['Tag'] RouterLink: typeof import('vue-router')['RouterLink'] RouterView: typeof import('vue-router')['RouterView'] SvgIcon: typeof import('./components/SvgIcon/index.vue')['default'] diff --git a/admin/src/utils/request.ts b/admin/src/utils/request.ts index 09b32db..1ce371f 100644 --- a/admin/src/utils/request.ts +++ b/admin/src/utils/request.ts @@ -1,7 +1,6 @@ import axios from 'axios' const request = axios.create({ - baseURL: import.meta.env.VITE_APP_API_BASE_URL as string | undefined, timeout: 60000 }) diff --git a/helper/Dockerfile-release b/helper/Dockerfile-release index 45127c2..da9a40d 100644 --- a/helper/Dockerfile-release +++ b/helper/Dockerfile-release @@ -1,4 +1,11 @@ # syntax=docker/dockerfile:1 +FROM node:14.5.0-alpine as bulder1 +ARG RELEASE_VERSION +WORKDIR /app/dtm +COPY . . +RUN cd admin && yarn +RUN VITE_ADMIN_VERSION=$RELEASE_VERSION yarn build + FROM --platform=$TARGETPLATFORM golang:1.16-alpine as builder ARG TARGETARCH ARG TARGETOS @@ -7,6 +14,7 @@ WORKDIR /app/dtm # RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct EXPOSE 8080 COPY . . +COPY --from=bulder1 /app/dtm/admin/dist /app/dtm/admin RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="-s -w -X main.Version=$RELEASE_VERSION" FROM --platform=$TARGETPLATFORM alpine diff --git a/main.go b/main.go index bfdbecf..fb47aec 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "embed" "fmt" "io/fs" + "io/ioutil" "net/http" "net/http/httputil" "net/url" @@ -35,10 +36,7 @@ func main() { //go:embed admin/dist var admin embed.FS -//go:embed admin/dist/index.html -var indexFile string - -var target = "" +var target = "admin.dtm.pub" func getSub(f1 fs.FS, sub string) fs.FS { f2, err := fs.Sub(f1, sub) @@ -46,14 +44,22 @@ func getSub(f1 fs.FS, sub string) fs.FS { return f2 } func addAdmin(app *gin.Engine, conf *config.ConfigType) { + // for released dtm, serve admin from local files because the build output has been embed + // for testing users, proxy admin to target because the build output has not been embed dist := getSub(admin, "admin/dist") - _, err := dist.Open("index.html") + index, err := dist.Open("index.html") if err == nil { - app.StaticFS("/assets", http.FS(getSub(dist, "assets"))) - app.GET("/admin/*name", func(c *gin.Context) { + defer index.Close() + cont, err := ioutil.ReadAll(index) + logger.FatalIfError(err) + sfile := string(cont) + renderIndex := func(c *gin.Context) { c.Header("content-type", "text/html;charset=utf-8") - c.String(200, indexFile) - }) + c.String(200, sfile) + } + app.StaticFS("/assets", http.FS(getSub(dist, "assets"))) + app.GET("/admin/*name", renderIndex) + app.GET("/", renderIndex) logger.Infof("admin is served from dir 'admin/dist/'") } else { app.GET("/", proxyAdmin)