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#8 from XiaoK29/dev
修改config
- Loading branch information
Showing
36 changed files
with
1,313 additions
and
683 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 |
---|---|---|
|
@@ -8,4 +8,5 @@ gofound | |
/*/*.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 系统设置 | ||
system: | ||
# 服务监听地址 | ||
addr: 5678 | ||
# 是否开启debug模式 | ||
debug: false | ||
# 是否开启gzip压缩 | ||
enableGzip: false | ||
|
||
# 是否开启认证 | ||
auth: | ||
enable: false | ||
# 认证用户名 | ||
username: admin | ||
# 认证密码 | ||
password: 123456 | ||
|
||
# 搜索引擎部分 | ||
engine: | ||
# 数据存放路径 | ||
dataDir: ./data | ||
# 词库路径 | ||
dictionaryDir: ./searcher/words/data/dictionary.txt | ||
# 文件分块 | ||
shard: 5 | ||
# 索引队列最大值 | ||
queueMax: 1000 | ||
# GC时间间隔 | ||
gcInterval: 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,8 @@ | ||
package config | ||
|
||
// Auth 权限设置 | ||
type Auth struct { | ||
Enable bool `mapstructure:"enable" json:"enable" yaml:"enable"` // 是否开启 | ||
Username string `mapstructure:"username" json:"username" yaml:"username"` // 用户名 | ||
Password string `mapstructure:"password" json:"password" yaml:"password"` // 用户密码 | ||
} |
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,8 @@ | ||
package config | ||
|
||
// Server 服务器设置 | ||
type Server struct { | ||
System *System `mapstructure:"system" json:"system" yaml:"system"` // 系统设置 | ||
Auth *Auth `mapstructure:"auth" json:"auth" yaml:"auth"` // 认证设置 | ||
Engine *Engine `mapstructure:"engine" json:"engine" yaml:"engine"` // 搜索引擎设置 | ||
} |
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 config | ||
|
||
// Engine 搜索引擎 | ||
type Engine struct { | ||
DataDir string `mapstructure:"dataDir" json:"dataDir" yaml:"dataDir"` // 数据存放路径 | ||
DictionaryDir string `mapstructure:"dictionaryDir" json:"dictionaryDir" yaml:"dictionaryDir"` // 词库路径 | ||
Shard int `mapstructure:"shard" json:"shard" yaml:"shard"` // 文件分块 | ||
QueueMax int `mapstructure:"queue_max" json:"queueMax" yaml:"queueMax"` // 索引队列最大值 | ||
GcInterval int `mapstructure:"gcInterval" json:"gcInterval" yaml:"gcInterval"` // GC时间间隔 | ||
} |
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,8 @@ | ||
package config | ||
|
||
// System 系统设置 | ||
type System struct { | ||
Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务监听地址 | ||
Debug bool `mapstructure:"debug" json:"debug" yaml:"debug"` // 是否开启debug模式 | ||
EnableGzip bool `mapstructure:"enableGzip" json:"enableGzip" yaml:"enableGzip"` // 是否开启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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controller | ||
|
||
import ( | ||
"gofound/searcher/model" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func Welcome(c *gin.Context) { | ||
ResponseSuccessWithData(c, "Welcome to GoFound") | ||
} | ||
|
||
// Query 查询 | ||
func Query(c *gin.Context) { | ||
var request = &model.SearchRequest{} | ||
if err := c.ShouldBind(&request); err != nil { | ||
ResponseErrorWithMsg(c, err.Error()) | ||
return | ||
} | ||
|
||
//调用搜索 | ||
r := srv.Base.Query(request) | ||
ResponseSuccessWithData(c, r) | ||
} | ||
|
||
// GC 释放GC | ||
func GC(c *gin.Context) { | ||
srv.Base.GC() | ||
ResponseSuccess(c) | ||
} | ||
|
||
// Status 获取服务器状态 | ||
func Status(c *gin.Context) { | ||
r := srv.Base.Status() | ||
ResponseSuccessWithData(c, r) | ||
} |
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,36 @@ | ||
package controller | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
// DatabaseDrop 删除数据库 | ||
func DatabaseDrop(c *gin.Context) { | ||
dbName := c.Query("database") | ||
if dbName == "" { | ||
ResponseErrorWithMsg(c, "database is empty") | ||
return | ||
} | ||
|
||
if err := srv.Database.Drop(dbName); err != nil { | ||
ResponseErrorWithMsg(c, err.Error()) | ||
return | ||
} | ||
|
||
ResponseSuccessWithData(c, "删除成功") | ||
} | ||
|
||
// DatabaseCreate 创建数据库 | ||
func DatabaseCreate(c *gin.Context) { | ||
dbName := c.Query("database") | ||
if dbName == "" { | ||
ResponseErrorWithMsg(c, "database is empty") | ||
return | ||
} | ||
|
||
srv.Database.Create(dbName) | ||
ResponseSuccessWithData(c, "创建成功") | ||
} | ||
|
||
// DBS 查询数据库 | ||
func DBS(c *gin.Context) { | ||
ResponseSuccessWithData(c, srv.Database.Show()) | ||
} |
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 controller | ||
|
||
import ( | ||
"gofound/searcher/model" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// AddIndex 添加索引 | ||
func AddIndex(c *gin.Context) { | ||
document := &model.IndexDoc{} | ||
if err := c.ShouldBindJSON(&document); err != nil { | ||
ResponseErrorWithMsg(c, err.Error()) | ||
return | ||
} | ||
dbName := c.Query("database") | ||
if dbName == "" { | ||
ResponseErrorWithMsg(c, "database is empty") | ||
return | ||
} | ||
srv.Index.AddIndex(dbName, document) | ||
|
||
ResponseSuccessWithData(c, nil) | ||
} | ||
|
||
// BatchAddIndex 批量添加索引 | ||
func BatchAddIndex(c *gin.Context) { | ||
documents := make([]model.IndexDoc, 0) | ||
if err := c.BindJSON(&documents); err != nil { | ||
ResponseErrorWithMsg(c, err.Error()) | ||
return | ||
} | ||
|
||
dbName := c.Query("database") | ||
if dbName == "" { | ||
ResponseErrorWithMsg(c, "database is empty") | ||
return | ||
} | ||
|
||
srv.Index.BatchAddIndex(dbName, documents) | ||
|
||
ResponseSuccess(c) | ||
} | ||
|
||
// RemoveIndex 删除索引 | ||
func RemoveIndex(c *gin.Context) { | ||
removeIndexModel := &model.RemoveIndexModel{} | ||
if err := c.BindJSON(&removeIndexModel); err != nil { | ||
ResponseErrorWithMsg(c, err.Error()) | ||
return | ||
} | ||
|
||
dbName := c.Query("database") | ||
if dbName == "" { | ||
ResponseErrorWithMsg(c, "database is empty") | ||
return | ||
} | ||
|
||
if err := srv.Index.RemoveIndex(dbName, removeIndexModel); err != nil { | ||
ResponseErrorWithMsg(c, err.Error()) | ||
return | ||
} | ||
|
||
ResponseSuccess(c) | ||
} |
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,40 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type ResponseData struct { | ||
State bool `json:"state"` | ||
Message string `json:"message,omitempty"` | ||
Data interface{} `json:"data,omitempty"` | ||
} | ||
|
||
// ResponseSuccessWithData 携带数据成功返回 | ||
func ResponseSuccessWithData(c *gin.Context, data interface{}) { | ||
c.JSON(http.StatusOK, &ResponseData{ | ||
State: true, | ||
Message: "success", | ||
Data: data, | ||
}) | ||
} | ||
|
||
// ResponseErrorWithMsg 返回错误 | ||
func ResponseErrorWithMsg(c *gin.Context, message string) { | ||
c.JSON(http.StatusOK, &ResponseData{ | ||
State: false, | ||
Message: message, | ||
Data: nil, | ||
}) | ||
} | ||
|
||
// ResponseSuccess 返回成功 | ||
func ResponseSuccess(c *gin.Context) { | ||
c.JSON(http.StatusOK, &ResponseData{ | ||
State: true, | ||
Message: "success", | ||
Data: nil, | ||
}) | ||
} |
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,21 @@ | ||
package controller | ||
|
||
import "gofound/service" | ||
|
||
var srv *Services | ||
|
||
type Services struct { | ||
Base *service.Base | ||
Index *service.Index | ||
Database *service.Database | ||
Word *service.Word | ||
} | ||
|
||
func NewServices() { | ||
srv = &Services{ | ||
Base: service.NewBase(), | ||
Index: service.NewIndex(), | ||
Database: service.NewDatabase(), | ||
Word: service.NewWord(), | ||
} | ||
} |
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 controller | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
// WordCut 分词 | ||
func WordCut(c *gin.Context) { | ||
q := c.Query("q") | ||
if q == "" { | ||
ResponseErrorWithMsg(c, "请输入关键字") | ||
return | ||
} | ||
r := srv.Word.WordCut(q) | ||
ResponseSuccessWithData(c, r) | ||
} |
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,23 @@ | ||
package core | ||
|
||
import ( | ||
"gofound/global" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
// Viper 解析器 | ||
func Viper(config string) *viper.Viper { | ||
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 | ||
} |
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 | ||
|
||
import ( | ||
"gofound/config" | ||
"gofound/searcher" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
VP *viper.Viper // 解析器 | ||
CONFIG *config.Server // 服务器设置 | ||
Container *searcher.Container | ||
) |
Oops, something went wrong.