Skip to content

Commit

Permalink
cmd: Add command to start server
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Jun 11, 2024
1 parent 3191a8d commit 0bae9ac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
51 changes: 50 additions & 1 deletion cmd/xconn/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

import (
"bytes"
_ "embed" // nolint:gci
"fmt"
"log"
"os"

"github.com/alecthomas/kingpin/v2"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"

"github.com/xconnio/wampproto-protobuf/go"
"github.com/xconnio/xconn-go"
)

var (
Expand All @@ -19,20 +25,25 @@ const (

ConfigDir = ".xconn"
ConfigFile = ConfigDir + "/config.yaml"

ProtobufSubProtocol = "wamp.2.protobuf"
)

type cmd struct {
parsedCommand string

init *kingpin.CmdClause

start *kingpin.CmdClause
}

func parseCommand(args []string) (*cmd, error) {
app := kingpin.New(args[0], "XConn")
app.Version(versionString).VersionFlag.Short('v')

c := &cmd{
init: app.Command("init", "Initialize sample router config."),
init: app.Command("init", "Initialize sample router config."),
start: app.Command("start", "Start the router."),
}

parsedCommand, err := app.Parse(args[1:])
Expand Down Expand Up @@ -61,6 +72,44 @@ func Run(args []string) (string, error) {
}

return fmt.Sprintf("Sample router config initialized at %s.", ConfigFile), nil

case c.start.FullCommand():
data, err := os.ReadFile(ConfigFile)
if err != nil {
return "", fmt.Errorf("unable to read config file: %w", err)
}

var decoder = yaml.NewDecoder(bytes.NewBuffer(data))
decoder.KnownFields(true)

var config Config
if err := decoder.Decode(&config); err != nil {
return "", fmt.Errorf("unable to decode config file: %w", err)
}

router := xconn.NewRouter()

for _, realm := range config.Realms {
router.AddRealm(realm.Name)
}

server := xconn.NewServer(router, nil)

for _, transport := range config.Transports {
if slices.Contains(transport.Serializers, "protobuf") {
serializer := &wampprotobuf.ProtobufSerializer{}
protobufSpec := xconn.NewWSSerializerSpec(ProtobufSubProtocol, serializer)

if err := server.RegisterSpec(protobufSpec); err != nil {
return "", err
}
}

if err := server.Start("0.0.0.0", transport.Port); err != nil {
return "", err
}
}

}

return "", nil
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/gammazero/workerpool v1.1.3
github.com/gobwas/ws v1.4.0
github.com/stretchr/testify v1.8.4
github.com/xconnio/wampproto-go v0.0.0-20240606221544-14a7a1771d92
github.com/xconnio/wampproto-go v0.0.0-20240611083555-4bcae9e441a3
github.com/xconnio/wampproto-protobuf/go v0.0.0-20240611092706-1e859744b5a2
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -28,5 +30,5 @@ require (
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -35,8 +36,10 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xconnio/wampproto-go v0.0.0-20240606221544-14a7a1771d92 h1:sQGmy7DEnqegkRvLqWNkz+BY7n1DiisfptURHk7Nh30=
github.com/xconnio/wampproto-go v0.0.0-20240606221544-14a7a1771d92/go.mod h1:/b7EyR1X9EkOHPQBJGz1KvdjClo1GsalBGIzjQU5+i4=
github.com/xconnio/wampproto-go v0.0.0-20240611083555-4bcae9e441a3 h1:iVd1el18AP1N3SnZy06ivkKcHtTuTD7cg3JhUM+xQbU=
github.com/xconnio/wampproto-go v0.0.0-20240611083555-4bcae9e441a3/go.mod h1:/b7EyR1X9EkOHPQBJGz1KvdjClo1GsalBGIzjQU5+i4=
github.com/xconnio/wampproto-protobuf/go v0.0.0-20240611092706-1e859744b5a2 h1:1WkQ68ICoin0wTerMn5FrWl+ZbK4XS3/W2Wr86jS9K8=
github.com/xconnio/wampproto-protobuf/go v0.0.0-20240611092706-1e859744b5a2/go.mod h1:SZAkbKSDucIUBrPgcKlT0SzVmktfrsD7OdP1chFR+vw=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
Expand All @@ -47,6 +50,8 @@ golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQ
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit 0bae9ac

Please sign in to comment.