Skip to content

Commit

Permalink
web api
Browse files Browse the repository at this point in the history
  • Loading branch information
danbai225 committed Jan 4, 2024
1 parent 470ef43 commit ba33cfc
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 129 deletions.
42 changes: 42 additions & 0 deletions api_server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api_server

import (
"fmt"
logs "github.com/danbai225/go-logs"
"github.com/gin-gonic/gin"
"go-rustdesk-server/common"
"go-rustdesk-server/data_server"
)

func Start() {
router := gin.Default()
apiGroup := router.Group("/api/v1")
apiGroup.POST("/login", login)
// 启动Web服务器,并指定端口
err := router.Run(fmt.Sprintf(":%d", common.Conf.WebPort))
if err != nil {
logs.Err(err)
}
}
func login(ctx *gin.Context) {
db, err := data_server.GetDataSever()
if err != nil {
logs.Err(err)
ctx.JSON(500, gin.H{"msg": "server error"})
return
}
name, err := db.GetUserByName("admin")
if err != nil {
logs.Err(err)
ctx.JSON(500, gin.H{"msg": "server error"})
return
}
if name == nil {
ctx.JSON(200, gin.H{"msg": "not found"})
return
}
if name.Password != "123" {
ctx.JSON(200, gin.H{"msg": "password error"})
return
}
}
116 changes: 0 additions & 116 deletions cmd/api/server_api.go

This file was deleted.

1 change: 1 addition & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
RelayPort uint16 `json:"relay_port"`
MustKey bool `json:"must_key"`
RelayIp string `json:"relay_ip"`
WebPort uint16 `json:"web_port"`
}

var Conf *Config
Expand Down
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"server_port": 21116,
"relay_port":21117,
"reg_port": 21120,
"relay_ip": "127.0.0.1"
"relay_ip": "127.0.0.1",
"web_port": 21114
}
2 changes: 1 addition & 1 deletion data_server/data_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type DataSever interface {
AddPeer(peer *model.Peer) error
AddPeerOrUpdate(peer *model.Peer) error
UpdatePeer(peer *model.Peer) error
DelPeerByUUID(uuid string) error
GetRelayByName(name string) (*model.Relay, error)
UpdateRelay(relay *model.Relay) error
AddRelay(relay *model.Relay) error
AddRelayOrUpdate(relay *model.Relay) error
GetRelayAllOnline() ([]*model.Relay, error)
GetUserByName(name string) (*model.User, error)
Close() error
}

Expand Down
23 changes: 13 additions & 10 deletions data_server/impl/clover_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

const (
objectIdField = "_id"
TableNamePeer = "Peer"
TableNameRelay = "Relay"
TableNameUser = "User"
)

type CloverDataSever struct {
Expand Down Expand Up @@ -123,15 +123,6 @@ func (c *CloverDataSever) AddPeerOrUpdate(peer *model.Peer) error {
}
return nil
}
func (c *CloverDataSever) DelPeerByUUID(uuid string) error {
peer, err := c.GetPeerByUUID(uuid)
defer c.peerLock.Unlock()
c.peerLock.Lock()
if err == nil {
return c.DB.Query(TableNamePeer).Where(clover.Field("_id").Eq(peer.Uid)).Delete()
}
return nil
}

func (c *CloverDataSever) AddRelay(relay *model.Relay) error {
if relay == nil {
Expand Down Expand Up @@ -203,3 +194,15 @@ func (c *CloverDataSever) GetRelayByName(name string) (*model.Relay, error) {
err = first.Unmarshal(&peer)
return &peer, err
}

func (c *CloverDataSever) GetUserByName(name string) (*model.User, error) {
defer c.relayLock.RUnlock()
c.relayLock.RLock()
first, err := c.DB.Query(TableNameUser).Where(clover.Field("name").Eq(name)).FindFirst()
if err != nil || first == nil {
return nil, err
}
peer := model.User{}
err = first.Unmarshal(&peer)
return &peer, err
}
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/danbai225/go-logs v0.3.2
github.com/gin-gonic/gin v1.9.1
github.com/go-ping/ping v1.1.0
github.com/gogf/gf/v2 v2.5.7
github.com/ostafen/clover v1.2.0
Expand All @@ -15,14 +16,21 @@ require (
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.0 // indirect
Expand All @@ -31,20 +39,33 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kpango/fastime v1.1.9 // indirect
github.com/kpango/glg v1.6.15 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit ba33cfc

Please sign in to comment.