Skip to content

Commit

Permalink
feat(client): supporting register service for kraken.
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Dec 13, 2024
1 parent 7f9ce0f commit 5d1320e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dezh-tech/immortal/client"
"github.com/dezh-tech/immortal/database"
"github.com/dezh-tech/immortal/handler"
"github.com/dezh-tech/immortal/relay/redis"
"github.com/dezh-tech/immortal/server"
"github.com/joho/godotenv"
Expand All @@ -18,7 +19,7 @@ type Config struct {
WebsocketServer server.Config `yaml:"ws_server"`
Database database.Config `yaml:"database"`
RedisConf redis.Config `yaml:"redis"`
Parameters Parameters
Handler handler.Config
}

// Load loads config from file and env.
Expand Down
33 changes: 16 additions & 17 deletions config/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ func (c *Config) LoadParameters(params *kraken.GetConfigResponse) error {
return err
}

c.Parameters.WebsocketServer = &server.Config{
URL: url,
Limitation: &server.Limitation{
MaxMessageLength: params.Limitations.MaxMessageLength,
MaxSubscriptions: params.Limitations.MaxSubscriptions,
MaxFilters: params.Limitations.MaxFilters,
MaxSubidLength: params.Limitations.MaxSubidLength,
MinPowDifficulty: params.Limitations.MinPowDifficulty,
AuthRequired: params.Limitations.AuthRequired,
PaymentRequired: params.Limitations.PaymentRequired,
RestrictedWrites: params.Limitations.RestrictedWrites,
MaxEventTags: params.Limitations.MaxEventTags,
MaxContentLength: params.Limitations.MaxContentLength,
CreatedAtLowerLimit: params.Limitations.CreatedAtLowerLimit,
CreatedAtUpperLimit: params.Limitations.CreatedAtUpperLimit,
},
c.WebsocketServer.URL = url

c.WebsocketServer.Limitation = &server.Limitation{
MaxMessageLength: params.Limitations.MaxMessageLength,
MaxSubscriptions: params.Limitations.MaxSubscriptions,
MaxFilters: params.Limitations.MaxFilters,
MaxSubidLength: params.Limitations.MaxSubidLength,
MinPowDifficulty: params.Limitations.MinPowDifficulty,
AuthRequired: params.Limitations.AuthRequired,
PaymentRequired: params.Limitations.PaymentRequired,
RestrictedWrites: params.Limitations.RestrictedWrites,
MaxEventTags: params.Limitations.MaxEventTags,
MaxContentLength: params.Limitations.MaxContentLength,
CreatedAtLowerLimit: params.Limitations.CreatedAtLowerLimit,
CreatedAtUpperLimit: params.Limitations.CreatedAtUpperLimit,
}

c.Parameters.Handler = &handler.Config{
c.Handler = handler.Config{
DefaultQueryLimit: params.Limitations.DefaultQueryLimit,
MaxQueryLimit: params.Limitations.MaxQueryLimit,
}
Expand Down
4 changes: 2 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ func getCollectionName(k types.Kind) string {

type Handler struct {
db *database.Database
config *Config
config Config
}

func New(db *database.Database, cfg *Config) *Handler {
func New(db *database.Database, cfg Config) *Handler {
return &Handler{
db: db,
config: cfg,
Expand Down
2 changes: 1 addition & 1 deletion relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func New(cfg *config.Config) (*Relay, error) {
return nil, err
}

h := handler.New(db, cfg.Parameters.Handler)
h := handler.New(db, cfg.Handler)

ws, err := server.New(cfg.WebsocketServer, h, m, r)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions server/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {
client, ok := s.conns[conn]
if !ok {
_ = conn.WriteMessage(1, message.MakeOK(false,
"",
msg.Event.ID,
fmt.Sprintf("error: can't find connection %s",
conn.RemoteAddr())))

Expand All @@ -51,7 +51,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {
authm := message.MakeAuth(client.challenge)

okm := message.MakeOK(false,
"",
msg.Event.ID,
"auth-required: we only accept events from authenticated users.",
)

Expand All @@ -68,7 +68,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {
authm := message.MakeAuth(client.challenge)

okm := message.MakeOK(false,
"",
msg.Event.ID,
"auth-required: this event may only be published by its author.",
)

Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {

if len(msg.Event.Content) > int(s.config.Limitation.MaxContentLength) {
okm := message.MakeOK(false,
"",
msg.Event.ID,
fmt.Sprintf("error: max limit of content length is %d", s.config.Limitation.MaxContentLength),
)

Expand All @@ -125,7 +125,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {

if msg.Event.Difficulty() < int(s.config.Limitation.MinPowDifficulty) {
okm := message.MakeOK(false,
"",
msg.Event.ID,
fmt.Sprintf("error: min pow required is %d", s.config.Limitation.MinPowDifficulty),
)

Expand All @@ -138,7 +138,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {

if len(msg.Event.Tags) > int(s.config.Limitation.MaxEventTags) {
okm := message.MakeOK(false,
"",
msg.Event.ID,
fmt.Sprintf("error: max limit of tags count is %d", s.config.Limitation.MaxEventTags),
)

Expand All @@ -152,7 +152,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) {
if msg.Event.CreatedAt < s.config.Limitation.CreatedAtLowerLimit ||
msg.Event.CreatedAt > s.config.Limitation.CreatedAtUpperLimit {
okm := message.MakeOK(false,
"",
msg.Event.ID,
fmt.Sprintf("error: created at must be as least %d and at most %d",
s.config.Limitation.CreatedAtLowerLimit, s.config.Limitation.CreatedAtUpperLimit),
)
Expand Down

0 comments on commit 5d1320e

Please sign in to comment.