配置从 os.Getenv获取

This commit is contained in:
suguo.yao 2022-10-30 16:38:28 +08:00
parent 335d424cc8
commit 9df3243900
8 changed files with 74 additions and 107 deletions

View File

@ -2,10 +2,11 @@ package gin
// GIN 配置
type Config struct {
RootPath string
Addr string
Port int
Ssl bool
SslPem string
SslKey string
RootPath string
Addr string
Port int
Ssl bool
SslPem string
SslKey string
EnableGzip bool
}

View File

@ -7,6 +7,7 @@ import (
"strconv"
"time"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/unrolled/secure"
)
@ -24,6 +25,9 @@ func Service(conf *Config) {
}
go func() {
router := gin.New()
if conf.EnableGzip {
router.Use(gzip.Gzip(gzip.DefaultCompression))
}
routerSetup(router, &conf.RootPath)
if conf.Ssl {

View File

@ -2,16 +2,12 @@ package global
// Config 服务器设置
type Config struct {
Addr string `yaml:"addr"` // 监听地址
Port int `yaml:"port"` //端口
Data string `json:"data"` // 数据目录
Debug bool `yaml:"debug"` // 调试模式
Dictionary string `json:"dictionary"` // 字典路径
EnableAdmin bool `yaml:"enableAdmin"` //启用admin
Gomaxprocs int `json:"gomaxprocs"` //GOMAXPROCS
Shard int `yaml:"shard"` //分片数
Auth string `json:"auth"` //认证
EnableGzip bool `yaml:"enableGzip"` //是否开启gzip压缩
Timeout int64 `json:"timeout"` //超时时间
BufferNum int `yaml:"bufferNum"` //分片缓冲数
Addr string
Port int
Data string `json:"data"` // 数据目录
Dictionary string `json:"dictionary"` // 字典路径
Gomaxprocs int `json:"gomaxprocs"` //GOMAXPROCS
Shard int `yaml:"shard"` //分片数
Timeout int64 `json:"timeout"` //超时时间
BufferNum int `yaml:"bufferNum"` //分片缓冲数
}

View File

@ -1,10 +1,42 @@
package global
import (
"myschools.me/suguo/gofound/searcher"
"os"
"runtime"
"strconv"
)
var (
CONFIG *Config // 服务器设置
Container *searcher.Container
CONFIG *Config // 服务器设置
)
func Parse() {
addr := os.Getenv("addr")
if addr == "" {
addr = "127.0.0.1"
}
port := os.Getenv("port")
if port == "" {
port = "5678"
}
p, err := strconv.Atoi(port)
if err != nil {
p = 5678
}
dataDir := os.Getenv("data") //设置数据目录
dictionaryPath := os.Getenv("dictionary") //设置词典路径
gomaxprocs := runtime.NumCPU() * 2 //设置GOMAXPROCS
timeout := int64(10 * 60) //数据库超时关闭时间(秒)
bufferNum := 1000 //分片缓冲数量
CONFIG = &Config{
Addr: addr,
Port: p,
Data: dataDir,
Dictionary: dictionaryPath,
Gomaxprocs: gomaxprocs,
Timeout: timeout,
BufferNum: bufferNum,
}
}

20
go.mod
View File

@ -5,7 +5,8 @@ go 1.18
require (
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/emirpasic/gods v1.12.0
github.com/gin-gonic/gin v1.7.7
github.com/gin-contrib/gzip v0.0.6
github.com/gin-gonic/gin v1.8.1
github.com/go-ego/gse v0.70.2
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46
github.com/shirou/gopsutil/v3 v3.22.4
@ -18,29 +19,28 @@ require (
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/vcaesar/cedar v0.20.1 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

78
main.go
View File

@ -1,22 +1,19 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"syscall"
"gopkg.in/yaml.v2"
"myschools.me/suguo/gofound/gin"
"myschools.me/suguo/gofound/global"
"myschools.me/suguo/gofound/service"
)
func main() {
global.CONFIG = parser()
global.Parse()
defer func() {
if r := recover(); r != nil {
@ -29,12 +26,13 @@ func main() {
service.ContainerInit(tokenizer)
gin.Service(&gin.Config{
RootPath: "/api",
Addr: global.CONFIG.Addr,
Port: global.CONFIG.Port,
Ssl: false,
SslPem: "",
SslKey: "",
RootPath: "/api",
Addr: global.CONFIG.Addr,
Port: global.CONFIG.Port,
Ssl: false,
SslPem: "",
SslKey: "",
EnableGzip: true,
})
// 优雅关机
@ -43,63 +41,3 @@ func main() {
<-quit
log.Println("Shutdown Server ...")
}
// Parser 解析器
func parser() *global.Config {
var addr = flag.String("addr", "127.0.0.1", "设置监听地址和端口")
var port = flag.Int("port", 5678, "port")
//兼容windows
dir := fmt.Sprintf(".%sdata", string(os.PathSeparator))
var dataDir = flag.String("data", dir, "设置数据存储目录")
var debug = flag.Bool("debug", true, "设置是否开启调试模式")
var dictionaryPath = flag.String("dictionary", "", "设置词典路径")
var enableAdmin = flag.Bool("enableAdmin", true, "设置是否开启后台管理")
var gomaxprocs = flag.Int("gomaxprocs", runtime.NumCPU()*2, "设置GOMAXPROCS")
var auth = flag.String("auth", "", "开启认证,例如: admin:123456")
var enableGzip = flag.Bool("enableGzip", true, "是否开启gzip压缩")
var timeout = flag.Int64("timeout", 10*60, "数据库超时关闭时间(秒)")
var bufferNum = flag.Int("bufferNum", 1000, "分片缓冲数量")
var configPath = flag.String("config", "", "配置文件路径,配置此项其他参数忽略")
flag.Parse()
config := &global.Config{}
if *configPath != "" {
//解析配置文件
//file, err := ioutil.ReadFile(*configPath)
file, err := os.ReadFile(*configPath) //详情https://github.com/golang/go/issues/42026
if err != nil {
panic(err)
}
err = yaml.Unmarshal(file, config)
if err != nil {
panic(err)
}
return config
}
config = &global.Config{
Addr: *addr,
Port: *port,
Data: *dataDir,
Debug: *debug,
Dictionary: *dictionaryPath,
EnableAdmin: *enableAdmin,
Gomaxprocs: *gomaxprocs,
Auth: *auth,
EnableGzip: *enableGzip,
Timeout: *timeout,
BufferNum: *bufferNum,
}
return config
}

View File

@ -12,7 +12,6 @@ var container *searcher.Container
func ContainerInit(tokenizer *words.Tokenizer) {
container = &searcher.Container{
Dir: global.CONFIG.Data,
Debug: global.CONFIG.Debug,
Tokenizer: tokenizer,
Shard: global.CONFIG.Shard,
Timeout: global.CONFIG.Timeout,

View File

@ -38,16 +38,13 @@ func serverInfo() map[string]interface{} {
"dataPath": global.CONFIG.Data,
"dictionaryPath": global.CONFIG.Dictionary,
"gomaxprocs": runtime.NumCPU() * 2,
"debug": global.CONFIG.Debug,
"shard": global.CONFIG.Shard,
"dataSize": system.GetFloat64MB(utils.DirSizeB(global.CONFIG.Data)),
"executable": os.Args[0],
"dbs": container.GetDataBaseNumber(),
//"indexCount": global.container.GetIndexCount(),
//"documentCount": global.container.GetDocumentCount(),
"pid": os.Getpid(),
"enableAuth": global.CONFIG.Auth != "",
"enableGzip": global.CONFIG.EnableGzip,
"bufferNum": global.CONFIG.BufferNum,
"pid": os.Getpid(),
"bufferNum": global.CONFIG.BufferNum,
}
}