From f0765132142cb15a8b363e5e11467c48c1022cb9 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 30 May 2021 21:48:12 +0800 Subject: [PATCH] move the function of switching desktop/mobile page to frontend --- cmd/webserver.go | 14 +++----------- go.mod | 1 - go.sum | 2 -- src/Desktop.vue | 6 +++++- src/index-main.js | 35 +++++++++++++++++++++++++++++++++++ src/public/index.html | 26 ++++++++++++++++++++++++++ third-patry-dependencies.json | 6 ------ vue.config.js | 6 ++++++ 8 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 src/index-main.js create mode 100644 src/public/index.html diff --git a/cmd/webserver.go b/cmd/webserver.go index f8796779..ad65f68d 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -2,14 +2,12 @@ package cmd import ( "fmt" - "net/http" "path/filepath" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/validator/v10" - "github.com/mssola/user_agent" "github.com/urfave/cli/v2" "github.com/mayswind/ezbookkeeping/pkg/api" @@ -91,19 +89,13 @@ func startWebServer(c *cli.Context) error { router.NoRoute(bindApi(api.Default.ApiNotFound)) router.NoMethod(bindApi(api.Default.MethodNotAllowed)) - router.GET("/", func(c *gin.Context) { - ua := user_agent.New(c.GetHeader("User-Agent")) - - if ua.Mobile() { - c.Redirect(http.StatusMovedPermanently, "/mobile/") - } else { - c.Redirect(http.StatusMovedPermanently, "/desktop/") - } - }) + router.StaticFile("/", filepath.Join(config.StaticRootPath, "index.html")) router.StaticFile("robots.txt", filepath.Join(config.StaticRootPath, "robots.txt")) router.StaticFile("favicon.ico", filepath.Join(config.StaticRootPath, "favicon.ico")) router.StaticFile("favicon.png", filepath.Join(config.StaticRootPath, "favicon.png")) + router.StaticFile("touchicon.png", filepath.Join(config.StaticRootPath, "touchicon.png")) + router.StaticFile("manifest.json", filepath.Join(config.StaticRootPath, "manifest.json")) mobileEntryRoute := router.Group("/mobile") mobileEntryRoute.Use(bindMiddleware(middlewares.ServerSettingsCookie(config))) diff --git a/go.mod b/go.mod index 7f8146d4..94e3706e 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/go-sql-driver/mysql v1.5.0 github.com/lib/pq v1.8.0 github.com/mattn/go-sqlite3 v1.14.4 - github.com/mssola/user_agent v0.5.2 github.com/pquerna/otp v1.3.0 github.com/sirupsen/logrus v1.7.0 github.com/smartystreets/goconvey v1.6.4 // indirect diff --git a/go.sum b/go.sum index 975cfd2b..a6ef9c19 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,6 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/mssola/user_agent v0.5.2 h1:CZkTUahjL1+OcZ5zv3kZr8QiJ8jy2H08vZIEkBeRbxo= -github.com/mssola/user_agent v0.5.2/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/src/Desktop.vue b/src/Desktop.vue index 0fad821f..9648ab2b 100644 --- a/src/Desktop.vue +++ b/src/Desktop.vue @@ -5,7 +5,11 @@ diff --git a/src/index-main.js b/src/index-main.js new file mode 100644 index 00000000..f666fd43 --- /dev/null +++ b/src/index-main.js @@ -0,0 +1,35 @@ +import uaParser from 'ua-parser-js'; + +function isMobileDevice() { + if (!navigator.userAgent) { + return false; + } + + const uaParseRet = uaParser(navigator.userAgent); + + if (!uaParseRet || !uaParseRet.device) { + return false; + } + + const device = uaParseRet.device; + + if (device.type === 'mobile' || device.type === 'tablet' || device.type === 'wearable' || device.type === 'embedded') { + return true; + } + + return false; +} + +function navigate(type) { + if (process.env.NODE_ENV === 'production') { + window.location.replace(`${type}/`); + } else { + window.location.replace(`${type}.html`); + } +} + +if (isMobileDevice()) { + navigate('mobile'); +} else { + navigate('desktop'); +} diff --git a/src/public/index.html b/src/public/index.html new file mode 100644 index 00000000..c5637f6d --- /dev/null +++ b/src/public/index.html @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + ezBookkeeping + + + + + + +
+ + diff --git a/third-patry-dependencies.json b/third-patry-dependencies.json index fdab60fa..1b8c8fe6 100644 --- a/third-patry-dependencies.json +++ b/third-patry-dependencies.json @@ -69,12 +69,6 @@ "url": "https://github.com/mattn/go-sqlite3", "licenseUrl": "https://github.com/mattn/go-sqlite3/blob/master/LICENSE" }, - { - "name": "UserAgent", - "copyright": "Copyright (c) 2012-2020 Miquel Sabaté Solà", - "url": "https://github.com/mssola/user_agent", - "licenseUrl": "https://github.com/mssola/user_agent/blob/master/LICENSE" - }, { "name": "Go Cryptography", "copyright": "Copyright (c) 2009 The Go Authors. All rights reserved.", diff --git a/vue.config.js b/vue.config.js index 25fd17c2..4155800c 100644 --- a/vue.config.js +++ b/vue.config.js @@ -10,6 +10,12 @@ const licenseFile = fs.readFileSync('./LICENSE', 'UTF-8'); module.exports = { pages: { + index: { + entry: 'src/index-main.js', + template: 'src/public/index.html', + filename: 'index.html', + chunks: ['vendors-common-bundle', 'vendors-index-bundle', 'common-bundle', 'index'] + }, desktop: { entry: 'src/desktop-main.js', template: 'src/public/desktop.html',