Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ebe0a3af
authored
Jun 08, 2020
by
mushishixian
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
整合mgo操作
parent
90466744
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
83 additions
and
15 deletions
boot/app.go
cmd/course_http_server.go
conf/config.ini
go.mod
pkg/config/config.go
pkg/mongo/mongo.go
readme.md
test/test.go
boot/app.go
View file @
ebe0a3af
...
...
@@ -3,11 +3,12 @@ package boot
import
(
"search_server/pkg/config"
"search_server/pkg/gredis"
"search_server/pkg/mongo"
"search_server/pkg/mysql"
)
func
Boot
()
(
err
error
)
{
if
err
=
config
.
SetUp
();
err
!=
nil
{
func
Boot
(
configPath
string
)
(
err
error
)
{
if
err
=
config
.
SetUp
(
configPath
);
err
!=
nil
{
panic
(
err
)
}
if
err
=
mysql
.
Setup
();
err
!=
nil
{
...
...
@@ -16,5 +17,8 @@ func Boot() (err error) {
if
err
=
gredis
.
Setup
();
err
!=
nil
{
return
}
if
err
=
mongo
.
SetUp
();
err
!=
nil
{
return
}
return
}
cmd/course_http_server.go
View file @
ebe0a3af
package
main
import
(
"flag"
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/web"
"log"
"search_server/boot"
"search_server/framework/gin_"
_
"search_server/controller"
"
log
"
"
search_server/framework/gin_
"
)
func
main
()
{
if
err
:=
boot
.
Boot
();
err
!=
nil
{
var
path
,
temp
string
flag
.
StringVar
(
&
path
,
"config"
,
"../conf/config.ini"
,
"配置文件"
)
flag
.
StringVar
(
&
temp
,
"server_address"
,
"conf/config.ini"
,
"配置文件"
)
flag
.
Parse
()
if
err
:=
boot
.
Boot
(
path
);
err
!=
nil
{
log
.
Println
(
err
)
}
...
...
conf/config.ini
View file @
ebe0a3af
...
...
@@ -16,4 +16,5 @@ max_active = 10
idle_timeout
=
20
[mongo]
host
=
\ No newline at end of file
host
=
127.0.0.1:27017
database
=
liexin_config
\ No newline at end of file
go.mod
View file @
ebe0a3af
...
...
@@ -18,8 +18,8 @@ require (
google.golang.org/appengine v1.6.3 // indirect
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece // indirect
google.golang.org/grpc v1.29.1 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
)
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
pkg/config/config.go
View file @
ebe0a3af
...
...
@@ -9,8 +9,8 @@ var (
cfg
*
ini
.
File
)
func
SetUp
()
(
err
error
)
{
cfg
,
err
=
ini
.
Load
(
"../conf/config.ini"
)
func
SetUp
(
path
string
)
(
err
error
)
{
cfg
,
err
=
ini
.
Load
(
path
)
return
}
...
...
pkg/mongo/mongo.go
0 → 100644
View file @
ebe0a3af
package
mongo
import
(
"gopkg.in/mgo.v2"
"search_server/pkg/config"
)
var
session
*
mgo
.
Session
func
SetUp
()
(
err
error
)
{
url
:=
config
.
Get
(
"mongo.url"
)
.
String
()
session
,
err
=
mgo
.
Dial
(
url
)
if
err
!=
nil
{
return
err
}
return
}
func
GetDB
()
*
mgo
.
Database
{
database
:=
config
.
Get
(
"mongo.database"
)
.
String
()
db
:=
session
.
DB
(
database
)
return
db
}
readme.md
View file @
ebe0a3af
## 搜索服务
#### 目录结构
### 项目启动
直接到cmd下面
### 目录结构
├── bat //运行脚本或者protoc生成脚本
├── boot //启动文件,比如配置加载,数据库加载等等
├── cmd //服务的主入口,可直接go run执行
├── conf //配置文件目录
├── framework //gin框架接入
├──
lib
├──
mapper
├──
controller //顾名思义
├──
dao //数据访问层
├── model //模型目录
├── pkg //公用包目录,比如配置读取,获取数据库实例,通用函数或者变量等都可以从这里获取
├── protopb //生成的protos.pb.go文件
├── protos //原始的protos文件
├── service //业务逻辑操作所在目录
### 已经封装的模块以及用法
#### 1.配置读取
接在conf/config.ini文件添加自己的配置项
然后使用config.Get("xxx.xxx")即可
#### 2.mysql连接实例
使用mysql.GetDB()即可获取对应的实例,不同数据库的连接可以自己定义不同的GetDB方法
#### 3.redis使用
可以直接调用gredis.Hset(),gredis.Hget()等方法,具体使用请参考代码,其中返回的结果是一个redis对象,需要自己转成自己需要的类型
#### 4.mgo连接实例
使用mongo.GetDB()即可,不同数据库的连接可以自己定义不同的GetDB方法
然后用C方法指定集合进行操作即可:
mongo.GetDB().C("xxx").Find(bson.M{}).Count()
test/test.go
0 → 100644
View file @
ebe0a3af
package
main
import
(
"flag"
"fmt"
"gopkg.in/mgo.v2/bson"
"log"
"search_server/boot"
"search_server/pkg/mongo"
)
func
main
()
{
var
path
string
flag
.
StringVar
(
&
path
,
"config"
,
"conf/config.ini"
,
"配置文件"
)
flag
.
Parse
()
if
err
:=
boot
.
Boot
(
path
);
err
!=
nil
{
log
.
Println
(
err
)
}
count
,
_
:=
mongo
.
GetDB
()
.
C
(
"lie_configs"
)
.
Find
(
bson
.
M
{})
.
Count
()
fmt
.
Println
(
count
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment