From 1ba9790a42bc1802e58b17d630c07d4ff0e7eb13 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Fri, 20 Nov 2020 22:52:44 +0800 Subject: [PATCH] server checks if current device is mobile device, and let browser navigate to specific pages --- cmd/webserver.go | 9 ++++++++- go.mod | 1 + go.sum | 2 ++ src/Desktop.vue | 8 ++++++-- src/consts/licenses.js | 6 ++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cmd/webserver.go b/cmd/webserver.go index 01a62a6b..9ef86b08 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -9,6 +9,7 @@ import ( "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/lab/pkg/api" @@ -81,7 +82,13 @@ func startWebServer(c *cli.Context) error { router.NoMethod(bindApi(api.Default.MethodNotAllowed)) router.GET("/", func(c *gin.Context) { - c.Redirect(http.StatusMovedPermanently, "/mobile/") + ua := user_agent.New(c.GetHeader("User-Agent")) + + if ua.Mobile() { + c.Redirect(http.StatusMovedPermanently, "/mobile/") + } else { + c.Redirect(http.StatusMovedPermanently, "/desktop/") + } }) router.StaticFile("robots.txt", filepath.Join(config.StaticRootPath, "robots.txt")) diff --git a/go.mod b/go.mod index 6c84aa04..a32129f9 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ 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 a6ef9c19..975cfd2b 100644 --- a/go.sum +++ b/go.sum @@ -61,6 +61,8 @@ 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 95feb14e..0fad821f 100644 --- a/src/Desktop.vue +++ b/src/Desktop.vue @@ -1,7 +1,11 @@ diff --git a/src/consts/licenses.js b/src/consts/licenses.js index 7549c3bc..3354cdfe 100644 --- a/src/consts/licenses.js +++ b/src/consts/licenses.js @@ -69,6 +69,12 @@ const licenses = [ 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.',