Skip to content

Commit

Permalink
sync accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed May 18, 2024
1 parent 7f19704 commit 47d8583
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 7 deletions.
58 changes: 58 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,39 @@ const docTemplate = `{
}
}
},
"/api/account/v1/sync": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "download accounts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Account"
],
"parameters": [
{
"type": "integer",
"description": "how many accounts to download",
"name": "count",
"in": "query",
"required": true
}
],
"responses": {
"201": {
"description": "Created"
}
}
}
},
"/api/auth/v1/login": {
"post": {
"description": "Get AccessToken By ApiKey",
Expand All @@ -79,6 +112,31 @@ const docTemplate = `{
}
}
}
},
"/api/dashboard/v1/node": {
"get": {
"description": "get node members",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Dashboard"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
Expand Down
58 changes: 58 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@
}
}
},
"/api/account/v1/sync": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "download accounts",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Account"
],
"parameters": [
{
"type": "integer",
"description": "how many accounts to download",
"name": "count",
"in": "query",
"required": true
}
],
"responses": {
"201": {
"description": "Created"
}
}
}
},
"/api/auth/v1/login": {
"post": {
"description": "Get AccessToken By ApiKey",
Expand All @@ -68,6 +101,31 @@
}
}
}
},
"/api/dashboard/v1/node": {
"get": {
"description": "get node members",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Dashboard"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
Expand Down
36 changes: 36 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ paths:
- JWT: []
tags:
- Account
/api/account/v1/sync:
get:
consumes:
- application/json
description: download accounts
parameters:
- description: how many accounts to download
in: query
name: count
required: true
type: integer
produces:
- application/json
responses:
"201":
description: Created
security:
- JWT: []
tags:
- Account
/api/auth/v1/login:
post:
consumes:
Expand All @@ -54,6 +74,22 @@ paths:
description: Created
tags:
- Auth
/api/dashboard/v1/node:
get:
consumes:
- application/json
description: get node members
produces:
- application/json
responses:
"200":
description: OK
schema:
items:
type: string
type: array
tags:
- Dashboard
securityDefinitions:
JWT:
description: Type 'Bearer \<TOKEN\>' to correctly set the AccessToken
Expand Down
6 changes: 3 additions & 3 deletions internal/community/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func BindAccount(hashedAccount string, publicKey *string) error {
}
}

func ListMembers() []string {
func ListNodes() []string {
var members []string
for _, member := range community.Node.Members.Members() {
members = append(members, member.Name)
for _, node := range community.Node.Members.Members() {
members = append(members, node.Name)
}
return members
}
Expand Down
7 changes: 5 additions & 2 deletions internal/community/node/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func (d *CommunityDelegate) MergeRemoteState(buf []byte, join bool) {
if len(buf) > 0 {
if join {
go func() {
members := storage.UnmarshalMembers(buf)
storage.InitRemoteMember(members)
if snap, err := storage.UnmarshalSnapshot(buf); err == nil {
_ = snap
// members := storage.UnmarshalMembers(buf)
// storage.InitRemoteMember(members)
}
}()
} else {
go func() {
Expand Down
28 changes: 28 additions & 0 deletions internal/web_server/controllers/account/v1/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package account_v1

import (
"another_node/internal/web_server/pkg"
"another_node/internal/web_server/pkg/response"

"github.com/gin-gonic/gin"
)

// Sync download accounts
// @Tags Account
// @Description download accounts
// @Accept json
// @Produce json
// @Success 201
// @Router /api/account/v1/sync [GET]
// @Param count query int true "how many accounts to download"
// @Security JWT
func Sync(ctx *gin.Context) {
if count, err := pkg.GetUIntParamFromQueryOrPath("count", ctx, false); err != nil {
response.BadRequest(ctx, err)
return
} else {
_ = count
//community.SyncAccounts(count)
response.Created(ctx, nil)
}
}
4 changes: 2 additions & 2 deletions internal/web_server/controllers/dashboard/v1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
// @Description get node members
// @Accept json
// @Produce json
// @Success 200
// @Success 200 {array} string
// @Router /api/dashboard/v1/node [GET]
func Node(ctx *gin.Context) {
members := community.ListMembers()
members := community.ListNodes()

ctx.JSON(200, members)
}
37 changes: 37 additions & 0 deletions internal/web_server/pkg/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pkg

import (
"strconv"
"strings"

"github.com/gin-gonic/gin"
)

func GetUIntParamFromQueryOrPath(paramKey string, ctx *gin.Context, allowNull bool) (uint, error) {

keyStr := ctx.Param(paramKey)
if len(keyStr) == 0 {
keyStr = ctx.Query(paramKey)
}
if allowNull {
if len(keyStr) == 0 {
return 0, nil
}
}
key, err := strconv.ParseUint(keyStr, 10, 64)
if err != nil {
return 0, err
} else {
return uint(key), nil
}
}

func GetBoolParamFromQuery(paramKey string, ctx *gin.Context, defaultValue bool) bool {

keyStr := ctx.Query(paramKey)
if len(keyStr) == 0 {
return defaultValue
}

return strings.EqualFold("true", keyStr)
}

0 comments on commit 47d8583

Please sign in to comment.