-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add cli for managing the relay
- Loading branch information
Showing
15 changed files
with
398 additions
and
183 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package commands | ||
|
||
func HandleHelp(_ []string) { | ||
// TODO::: | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/dezh-tech/immortal/config" | ||
"github.com/dezh-tech/immortal/relay" | ||
) | ||
|
||
func HandleRun(args []string) { | ||
if len(args) < 3 { | ||
ExitOnError(errors.New("at least 2 arguments expected, got 1.\nuse help command for more information")) | ||
} | ||
|
||
cfg, err := config.LoadfromFile(args[2]) | ||
if err != nil { | ||
ExitOnError(err) | ||
} | ||
|
||
r := relay.NewRelay(*cfg) | ||
if err := r.Start(); err != nil { | ||
ExitOnError(err) | ||
} | ||
|
||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) | ||
<-sigChan | ||
|
||
if err := r.Stop(); err != nil { | ||
ExitOnError(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func ExitOnError(err error) { | ||
fmt.Printf("immortal error: %s\n", err.Error()) //nolint | ||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
package main | ||
|
||
import "github.com/dezh-tech/immortal/relay" | ||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
// TODO::: create a full functioning CLI to manage rely. | ||
"github.com/dezh-tech/immortal" | ||
"github.com/dezh-tech/immortal/cmd/commands" | ||
) | ||
|
||
func main() { | ||
s := relay.NewRelay() | ||
err := s.Start() | ||
if err != nil { | ||
panic(err) | ||
if len(os.Args) < 2 { | ||
commands.ExitOnError(errors.New("at least 2 arguments expected, got 1.\nuse help command for more information")) | ||
} | ||
|
||
switch os.Args[1] { | ||
case "run": | ||
commands.HandleRun(os.Args) | ||
|
||
case "help": | ||
commands.HandleHelp(os.Args) | ||
|
||
case "version": | ||
fmt.Println(immortal.StringVersion()) //nolint | ||
os.Exit(0) | ||
|
||
default: | ||
commands.HandleHelp(os.Args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/dezh-tech/immortal/server" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Config reprsents the configs used by relay and other concepts on system. | ||
type Config struct { | ||
ServerConf server.Config `yaml:"server"` | ||
DatabseDSN string | ||
} | ||
|
||
// LoadfromFile loads config from file, databse and env. | ||
func LoadfromFile(path string) (*Config, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, Error{ | ||
reason: err.Error(), | ||
} | ||
} | ||
defer file.Close() | ||
|
||
config := &Config{} | ||
|
||
decoder := yaml.NewDecoder(file) | ||
|
||
if err := decoder.Decode(config); err != nil { | ||
return nil, Error{ | ||
reason: err.Error(), | ||
} | ||
} | ||
|
||
config.DatabseDSN = os.Getenv("IMMO_DB_DSN") | ||
|
||
if err = config.basicCheck(); err != nil { | ||
return nil, Error{ | ||
reason: err.Error(), | ||
} | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
// basicCheck validates the basic stuff in config. | ||
func (c *Config) basicCheck() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This is default immortal config file. | ||
# This config file contains essential information which is needed for bootstraping. | ||
# The rest of configs such as limitations or NIP-11 profile must be changed on database config table, | ||
# which is defined and documented on documents/config.md. | ||
|
||
# server contains information about websocket server. | ||
server: | ||
# bind is the IP address to be bind and listen on. | ||
bind: 127.0.0.1 | ||
# port is websocket port to be listen on. | ||
port: 7777 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dezh-tech/immortal/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadfromFile(t *testing.T) { | ||
cfg, err := config.LoadfromFile("./config.yml") | ||
require.NoError(t, err, "error must be nil.") | ||
|
||
assert.Equal(t, "7777", cfg.ServerConf.Port) | ||
assert.Equal(t, "127.0.0.1", cfg.ServerConf.Bind) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
// Error represents an error in loading or validating config. | ||
type Error struct { | ||
reason string | ||
} | ||
|
||
func (e Error) Error() string { | ||
return fmt.Sprintf("config error: %s\n", e.reason) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.