forked from sea-team/gofound
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sea-team#11 from newpanjing/dev
Dev
- Loading branch information
Showing
49 changed files
with
3,299 additions
and
979 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ gofound | |
/go.sum | ||
/.idea/ | ||
/*/*.bin | ||
/dist/ | ||
/cache | ||
/tests/index | ||
/tests/index | ||
/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,6 @@ | |
|
||
## 技术栈 | ||
|
||
+ 平衡二叉查找树 | ||
+ 二分法查找 | ||
+ 快速排序法 | ||
+ 倒排索引 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#监听地址 | ||
addr: 0.0.0.0:5678 | ||
|
||
#数据目录 | ||
data: ./data | ||
#词典目录 | ||
dictionary: ./data/dictionary.txt | ||
|
||
#是否启用admin | ||
enableAdmin: true | ||
|
||
# 最大线程数 | ||
gomaxprocs: 4 | ||
|
||
# admin 用户名和密码 | ||
auth: admin:123456 | ||
|
||
# 接口是否开启压缩 | ||
enableGzip: true | ||
|
||
# 数据库关闭超时时间 | ||
timeout: 600 | ||
|
||
# 分片数量 | ||
shard: 10 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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.Data, | ||
Debug: global.CONFIG.Debug, | ||
Tokenizer: tokenizer, | ||
Shard: global.CONFIG.Shard, | ||
Timeout: global.CONFIG.Timeout, | ||
} | ||
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.Dictionary) | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package core | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"gofound/global" | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"os" | ||
"runtime" | ||
) | ||
|
||
// Parser 解析器 | ||
func Parser() *global.Config { | ||
|
||
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压缩") | ||
var timeout = flag.Int64("timeout", 10*60, "数据库超时关闭时间(秒)") | ||
|
||
var configPath = flag.String("config", "", "配置文件路径,配置此项其他参数忽略") | ||
|
||
flag.Parse() | ||
|
||
config := &global.Config{} | ||
|
||
if *configPath != "" { | ||
//解析配置文件 | ||
file, err := ioutil.ReadFile(*configPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = yaml.Unmarshal(file, config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return config | ||
} | ||
config = &global.Config{ | ||
Addr: *addr, | ||
Data: *dataDir, | ||
Debug: *debug, | ||
Dictionary: *dictionaryPath, | ||
EnableAdmin: *enableAdmin, | ||
Gomaxprocs: *gomaxprocs, | ||
Auth: *auth, | ||
EnableGzip: *enableGzip, | ||
Timeout: *timeout, | ||
} | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package global | ||
|
||
// Config 服务器设置 | ||
type Config struct { | ||
Addr string `yaml:"addr"` // 监听地址 | ||
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"` //超时时间 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package global | ||
|
||
import ( | ||
"gofound/searcher" | ||
) | ||
|
||
var ( | ||
CONFIG *Config // 服务器设置 | ||
Container *searcher.Container | ||
) |
Oops, something went wrong.