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.
- Loading branch information
1 parent
4b3c3b3
commit 20c83b8
Showing
49 changed files
with
2,084 additions
and
430 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,7 +6,6 @@ gofound | |
/go.sum | ||
/.idea/ | ||
/*/*.bin | ||
/dist/ | ||
/cache | ||
/tests/index | ||
/data |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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") | ||
} |
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,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 | ||
} |
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,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压缩 | ||
} |
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 |
---|---|---|
@@ -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 | ||
) |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.