Commit 5e92a25e by Joneq

crm服务开始

parents
Homestead.yaml
Homestead.json
# ide
.idea
*.properties
*.iml
/logs
/components
/storage/app
/storage/framework
/storage/logs
/.idea
/storage/debugbar
.DS_Store?
.DS_Store
/configs
cmd/cmd.exe
cmd/move/logs/
/cmd/dong/logs/*.log
/sh
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="NodePackageJsonFileManager">
<packageJsonPaths />
</component>
</project>
\ No newline at end of file
# Default ignored files
/workspace.xml
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" />
</settings>
</component>
\ No newline at end of file
## caipu-server
### v1.0.0
1. 上线功能xxx
# Owner
# Author
# Reviewer
# caipu-server
## 项目简介
1.GO服务DEMO
2.运行命令
go run cmd/main.go -conf=./configs/
package main
import (
"flag"
"context"
"crm-server/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log"
)
func main() {
//获取输入参数
flag.Parse()
if err := paladin.Init(); err != nil {
panic(err)
}
//初始化日志目录
log.Init(&log.Config{Dir: "logs"})
defer log.Close()
log.Info("caipu-server start")
//声明service层
svc := service.New()
ctx := context.Background()
//调用会员服务
svc.GetUserInfoByUserId(ctx,1)
}
module crm-server
go 1.12
require (
github.com/bilibili/kratos v0.0.0-20191025092737-e14170de04ba
github.com/gogo/protobuf v1.3.0
github.com/golang/protobuf v1.3.2
github.com/pkg/errors v0.8.1
golang.org/x/net v0.0.0-20191011234655-491137f69257
google.golang.org/grpc v1.24.0
)
This diff is collapsed. Click to expand it.
package dao
import (
"context"
"time"
"crm-server/internal/model"
"github.com/bilibili/kratos/pkg/cache/memcache"
"github.com/bilibili/kratos/pkg/cache/redis"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/database/sql"
"github.com/bilibili/kratos/pkg/log"
xtime "github.com/bilibili/kratos/pkg/time"
)
// Dao dao interface
type Dao interface {
Close()
Ping(ctx context.Context) (err error)
GetUserInfoByUserId(ctx context.Context,user_id int64) (r *model.Users,err error)
}
// dao dao.
type dao struct {
db *sql.DB
redis *redis.Pool
redisExpire int32
mc *memcache.Memcache
mcExpire int32
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
// New new a dao and return.
func New() (Dao) {
var (
dc struct {
Crm *sql.Config
}
rc struct {
Demo *redis.Config
DemoExpire xtime.Duration
}
mc struct {
Demo *memcache.Config
DemoExpire xtime.Duration
}
)
checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc))
checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc))
checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc))
return &dao{
// mysql
db: sql.NewMySQL(dc.Crm),
// redis
redis: redis.NewPool(rc.Demo),
redisExpire: int32(time.Duration(rc.DemoExpire) / time.Second),
// memcache
mc: memcache.New(mc.Demo),
mcExpire: int32(time.Duration(mc.DemoExpire) / time.Second),
}
}
// Close close the resource.
func (d *dao) Close() {
d.mc.Close()
d.redis.Close()
d.db.Close()
}
// Ping ping the resource.
func (d *dao) Ping(ctx context.Context) (err error) {
if err = d.pingMC(ctx); err != nil {
return
}
if err = d.pingRedis(ctx); err != nil {
return
}
return d.db.Ping(ctx)
}
func (d *dao) pingMC(ctx context.Context) (err error) {
if err = d.mc.Set(ctx, &memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
log.Error("conn.Set(PING) error(%v)", err)
}
return
}
func (d *dao) pingRedis(ctx context.Context) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
if _, err = conn.Do("SET", "ping", "pong"); err != nil {
log.Error("conn.Set(PING) error(%v)", err)
}
return
}
package dao
import (
"context"
"crm-server/internal/model"
"github.com/bilibili/kratos/pkg/database/sql"
"github.com/pkg/errors"
)
const (
_user_Info_One = "SELECT user_id, user_name FROM ys_users WHERE user_id=?"
)
//通过UID获取用户信息
func (d *dao) GetUserInfoByUserId(ctx context.Context, user_id int64) (r *model.Users, err error) {
var row *sql.Row
r = new(model.Users)
row = d.db.QueryRow(ctx, _user_Info_One, user_id)
if err = row.Scan(&r.User_id, &r.User_name); err != nil {
if err == sql.ErrNoRows {
err = nil
r = nil
return
}
err = errors.WithStack(err)
return
}
return
}
package model
// Kratos hello kratos.
type Kratos struct {
Hello string
}
\ No newline at end of file
package model
// Kratos hello kratos.
type Users struct {
User_id int64 `json:"user_id"`
User_name string `json:"user_name"`
}
\ No newline at end of file
package service
import (
"context"
"crm-server/internal/dao"
"crm-server/internal/model"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/pkg/errors"
)
// Service service.
type Service struct {
ac *paladin.Map
dao dao.Dao
}
// New new a service and return.
func New() (s *Service) {
var ac = new(paladin.TOML)
if err := paladin.Watch("application.toml", ac); err != nil {
panic(err)
}
s = &Service{
ac: ac,
dao: dao.New(),
}
return s
}
// Ping ping the resource.
func (s *Service) Ping(ctx context.Context) (err error) {
return s.dao.Ping(ctx)
}
// Close close the resource.
func (s *Service) Close() {
s.dao.Close()
}
//通过用户ID获取用户信息
func (s *Service) GetUserInfoByUserId(ctx context.Context, user_id int64) (res *model.Users, err error) {
if res, err = s.dao.GetUserInfoByUserId(ctx, user_id); err != nil {
err = errors.WithStack(err)
}
return
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment