Browse Source

feat: 优化配置,给配置默认值

tuon 1 year ago
parent
commit
c98a97c23f
5 changed files with 33 additions and 48 deletions
  1. 25 13
      server/config/config.go
  2. 2 2
      server/db/db.go
  3. 1 1
      server/go.mod
  4. 1 9
      server/go.sum
  5. 4 23
      server/middleware/auth.go

+ 25 - 13
server/config/config.go

@@ -15,9 +15,15 @@ import (
 )
 
 type AppConfig struct {
-	BaseApi     string
-	DataDir     string
-	ContextPath string
+	BaseApi              string
+	DataDir              string
+	DBDir                string
+	ContextPath          string
+	NginxPath            string
+	NginxDir             string
+	ThirdSession         bool
+	ThirdSessionName     string
+	ThirdSessionCheckUrl string
 }
 
 type CompleteOauth2Config struct {
@@ -44,20 +50,15 @@ func GetDataDir() string {
 }
 
 func init() {
+	beego.BConfig.CopyRequestBody = true
 	// 需要和前端配置好
-	baseApi := beego.AppConfig.String("baseApi")
-	if baseApi == "" {
-		baseApi = "/ngx"
-		err := beego.AppConfig.Set("baseApi", baseApi)
-		if err != nil {
-			logs.Info("init set baseApi", err)
-		}
-	}
+	baseApi := beego.AppConfig.DefaultString("baseApi", "/nginx-ui/api")
 	baseApi = strings.TrimSuffix(baseApi, "/")
-	Config.ContextPath = beego.AppConfig.DefaultString("contextpath", "")
+	Config.ContextPath = beego.AppConfig.DefaultString("contextpath", "/nginx-ui")
 	Config.ContextPath = strings.TrimSuffix(Config.ContextPath, "/")
 	Config.BaseApi = baseApi
-	Config.DataDir = beego.AppConfig.String("datadir")
+	Config.DataDir = beego.AppConfig.DefaultString("datadir", "./data")
+	Config.DBDir = beego.AppConfig.DefaultString("dbdir", "./data/db")
 	if exist := utils.IsExist(Config.DataDir); exist == false {
 		err := os.MkdirAll(Config.DataDir, 0777)
 		logs.Warn("create data dir fail", err)
@@ -66,6 +67,17 @@ func init() {
 		}
 	}
 
+	Config.NginxPath = beego.AppConfig.DefaultString("nginxPath", "/usr/sbin/nginx")
+	Config.NginxDir = beego.AppConfig.DefaultString("nginxDir", "/etc/nginx")
+
+	Config.ThirdSession = beego.AppConfig.DefaultBool("thirdsessionenable", false)
+	Config.ThirdSessionName = beego.AppConfig.DefaultString("thirdsessionname", "")
+	Config.ThirdSessionCheckUrl = beego.AppConfig.DefaultString("thirdsessioncheckurl", "")
+	if Config.ThirdSession && (len(Config.ThirdSessionName) == 0 || len(Config.ThirdSessionCheckUrl) == 0) {
+		logs.Warn("please config thirdsessionname and thirdsessioncheckurl, third session will skip!")
+		Config.ThirdSession = false
+	}
+
 	OauthConfig.ClientID = beego.AppConfig.DefaultString("oauth2_client_id", "")
 	OauthConfig.ClientSecret = beego.AppConfig.DefaultString("oauth2_client_secret", "")
 

+ 2 - 2
server/db/db.go

@@ -1,17 +1,17 @@
 package db
 
 import (
-	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/orm"
 	_ "github.com/mattn/go-sqlite3"
 	"os"
+	"server/config"
 	"server/models"
 	"server/utils"
 	"time"
 )
 
 func Init() {
-	dir := beego.AppConfig.String("dbdir")
+	dir := config.Config.DBDir
 	if !utils.IsExist(dir) {
 		os.MkdirAll(dir, 0777)
 	}

+ 1 - 1
server/go.mod

@@ -11,6 +11,7 @@ require (
 	github.com/pkg/sftp v1.13.5
 	github.com/smartystreets/goconvey v1.6.4
 	golang.org/x/crypto v0.11.0
+	golang.org/x/oauth2 v0.10.0
 )
 
 require (
@@ -39,7 +40,6 @@ require (
 	github.com/ulikunitz/xz v0.5.10 // indirect
 	go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
 	golang.org/x/net v0.12.0 // indirect
-	golang.org/x/oauth2 v0.10.0 // indirect
 	golang.org/x/sys v0.10.0 // indirect
 	golang.org/x/text v0.11.0 // indirect
 	google.golang.org/appengine v1.6.7 // indirect

+ 1 - 9
server/go.sum

@@ -94,6 +94,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
 github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
 github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
 github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
 github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -190,8 +191,6 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
 golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
-golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
-golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
 golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
 golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
 golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -236,8 +235,6 @@ golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLL
 golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
-golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
-golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
 golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
 golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -272,20 +269,15 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
-golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
 golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
-golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
 golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
 golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
-golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
 golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
 golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
 golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

+ 4 - 23
server/middleware/auth.go

@@ -23,40 +23,21 @@ var whitelist = map[string]bool{
 
 var UnauthorizedResp = `{"code": 401, "msg":"未登录或者登录已过期!"}`
 
-type ThirdSession struct {
-	Enable     bool
-	CookieName string
-	CheckUrl   string
-}
-
-var thirdSession = ThirdSession{
-	Enable:     false,
-	CookieName: "",
-	CheckUrl:   "",
-}
-
 func init() {
 	beego.BConfig.WebConfig.Session.SessionAutoSetCookie = true
-
-	thirdSession.Enable = beego.AppConfig.DefaultBool("thirdsessionenable", false)
-	thirdSession.CookieName = beego.AppConfig.DefaultString("thirdsessionname", "")
-	thirdSession.CheckUrl = beego.AppConfig.DefaultString("thirdsessioncheckurl", "")
-	if thirdSession.Enable && (len(thirdSession.CookieName) == 0 || len(thirdSession.CheckUrl) == 0) {
-		logs.Info("no thirdsessionname or thirdsessioncheckurl info,skip !")
-		thirdSession.Enable = false
-	}
 }
 
 func checkThirdSession(ctx *context.Context, sess session.Store) {
-	if !thirdSession.Enable {
+	cfg := config.Config
+	if !cfg.ThirdSession {
 		return
 	}
-	cookie, err := ctx.Request.Cookie(thirdSession.CookieName)
+	cookie, err := ctx.Request.Cookie(cfg.ThirdSessionName)
 	if err != nil {
 		logs.Warn("no cookie", err)
 		return
 	}
-	req := httplib.Get(thirdSession.CheckUrl)
+	req := httplib.Get(cfg.ThirdSessionCheckUrl)
 	req.SetEnableCookie(true)
 	req.SetCookie(cookie)
 	user := models.User{}