Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nodemanager
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Odysseus
nodemanager
Commits
b3a690af
Commit
b3a690af
authored
Jan 16, 2024
by
vicotor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add payment option
parent
98feb121
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
78 additions
and
57 deletions
+78
-57
config.toml
config.toml
+1
-0
Config.go
config/Config.go
+2
-1
go.mod
go.mod
+2
-1
go.sum
go.sum
+3
-0
db.go
model/db.go
+4
-1
user.go
model/user.go
+22
-18
workerManager.go
server/workerManager.go
+44
-36
No files found.
config.toml
View file @
b3a690af
endpoint
=
"127.0.0.1:10001"
metrics_port
=
28010
private_key
=
"E671C143A110C239B563F702E9F4017CA6B2B2912F675EED9AA4FED684EB30CC"
enable_pay
=
false
[redis]
addr
=
"127.0.0.1:6379"
...
...
config/Config.go
View file @
b3a690af
...
...
@@ -10,7 +10,7 @@ type MysqlConfig struct {
Host
string
`json:"host" toml:"host"`
Port
int
`json:"port" toml:"port"`
User
string
`json:"user" toml:"user"`
Passwd
string
`json:"passw
d" toml:"passw
d"`
Passwd
string
`json:"passw
ord" toml:"passwor
d"`
DbName
string
`json:"database" toml:"database"`
}
...
...
@@ -24,6 +24,7 @@ type Config struct {
PrivateKey
string
`json:"private_key" toml:"private_key"`
Endpoint
string
`json:"endpoint" toml:"endpoint"`
MetricPort
int
`json:"metrics_port" toml:"metrics_port"`
EnablePay
bool
`json:"enable_pay" toml:"enable_pay"`
Redis
RedisConfig
`json:"redis" toml:"redis"`
DbConfig
MysqlConfig
`json:"mysql" toml:"mysql"`
}
...
...
go.mod
View file @
b3a690af
...
...
@@ -6,6 +6,8 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/astaxie/beego v1.12.3
github.com/ethereum/go-ethereum v1.13.10
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.5.3
github.com/hashicorp/golang-lru v0.5.4
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/odysseus/odysseus-protocol v0.0.0-00010101000000-000000000000
...
...
@@ -27,7 +29,6 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
...
...
go.sum
View file @
b3a690af
...
...
@@ -56,6 +56,7 @@ github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
...
...
@@ -112,9 +113,11 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkL
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
...
...
model/db.go
View file @
b3a690af
...
...
@@ -8,9 +8,12 @@ import (
)
func
DbInit
()
{
if
config
.
GetConfig
()
.
EnablePay
==
false
{
return
}
// Set up database
dbconf
:=
config
.
GetConfig
()
.
DbConfig
datasource
:=
fmt
.
Sprintf
(
"%s:%s@tcp(%s:%
s
)/%s?charset=utf8"
,
dbconf
.
User
,
dbconf
.
Passwd
,
dbconf
.
Host
,
dbconf
.
Port
,
dbconf
.
DbName
)
datasource
:=
fmt
.
Sprintf
(
"%s:%s@tcp(%s:%
d
)/%s?charset=utf8"
,
dbconf
.
User
,
dbconf
.
Passwd
,
dbconf
.
Host
,
dbconf
.
Port
,
dbconf
.
DbName
)
orm
.
RegisterDriver
(
"mysql"
,
orm
.
DRMySQL
)
err
:=
orm
.
RegisterDataBase
(
"default"
,
"mysql"
,
datasource
)
if
err
!=
nil
{
...
...
model/user.go
View file @
b3a690af
...
...
@@ -6,22 +6,26 @@ import (
)
type
User
struct
{
ID
uint
`orm:"auto;pk;column(id);description(用户id)"`
Name
string
`orm:"size(20);null;description(开发者名称)"`
Phone
string
`orm:"size(15);null;description(电话)"`
Mail
string
`orm:"size(120);null;description(邮箱)"`
ChainAccount
string
`orm:"size(42);null;description(区块链账号)"`
Type
uint8
`orm:"default(0);description(用户类型:1=自然人 2=企业)"`
IsAuthed
uint8
`orm:"default(0);description(是否实名)"`
RegisterFrom
uint8
`orm:"default(0);description(注册来源,三方账号)"`
RegisterFromDesc
string
`orm:"size(16);null;description(三方账户描述)"`
Username
string
`orm:"size(20);default('');description(开发者平台用户名)"`
Balance
int64
`orm:"null;description(用户账户余额)"`
Level
uint8
`orm:"default(0);description(信用等级,决定免费调用额度)"`
CustomID
string
`orm:"size(120);null;description(自定义kong的id)"`
CreatedTime
uint64
`orm:"default(0);description(创建时间)"`
UpdatedTime
uint64
`orm:"default(0);description(更新时间)"`
Deleted
uint8
`orm:"default(0);description(逻辑删除)"`
ID
int64
`orm:"column(id)" db:"id" json:"id" form:"id"`
// 用户id
Name
string
`orm:"column(name)" db:"name" json:"name" form:"name"`
// 开发者名称
Phone
string
`orm:"column(phone)" db:"phone" json:"phone" form:"phone"`
// 电话
Mail
string
`orm:"column(mail)" db:"mail" json:"mail" form:"mail"`
// 邮箱
ChainAccount
string
`orm:"column(chain_account)" db:"chain_account" json:"chain_account" form:"chain_account"`
// 区块链账号
Type
int64
`orm:"column(type)" db:"type" json:"type" form:"type"`
IsAuthed
int64
`orm:"column(is_authed)" db:"is_authed" json:"is_authed" form:"is_authed"`
// 是否实名
RegisterFrom
int64
`orm:"column(register_from)" db:"register_from" json:"register_from" form:"register_from"`
// 注册来源,三方账号
RegisterFromDesc
string
`orm:"column(register_from_desc)" db:"register_from_desc" json:"register_from_desc" form:"register_from_desc"`
// 三方账户描述
Username
string
`orm:"column(username)" db:"username" json:"username" form:"username"`
// 开发者平台用户名
Balance
int64
`orm:"column(balance)" db:"balance" json:"balance" form:"balance"`
// 用户账户余额
Level
int64
`orm:"column(level)" db:"level" json:"level" form:"level"`
// 信用等级,决定免费调用额度
CustomId
string
`orm:"column(custom_id)" db:"custom_id" json:"custom_id" form:"custom_id"`
// 自定义kong的id
CreatedTime
int64
`orm:"column(created_time)" db:"created_time" json:"created_time" form:"created_time"`
// 创建时间
UpdatedTime
int64
`orm:"column(updated_time)" db:"updated_time" json:"updated_time" form:"updated_time"`
// 更新时间
Deleted
int64
`orm:"column(deleted)" db:"deleted" json:"deleted" form:"deleted"`
// 逻辑删除
}
func
(
User
)
TableName
()
string
{
return
"user"
}
type
UserRepository
interface
{
...
...
@@ -46,8 +50,8 @@ func (repo *UserRepositoryImpl) Create(user *User) error {
}
func
(
repo
*
UserRepositoryImpl
)
GetByUserId
(
uid
int64
)
(
*
User
,
error
)
{
user
:=
User
{
ID
:
ui
nt
(
uid
)
}
err
:=
repo
.
o
.
Read
(
&
user
,
"Id"
)
user
:=
User
{
ID
:
ui
d
}
err
:=
repo
.
o
.
Read
(
&
user
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
server/workerManager.go
View file @
b3a690af
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/golang/protobuf/proto"
lru
"github.com/hashicorp/golang-lru"
"github.com/odysseus/nodemanager/config"
"github.com/odysseus/nodemanager/model"
"github.com/odysseus/nodemanager/utils"
odysseus
"github.com/odysseus/odysseus-protocol/gen/proto/go/base/v1"
...
...
@@ -283,43 +284,8 @@ func (wm *WorkerManager) manageWorker(worker *Worker) error {
}
else
{
log
.
WithField
(
"taskid"
,
task
.
TaskId
)
.
Debug
(
"post task result"
)
}
// pay for task.
uid
,
err
:=
strconv
.
ParseInt
(
task
.
TaskUid
,
10
,
64
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"error"
:
err
,
})
.
Error
(
"parse task uid failed"
)
}
user
,
err
:=
wm
.
userRepo
.
GetByUserId
(
uid
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"error"
:
err
,
})
.
Error
(
"get user by uid failed"
)
}
if
user
!=
nil
{
fee
,
_
:=
strconv
.
ParseInt
(
task
.
TaskFee
,
10
,
64
)
user
.
Balance
=
user
.
Balance
-
fee
err
=
wm
.
userRepo
.
Update
(
user
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"error"
:
err
,
})
.
Error
(
"update user failed"
)
}
else
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"fee"
:
fee
,
})
.
Info
(
"pay task fee finished"
)
}
}
}()
wm
.
Payment
(
task
)
_
=
wm
.
AddWorker
(
worker
)
// todo: post event for task succeed or failed
return
true
...
...
@@ -395,3 +361,45 @@ func (wm *WorkerManager) handleWorkerMsg(worker *Worker) {
}
}
}
func
(
wm
*
WorkerManager
)
Payment
(
task
*
odysseus
.
TaskContent
)
error
{
if
config
.
GetConfig
()
.
EnablePay
==
true
{
// pay for task.
uid
,
err
:=
strconv
.
ParseInt
(
task
.
TaskUid
,
10
,
64
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"error"
:
err
,
})
.
Error
(
"parse task uid failed"
)
}
user
,
err
:=
wm
.
userRepo
.
GetByUserId
(
uid
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"error"
:
err
,
})
.
Error
(
"get user by uid failed"
)
}
if
user
!=
nil
{
fee
,
_
:=
strconv
.
ParseInt
(
task
.
TaskFee
,
10
,
64
)
user
.
Balance
=
user
.
Balance
-
fee
err
=
wm
.
userRepo
.
Update
(
user
)
if
err
!=
nil
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"error"
:
err
,
})
.
Error
(
"update user failed"
)
}
else
{
log
.
WithFields
(
log
.
Fields
{
"taskid"
:
task
.
TaskId
,
"uid"
:
task
.
TaskUid
,
"fee"
:
fee
,
})
.
Info
(
"pay task fee finished"
)
}
}
}
return
nil
}
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