-
Notifications
You must be signed in to change notification settings - Fork 20
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 #1 from sixiaobai/master
odin first version
- Loading branch information
Showing
27 changed files
with
1,304 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
.PHONY: build | ||
|
||
SERVICE := odin | ||
MAIN := cmd/odin | ||
#REGISTRY := etcd | ||
REGISTRY := zookeeper | ||
AUTHOR := $(shell git log --pretty=format:"%an"|head -n 1) | ||
VERSION := $(shell git rev-list HEAD | head -1) | ||
BUILD_INFO := $(shell git log --pretty=format:"%s" | head -1) | ||
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M:%S) | ||
CUR_PWD := $(shell pwd) | ||
export GO111MODULE=on | ||
export GOPROXY=http://goproxy.xesv5.com | ||
|
||
LD_FLAGS='-X "$(SERVICE)/version.TAG=$(TAG)" -X "$(SERVICE)/version.VERSION=$(VERSION)" -X "$(SERVICE)/version.AUTHOR=$(AUTHOR)" -X "$(SERVICE)/version.BUILD_INFO=$(BUILD_INFO)" -X "$(SERVICE)/version.BUILD_DATE=$(BUILD_DATE)"' | ||
|
||
default: build | ||
|
||
build: | ||
go build -tags "$(REGISTRY)" -ldflags $(LD_FLAGS) -gcflags "-N" -i -o ./bin/$(SERVICE) ./$(MAIN) | ||
dev: build | ||
cp $(CUR_PWD)/conf/conf_dev.ini $(CUR_PWD)/conf/conf.ini && ./bin/$(SERVICE) -v=true | ||
clean: | ||
rm bin/* |
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 +1,48 @@ | ||
# odin | ||
# odin | ||
|
||
## directory | ||
|
||
``` | ||
├── app | ||
│ ├── common | ||
│ │ ├── Const.go | ||
│ │ ├── util.go | ||
│ │ └── var.go | ||
│ ├── entity | ||
│ │ └── user.go | ||
│ ├── repository | ||
│ │ ├── memoryDao | ||
│ │ │ └── user.go | ||
│ │ ├── pikaDao | ||
│ │ │ └── user.go | ||
│ │ ├── redisDao | ||
│ │ │ └── user.go | ||
│ │ └── repository.go | ||
│ ├── service | ||
│ │ ├── serviceBridge.go | ||
│ │ └── service.go | ||
│ ├── serviceImpl | ||
│ │ ├── HelloService.go | ||
│ │ └── UserService.go | ||
│ ├── serviceInit.go | ||
│ └── serviceInterface | ||
│ └── interface.go | ||
├── bin | ||
│ └── odin | ||
├── cmd | ||
│ └── odin | ||
│ └── main.go | ||
├── conf | ||
│ ├── conf_dev.ini | ||
│ ├── conf.ini | ||
│ ├── conf_release.ini | ||
│ └── conf_online.ini | ||
├── examples | ||
│ └── main.go | ||
├── go.mod | ||
├── Makefile | ||
├── README.md | ||
└── version | ||
└── version.go | ||
``` | ||
|
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,6 @@ | ||
package common | ||
|
||
//daoType | ||
const PIKA = "pika" | ||
const REDIS = "redis" | ||
const MEMORY = "memory" |
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,3 @@ | ||
package common | ||
|
||
var CurrentUserID int = 1 |
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,16 @@ | ||
package entity | ||
|
||
import "context" | ||
|
||
type User struct { | ||
Id int | ||
Name string | ||
Age int | ||
City string | ||
} | ||
|
||
type UserRepo interface { | ||
AddUser(context.Context, *User) (int, error) | ||
GetUserInfo(ctx context.Context, id int) (*User, error) | ||
UpdateUser(context.Context, *User) error | ||
} |
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,46 @@ | ||
package memoryRepo | ||
|
||
import ( | ||
"context" | ||
"odin/app/common" | ||
"odin/app/entity" | ||
"sync" | ||
) | ||
|
||
type UserRepo struct { | ||
store map[int]*entity.User | ||
} | ||
|
||
var once sync.Once | ||
|
||
var repo *UserRepo | ||
|
||
func NewUserRepo() *UserRepo { | ||
if repo != nil { | ||
return repo | ||
} | ||
once.Do(func() { | ||
this := new(UserRepo) | ||
this.store = make(map[int]*entity.User) | ||
repo = this | ||
}) | ||
return repo | ||
} | ||
|
||
func (this *UserRepo) AddUser(ctx context.Context, user *entity.User) (id int, err error) { | ||
common.CurrentUserID += 1 | ||
user.Id = common.CurrentUserID | ||
this.store[user.Id] = user | ||
id = user.Id | ||
return | ||
} | ||
|
||
func (this *UserRepo) GetUserInfo(ctx context.Context, id int) (user *entity.User, err error) { | ||
user = this.store[id] | ||
return | ||
} | ||
|
||
func (this *UserRepo) UpdateUser(ctx context.Context, user *entity.User) (err error) { | ||
this.store[user.Id] = user | ||
return | ||
} |
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,60 @@ | ||
package mysqlRepo | ||
|
||
import ( | ||
"time" | ||
|
||
dbdao "github.com/tal-tech/torm" | ||
) | ||
|
||
type Student struct { | ||
Id int `xorm:"not null pk autoincr INT(11)"` | ||
Sex int `xorm:"not null default 0 TINYINT(4)"` | ||
Name string `xorm:"not null default '' VARCHAR(256)"` | ||
City string `xorm:"not null default '' CHAR(64)"` | ||
CreateTime time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' comment('插入时间') TIMESTAMP"` | ||
UpdateTime time.Time `xorm:"not null default '0000-00-00 00:00:00' comment('更新时间') TIMESTAMP"` | ||
} | ||
|
||
type StudentDao struct { | ||
dbdao.DbBaseDao | ||
} | ||
|
||
func NewStudentDao(v ...interface{}) *StudentDao { | ||
this := new(StudentDao) | ||
if ins := dbdao.GetDbInstance("godemo", "writer"); ins != nil { | ||
this.UpdateEngine(ins.Engine) | ||
} else { | ||
return nil | ||
} | ||
if len(v) != 0 { | ||
this.UpdateEngine(v...) | ||
} | ||
return this | ||
} | ||
|
||
func (this *StudentDao) Get(mId dbdao.Param) (ret []Student, err error) { | ||
ret = make([]Student, 0) | ||
this.InitSession() | ||
|
||
this.BuildQuery(mId, "id") | ||
|
||
err = this.Session.Find(&ret) | ||
return | ||
} | ||
func (this *StudentDao) GetLimit(mId dbdao.Param, pn, rn int) (ret []Student, err error) { | ||
ret = make([]Student, 0) | ||
this.InitSession() | ||
|
||
this.BuildQuery(mId, "id") | ||
|
||
err = this.Session.Limit(rn, pn).Find(&ret) | ||
return | ||
} | ||
func (this *StudentDao) GetCount(mId dbdao.Param) (ret int64, err error) { | ||
this.InitSession() | ||
|
||
this.BuildQuery(mId, "id") | ||
|
||
ret, err = this.Session.Count(new(Student)) | ||
return | ||
} |
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,57 @@ | ||
package pikaRepo | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"odin/app/common" | ||
"odin/app/entity" | ||
"sync" | ||
|
||
redisdao "github.com/tal-tech/xredis" | ||
) | ||
|
||
type UserRepo struct { | ||
} | ||
|
||
var once sync.Once | ||
|
||
var repo *UserRepo | ||
|
||
func NewUserRepo() *UserRepo { | ||
if repo != nil { | ||
return repo | ||
} | ||
once.Do(func() { | ||
repo = new(UserRepo) | ||
}) | ||
return repo | ||
} | ||
|
||
const PIKA_USER_PREFIX = "micro_pika_user_%v" | ||
|
||
func (this *UserRepo) AddUser(ctx context.Context, user *entity.User) (id int, err error) { | ||
common.CurrentUserID += 1 | ||
user.Id = common.CurrentUserID | ||
bytes, _ := json.Marshal(user) | ||
xesRedis := redisdao.NewSimpleXesRedis(ctx, "pika") | ||
_, err = xesRedis.Set(PIKA_USER_PREFIX, []interface{}{user.Id}, string(bytes), 0) | ||
return | ||
} | ||
|
||
func (this *UserRepo) GetUserInfo(ctx context.Context, id int) (user *entity.User, err error) { | ||
xesRedis := redisdao.NewSimpleXesRedis(ctx, "pika") | ||
ret, err := xesRedis.Get(PIKA_USER_PREFIX, []interface{}{id}) | ||
if err != nil { | ||
return | ||
} | ||
user = new(entity.User) | ||
json.Unmarshal([]byte(ret), user) | ||
return | ||
} | ||
|
||
func (this *UserRepo) UpdateUser(ctx context.Context, user *entity.User) (err error) { | ||
bytes, _ := json.Marshal(user) | ||
xesRedis := redisdao.NewSimpleXesRedis(ctx, "pika") | ||
_, err = xesRedis.Set(PIKA_USER_PREFIX, []interface{}{user.Id}, string(bytes), 0) | ||
return | ||
} |
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,57 @@ | ||
package redisRepo | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"odin/app/common" | ||
"odin/app/entity" | ||
"sync" | ||
|
||
redisdao "github.com/tal-tech/xredis" | ||
) | ||
|
||
type UserRepo struct { | ||
} | ||
|
||
var once sync.Once | ||
|
||
var repo *UserRepo | ||
|
||
func NewUserRepo() *UserRepo { | ||
if repo != nil { | ||
return repo | ||
} | ||
once.Do(func() { | ||
repo = new(UserRepo) | ||
}) | ||
return repo | ||
} | ||
|
||
const REDIS_USER_PREFIX = "micro_redis_user_%v" | ||
|
||
func (this *UserRepo) AddUser(ctx context.Context, user *entity.User) (id int, err error) { | ||
common.CurrentUserID += 1 | ||
user.Id = common.CurrentUserID | ||
bytes, _ := json.Marshal(user) | ||
xesRedis := redisdao.NewSimpleXesRedis(ctx, "redis") | ||
_, err = xesRedis.Set(REDIS_USER_PREFIX, []interface{}{user.Id}, string(bytes), 0) | ||
return | ||
} | ||
|
||
func (this *UserRepo) GetUserInfo(ctx context.Context, id int) (user *entity.User, err error) { | ||
xesRedis := redisdao.NewSimpleXesRedis(ctx, "redis") | ||
ret, err := xesRedis.Get(REDIS_USER_PREFIX, []interface{}{id}) | ||
if err != nil { | ||
return | ||
} | ||
user = new(entity.User) | ||
json.Unmarshal([]byte(ret), user) | ||
return | ||
} | ||
|
||
func (this *UserRepo) UpdateUser(ctx context.Context, user *entity.User) (err error) { | ||
bytes, _ := json.Marshal(user) | ||
xesRedis := redisdao.NewSimpleXesRedis(ctx, "redis") | ||
_, err = xesRedis.Set(REDIS_USER_PREFIX, []interface{}{user.Id}, string(bytes), 0) | ||
return | ||
} |
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,37 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"odin/app/common" | ||
"odin/app/entity" | ||
"odin/app/repository/memoryRepo" | ||
"odin/app/repository/pikaRepo" | ||
"odin/app/repository/redisRepo" | ||
|
||
logger "github.com/tal-tech/loggerX" | ||
) | ||
|
||
//本方法为根据回放标识选择repo | ||
//获取repo方法名统一用Get[EntityName]Repo,具体逻辑、入参自行定义 | ||
func GetUserRepo(ctx context.Context) (entity.UserRepo, error) { | ||
daoType := common.MEMORY | ||
if IS_PLAYBACK := ctx.Value("IS_PLAYBACK"); IS_PLAYBACK != nil { | ||
if val, ok := IS_PLAYBACK.(string); ok { | ||
if val == "1" { | ||
daoType = common.PIKA | ||
} else { | ||
daoType = common.REDIS | ||
} | ||
} | ||
} | ||
switch daoType { | ||
case common.MEMORY: | ||
return memoryRepo.NewUserRepo(), nil | ||
case common.REDIS: | ||
return redisRepo.NewUserRepo(), nil | ||
case common.PIKA: | ||
return pikaRepo.NewUserRepo(), nil | ||
default: | ||
return nil, logger.NewError("不存在该存储:" + daoType) | ||
} | ||
} |
Oops, something went wrong.