diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index eb75654..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: goreleaser - -on: - workflow_dispatch: # - -permissions: - contents: write - # packages: write - # issues: write - -jobs: - - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - - - name: Install npm dependencies - run: npm i - working-directory: web/ - - - name: Run npm build - run: npm run build - working-directory: web/ - - - name: Check build static - run: ls -la - working-directory: web/ - -# goreleaser: -# runs-on: ubuntu-latest -# steps: -# - uses: actions/checkout@v3 -# with: -# fetch-depth: 0 -# - run: git fetch --force --tags -# - uses: actions/setup-go@v3 -# with: -# go-version: '>=1.20.2' -# cache: true -# # More assembly might be required: Docker logins, GPG, etc. It all depends -# # on your needs. -# - uses: goreleaser/goreleaser-action@v4 -# with: -# # either 'goreleaser' (default) or 'goreleaser-pro': -# distribution: goreleaser -# version: latest -# args: release --clean -# env: -# GITHUB_TOKEN: ${{ secrets.REPO_GITHUB_TOKEN }} -# # Your GoReleaser Pro key, if you are using the 'goreleaser-pro' -# # distribution: -# # GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} -# -# diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1304dc5..6b71114 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,8 +14,10 @@ permissions: jobs: - build: + build-and-release: runs-on: ubuntu-latest + env: + NODE_ENV: prod steps: - name: Checkout repository @@ -26,7 +28,7 @@ jobs: working-directory: web/ - name: Run npm build - run: npm run build + run: export NODE_ENV=prod && npm run build working-directory: web/ - name: Check build static diff --git a/go.mod b/go.mod index 801aca6..b5391d8 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module message-nest go 1.20 require ( - github.com/astaxie/beego v1.9.3-0.20171218111859-f16688817aa4 github.com/dgrijalva/jwt-go v3.1.0+incompatible github.com/gin-gonic/gin v1.9.1 github.com/go-ini/ini v1.32.1-0.20180214101753-32e4be5f41bb @@ -12,6 +11,7 @@ require ( github.com/go-playground/validator/v10 v10.16.0 github.com/google/uuid v1.5.0 github.com/jinzhu/gorm v0.0.0-20180213101209-6e1387b44c64 + github.com/markbates/pkger v0.17.1 github.com/robfig/cron/v3 v3.0.1 github.com/sirupsen/logrus v1.9.3 github.com/unknwon/com v1.0.1 @@ -27,12 +27,12 @@ require ( github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-sql-driver/mysql v1.4.1-0.20190510102335-877a9775f068 // indirect + github.com/gobuffalo/here v0.6.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d // indirect github.com/jinzhu/now v1.0.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect - github.com/kr/pretty v0.1.0 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/lib/pq v1.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -51,7 +51,6 @@ require ( google.golang.org/appengine v1.6.3 // indirect google.golang.org/protobuf v1.32.0 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect - gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/ini.v1 v1.47.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 698a133..79da2db 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/astaxie/beego v1.9.3-0.20171218111859-f16688817aa4 h1:dNIynF6ICiq1NghlpIBxljb2JbyC61/JqWB5A9cfUfo= -github.com/astaxie/beego v1.9.3-0.20171218111859-f16688817aa4/go.mod h1:0R4++1tUqERR0WYFWdfkcrsyoVBCG4DgpDGokT3yb+U= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= @@ -37,6 +35,8 @@ github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqR github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-sql-driver/mysql v1.4.1-0.20190510102335-877a9775f068 h1:q2kwd9Bcgl2QpSi/Wjcx9jzwyICt3EWTP5to43QhwaA= github.com/go-sql-driver/mysql v1.4.1-0.20190510102335-877a9775f068/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI= +github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= @@ -73,6 +73,8 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno= +github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= @@ -100,6 +102,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -156,6 +159,8 @@ gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AW gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/ini.v1 v1.47.0 h1:XAVsOWcIxjm6JVEbCbSZgSBZIF0BrCzXs4orAQr6uc8= gopkg.in/ini.v1 v1.47.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 479338a..e6ed65a 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "embed" "fmt" "message-nest/pkg/table" "message-nest/pkg/util" @@ -16,6 +17,11 @@ import ( "message-nest/routers" ) +var ( + //go:embed web/dist/* + f embed.FS +) + func init() { setting.Setup() models.Setup() @@ -29,7 +35,7 @@ func init() { func main() { gin.SetMode(setting.ServerSetting.RunMode) - routersInit := routers.InitRouter() + routersInit := routers.InitRouter(f) readTimeout := setting.ServerSetting.ReadTimeout writeTimeout := setting.ServerSetting.WriteTimeout endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort) diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 8f3f1c1..e55634e 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -21,6 +21,8 @@ type Server struct { HttpPort int ReadTimeout time.Duration WriteTimeout time.Duration + + EmbedHtml string } var ServerSetting = &Server{} diff --git a/routers/router.go b/routers/router.go index f4c6a42..c2baa48 100644 --- a/routers/router.go +++ b/routers/router.go @@ -1,23 +1,42 @@ package routers import ( + "embed" "github.com/gin-gonic/gin" + "io/fs" "message-nest/middleware" + "message-nest/pkg/setting" "message-nest/routers/api" "message-nest/routers/api/v1" + "net/http" ) +// 打包集成静态资源 +func ServerStaticHtml(r *gin.Engine, f embed.FS) { + if setting.ServerSetting.EmbedHtml == "disable" { + return + } + assets, _ := fs.Sub(f, "web/dist/assets") + dist, _ := fs.Sub(f, "web/dist") + + r.StaticFS("assets/", http.FS(assets)) + r.GET("/", func(ctx *gin.Context) { + ctx.FileFromFS("/", http.FS(dist)) + }) + +} + // InitRouter initialize routing information -func InitRouter() *gin.Engine { +func InitRouter(f embed.FS) *gin.Engine { r := gin.New() r.Use(middleware.LogMiddleware()) - //gin.DefaultWriter = logging.Logger.Out - //gin.DefaultErrorWriter = logging.Logger.Out r.Use(gin.Recovery()) r.Use(middleware.Cors()) - r.POST("/auth", api.GetAuth) + // 打包 + ServerStaticHtml(r, f) + r.POST("/auth", api.GetAuth) apiv1 := r.Group("/api/v1") apiv1.Use(middleware.JWT()) { diff --git a/web/config.js b/web/config.js new file mode 100644 index 0000000..24f9f5f --- /dev/null +++ b/web/config.js @@ -0,0 +1,10 @@ +// config.js + +const isProduction = process.env.NODE_ENV === 'prod'; + +const config = { + apiUrl: isProduction ? '' : 'http://localhost:8000', +}; + +export default config; + diff --git a/web/src/api/api.js b/web/src/api/api.js index 3414293..e8ffd6e 100644 --- a/web/src/api/api.js +++ b/web/src/api/api.js @@ -3,11 +3,12 @@ import axios from 'axios'; import { ElMessage } from 'element-plus' import { usePageState } from '../store/page_sate'; import { CONSTANT } from '../constant'; +import config from '../../config.js'; const ERR_NETWORK = "ERR_NETWORK"; const request = axios.create({ - baseURL: 'http://127.0.0.1:8000', + baseURL: config.apiUrl, timeout: 50000, withCredentials: true, }); @@ -57,6 +58,9 @@ request.interceptors.response.use( // 异常处理 const handleException = (error) => { + console.log('99999999', config ) + console.log('99999999', config.apiUrl ) + console.log('handleException', error); if (error.code == ERR_NETWORK) { ElMessage({ message: `网络错误!`, type: 'error' })