Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoK29 committed May 13, 2022
1 parent fef1bfd commit 5757627
Show file tree
Hide file tree
Showing 28 changed files with 625 additions and 156 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 系统设置
system:
# 服务监听地址
addr: 127.0.0.1:5678
addr: 5678
# 是否开启debug模式
debug: false
# 是否开启gzip压缩
Expand Down
29 changes: 0 additions & 29 deletions config.yml

This file was deleted.

6 changes: 3 additions & 3 deletions config/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

// System 系统设置
type System struct {
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // 服务监听地址
Debug bool `mapstructure:"debug" json:"debug" yaml:"debug"` // 是否开启debug模式
EnableGzip bool `mapstructure:"enableGzip" json:"enableGzip" yaml:"enableGzip"` // 是否开启gzip压缩
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压缩
}
36 changes: 36 additions & 0 deletions controller/base.go
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)
}
36 changes: 36 additions & 0 deletions controller/database.go
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())
}
65 changes: 65 additions & 0 deletions controller/index.go
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.ShouldBind(&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)
}
40 changes: 40 additions & 0 deletions controller/response.go
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,
})
}
21 changes: 21 additions & 0 deletions controller/services.go
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(),
}
}
14 changes: 14 additions & 0 deletions controller/word.go
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)
}
6 changes: 4 additions & 2 deletions global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package global

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

"github.com/spf13/viper"
)

var (
VP *viper.Viper // 解析器
CONFIG *config.Server // 服务器设置
VP *viper.Viper // 解析器
CONFIG *config.Server // 服务器设置
Container *searcher.Container
)
20 changes: 20 additions & 0 deletions initialize/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package initialize

import (
"gofound/global"
"gofound/searcher"
"gofound/searcher/words"
)

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

return container
}
8 changes: 8 additions & 0 deletions initialize/tokenizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package initialize

import "gofound/searcher/words"

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

0 comments on commit 5757627

Please sign in to comment.