Skip to content

Commit

Permalink
优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
newpanjing committed May 16, 2022
1 parent 4b3c3b3 commit 20c83b8
Show file tree
Hide file tree
Showing 49 changed files with 2,084 additions and 430 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ gofound
/go.sum
/.idea/
/*/*.bin
/dist/
/cache
/tests/index
/data
8 changes: 0 additions & 8 deletions config/auth.go

This file was deleted.

8 changes: 0 additions & 8 deletions config/config.go

This file was deleted.

10 changes: 0 additions & 10 deletions config/engine.go

This file was deleted.

8 changes: 0 additions & 8 deletions config/system.go

This file was deleted.

21 changes: 0 additions & 21 deletions controller/services.go

This file was deleted.

82 changes: 82 additions & 0 deletions core/initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package core

import (
"context"
"fmt"
"gofound/global"
"gofound/searcher"
"gofound/searcher/words"
"gofound/web/controller"
"gofound/web/router"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

func NewContainer(tokenizer *words.Tokenizer) *searcher.Container {
container := &searcher.Container{
Dir: global.CONFIG.DataDir,
Debug: global.CONFIG.Debug,
Tokenizer: tokenizer,
Shard: global.CONFIG.Shard,
}
go container.Init()

return container
}

func NewTokenizer(dictionaryPath string) *words.Tokenizer {
return words.NewTokenizer(dictionaryPath)
}

// Initialize 初始化
func Initialize() {

global.CONFIG = Parser()

defer func() {

if r := recover(); r != nil {
fmt.Printf("panic: %s\n", r)
}
}()

//初始化分词器
tokenizer := NewTokenizer(global.CONFIG.DictionaryDir)
global.Container = NewContainer(tokenizer)

// 初始化业务逻辑
controller.NewServices()

// 注册路由
r := router.SetupRouter()
// 启动服务
srv := &http.Server{
Addr: global.CONFIG.Addr,
Handler: r,
}
go func() {
// 开启一个goroutine启动服务
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println("listen:", err)
}
}()

// 优雅关机
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
log.Println("Server Shutdown:", err)
}

log.Println("Server exiting")
}
65 changes: 65 additions & 0 deletions core/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package core

import (
"flag"
"fmt"
"gofound/global"
"os"
"runtime"
//"github.com/spf13/viper"
)

// Parser 解析器
func Parser() *global.Config {
//v := viper.New()
//v.SetConfigFile(config)
//err := v.ReadInConfig()
//if err != nil {
// panic(err)
//}
//
//if err := v.Unmarshal(&global.CONFIG); err != nil {
// panic(err)
//}
//
//return v
var configPath = flag.String("config", "", "配置文件路径,配置此项其他参数忽略")
if *configPath != "" {
//解析配置文件
return nil
}

var addr = flag.String("addr", "0.0.0.0:5678", "设置监听地址和端口")
//兼容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", "./data/dictionary.txt", "设置词典路径")

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压缩")

flag.Parse()

config := &global.Config{
Addr: *addr,
DataDir: *dataDir,
Debug: *debug,
DictionaryDir: *dictionaryPath,
EnableAdmin: *enableAdmin,
Gomaxprocs: *gomaxprocs,
Auth: *auth,
EnableGzip: *enableGzip,
}
fmt.Println(config)

return config
}
23 changes: 0 additions & 23 deletions core/viper.go

This file was deleted.

14 changes: 14 additions & 0 deletions global/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package global

// Config 服务器设置
type Config struct {
Addr string // 监听地址
DataDir string // 数据目录
Debug bool // 调试模式
DictionaryDir string // 字典路径
EnableAdmin bool //启用admin
Gomaxprocs int //GOMAXPROCS
Shard int //分片数
Auth string //认证
EnableGzip bool //是否开启gzip压缩
}
6 changes: 1 addition & 5 deletions global/global.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package global

import (
"gofound/config"
"gofound/searcher"

"github.com/spf13/viper"
)

var (
VP *viper.Viper // 解析器
CONFIG *config.Server // 服务器设置
CONFIG *Config // 服务器设置
Container *searcher.Container
)
18 changes: 4 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,36 @@ require (
github.com/gin-gonic/gin v1.7.7
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46
github.com/shirou/gopsutil/v3 v3.22.4
github.com/spf13/viper v1.11.0
github.com/syndtr/goleveldb v1.0.0
github.com/wangbin/jiebago v0.3.2
)

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
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/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // 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 v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // 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/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/ini.v1 v1.66.4 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
20 changes: 0 additions & 20 deletions initialize/container.go

This file was deleted.

8 changes: 0 additions & 8 deletions initialize/tokenizer.go

This file was deleted.

Loading

0 comments on commit 20c83b8

Please sign in to comment.