From 0c0b2680077c1103609c87a652bcc2263460b1d7 Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Thu, 29 Jul 2021 08:42:33 -0700 Subject: Re-organize cmd paths --- .circleci/config.yml | 20 ++-- client/main.go | 114 --------------------- client/mobius-client-config.yaml | 8 -- client/mobius-hotline-server.service | 13 --- cmd/mobius-hotline-client/main.go | 114 +++++++++++++++++++++ .../mobius-client-config.yaml | 8 ++ .../mobius-hotline-server.service | 13 +++ cmd/mobius-hotline-server/main.go | 73 +++++++++++++ .../mobius/config/Agreement.txt | 1 + .../mobius/config/Files/hello.txt | 1 + .../mobius/config/MessageBoard.txt | 1 + .../mobius/config/ThreadedNews.yaml | 1 + .../mobius/config/Users/admin.yaml | 12 +++ .../mobius/config/Users/guest.yaml | 12 +++ .../mobius/config/config.yaml | 12 +++ server/main.go | 73 ------------- server/mobius/config/Agreement.txt | 1 - server/mobius/config/Files/hello.txt | 1 - server/mobius/config/MessageBoard.txt | 1 - server/mobius/config/ThreadedNews.yaml | 1 - server/mobius/config/Users/admin.yaml | 12 --- server/mobius/config/Users/guest.yaml | 12 --- server/mobius/config/config.yaml | 12 --- 23 files changed, 258 insertions(+), 258 deletions(-) delete mode 100644 client/main.go delete mode 100644 client/mobius-client-config.yaml delete mode 100644 client/mobius-hotline-server.service create mode 100644 cmd/mobius-hotline-client/main.go create mode 100644 cmd/mobius-hotline-client/mobius-client-config.yaml create mode 100644 cmd/mobius-hotline-client/mobius-hotline-server.service create mode 100644 cmd/mobius-hotline-server/main.go create mode 100644 cmd/mobius-hotline-server/mobius/config/Agreement.txt create mode 100644 cmd/mobius-hotline-server/mobius/config/Files/hello.txt create mode 100644 cmd/mobius-hotline-server/mobius/config/MessageBoard.txt create mode 100644 cmd/mobius-hotline-server/mobius/config/ThreadedNews.yaml create mode 100644 cmd/mobius-hotline-server/mobius/config/Users/admin.yaml create mode 100644 cmd/mobius-hotline-server/mobius/config/Users/guest.yaml create mode 100644 cmd/mobius-hotline-server/mobius/config/config.yaml delete mode 100644 server/main.go delete mode 100644 server/mobius/config/Agreement.txt delete mode 100644 server/mobius/config/Files/hello.txt delete mode 100644 server/mobius/config/MessageBoard.txt delete mode 100644 server/mobius/config/ThreadedNews.yaml delete mode 100644 server/mobius/config/Users/admin.yaml delete mode 100644 server/mobius/config/Users/guest.yaml delete mode 100644 server/mobius/config/config.yaml diff --git a/.circleci/config.yml b/.circleci/config.yml index 4ac6313..2d65a0f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -42,21 +42,21 @@ jobs: mkdir dist/mobius_server_darwin_amd64 mkdir dist/mobius_server_linux_arm - cd server + cd cmd/server - cp -r mobius/config ../dist/mobius_server_linux_amd64/config - cp -r mobius/config ../dist/mobius_server_darwin_amd64/config - cp -r mobius/config ../dist/mobius_server_linux_arm/config + cp -r mobius/config ../../dist/mobius_server_linux_amd64/config + cp -r mobius/config ../../dist/mobius_server_darwin_amd64/config + cp -r mobius/config ../../dist/mobius_server_linux_arm/config - gox -os="linux" -arch="amd64" -output="../dist/mobius_server_linux_amd64/mobius_server" - gox -os="darwin" -arch="amd64" -output="../dist/mobius_server_darwin_amd64/mobius_server" - gox -os="linux" -arch="arm" -output="../dist/mobius_server_linux_arm/mobius_server" + gox -os="linux" -arch="amd64" -output="../../dist/mobius_server_linux_amd64/mobius_server" + gox -os="darwin" -arch="amd64" -output="../../dist/mobius_server_darwin_amd64/mobius_server" + gox -os="linux" -arch="arm" -output="../../dist/mobius_server_linux_arm/mobius_server" cd ../client - gox -os="linux" -arch="amd64" -output="../dist/mobius_client_linux_amd64/mobius_client" - gox -os="darwin" -arch="amd64" -output="../dist/mobius_client_darwin_amd64/mobius_client" - cd ../dist + gox -os="linux" -arch="amd64" -output="../../dist/mobius_client_linux_amd64/mobius_client" + gox -os="darwin" -arch="amd64" -output="../../dist/mobius_client_darwin_amd64/mobius_client" + cd ../../dist tar -zcvf mobius_server_linux_amd64.tar.gz mobius_server_linux_amd64 tar -zcvf mobius_server_darwin_amd64.tar.gz mobius_server_darwin_amd64 tar -zcvf mobius_server_linux_arm.tar.gz mobius_server_linux_arm diff --git a/client/main.go b/client/main.go deleted file mode 100644 index ec6f895..0000000 --- a/client/main.go +++ /dev/null @@ -1,114 +0,0 @@ -package main - -import ( - "context" - "flag" - "fmt" - "github.com/jhalter/mobius/hotline" - "github.com/rivo/tview" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "log" - "os" - "os/signal" - "runtime" - "syscall" -) - -func main() { - _, cancelRoot := context.WithCancel(context.Background()) - - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt) - - version := flag.Bool("version", false, "print version and exit") - logLevel := flag.String("log-level", "info", "Log level") - logFile := flag.String("log-file", "", "output logs to file") - - flag.Parse() - - if *version { - fmt.Printf("v%s\n", hotline.VERSION) - os.Exit(0) - } - - zapLvl, ok := zapLogLevel[*logLevel] - if !ok { - fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) - os.Exit(0) - } - - // init DebugBuffer - db := &hotline.DebugBuffer{ - TextView: tview.NewTextView(), - } - - cores := []zapcore.Core{newZapCore(zapLvl, db)} - - // Add file logger if optional log-file flag was passed - if *logFile != "" { - f, err := os.OpenFile(*logFile, - os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Println(err) - } - defer f.Close() - if err != nil { - panic(err) - } - cores = append(cores, newZapCore(zapLvl, f)) - } - - l := zap.New(zapcore.NewTee(cores...)) - defer func() { _ = l.Sync() }() - logger := l.Sugar() - logger.Infow("Started Mobius client", "Version", hotline.VERSION) - - go func() { - sig := <-sigChan - logger.Infow("Stopping client", "signal", sig.String()) - cancelRoot() - }() - - cfgPath := defaultConfigPath() - - client := hotline.NewClient(cfgPath, logger) - client.DebugBuf = db - client.UI.Start() - -} - -func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "timestamp" - encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder - - return zapcore.NewCore( - zapcore.NewConsoleEncoder(encoderCfg), - zapcore.Lock(syncer), - level, - ) -} - -var zapLogLevel = map[string]zapcore.Level{ - "debug": zap.DebugLevel, - "info": zap.InfoLevel, - "warn": zap.WarnLevel, - "error": zap.ErrorLevel, -} - -func defaultConfigPath() (cfgPath string) { - os := runtime.GOOS - switch os { - case "windows": - cfgPath = "mobius-client-config.yaml" - case "darwin": - cfgPath = "/usr/local/etc/mobius-client-config.yaml" - case "linux": - cfgPath = "/usr/local/etc/mobius-client-config.yaml" - default: - fmt.Printf("unsupported OS") - } - - return cfgPath -} diff --git a/client/mobius-client-config.yaml b/client/mobius-client-config.yaml deleted file mode 100644 index 978122b..0000000 --- a/client/mobius-client-config.yaml +++ /dev/null @@ -1,8 +0,0 @@ -Username: unnamed -IconID: 414 -Tracker: hltracker.com:5498 -Bookmarks: - - Name: Example Server - Addr: localhost:5500 - Login: guest - Password: "" \ No newline at end of file diff --git a/client/mobius-hotline-server.service b/client/mobius-hotline-server.service deleted file mode 100644 index ec2437c..0000000 --- a/client/mobius-hotline-server.service +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=Mobius Server -After=network.target -StartLimitIntervalSec=0 - -[Service] -Type=simple -Restart=always -RestartSec=1 -ExecStart=/usr/local/bin/mobius-hotline-server - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/cmd/mobius-hotline-client/main.go b/cmd/mobius-hotline-client/main.go new file mode 100644 index 0000000..ec6f895 --- /dev/null +++ b/cmd/mobius-hotline-client/main.go @@ -0,0 +1,114 @@ +package main + +import ( + "context" + "flag" + "fmt" + "github.com/jhalter/mobius/hotline" + "github.com/rivo/tview" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "log" + "os" + "os/signal" + "runtime" + "syscall" +) + +func main() { + _, cancelRoot := context.WithCancel(context.Background()) + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, os.Interrupt) + + version := flag.Bool("version", false, "print version and exit") + logLevel := flag.String("log-level", "info", "Log level") + logFile := flag.String("log-file", "", "output logs to file") + + flag.Parse() + + if *version { + fmt.Printf("v%s\n", hotline.VERSION) + os.Exit(0) + } + + zapLvl, ok := zapLogLevel[*logLevel] + if !ok { + fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) + os.Exit(0) + } + + // init DebugBuffer + db := &hotline.DebugBuffer{ + TextView: tview.NewTextView(), + } + + cores := []zapcore.Core{newZapCore(zapLvl, db)} + + // Add file logger if optional log-file flag was passed + if *logFile != "" { + f, err := os.OpenFile(*logFile, + os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Println(err) + } + defer f.Close() + if err != nil { + panic(err) + } + cores = append(cores, newZapCore(zapLvl, f)) + } + + l := zap.New(zapcore.NewTee(cores...)) + defer func() { _ = l.Sync() }() + logger := l.Sugar() + logger.Infow("Started Mobius client", "Version", hotline.VERSION) + + go func() { + sig := <-sigChan + logger.Infow("Stopping client", "signal", sig.String()) + cancelRoot() + }() + + cfgPath := defaultConfigPath() + + client := hotline.NewClient(cfgPath, logger) + client.DebugBuf = db + client.UI.Start() + +} + +func newZapCore(level zapcore.Level, syncer zapcore.WriteSyncer) zapcore.Core { + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + + return zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderCfg), + zapcore.Lock(syncer), + level, + ) +} + +var zapLogLevel = map[string]zapcore.Level{ + "debug": zap.DebugLevel, + "info": zap.InfoLevel, + "warn": zap.WarnLevel, + "error": zap.ErrorLevel, +} + +func defaultConfigPath() (cfgPath string) { + os := runtime.GOOS + switch os { + case "windows": + cfgPath = "mobius-client-config.yaml" + case "darwin": + cfgPath = "/usr/local/etc/mobius-client-config.yaml" + case "linux": + cfgPath = "/usr/local/etc/mobius-client-config.yaml" + default: + fmt.Printf("unsupported OS") + } + + return cfgPath +} diff --git a/cmd/mobius-hotline-client/mobius-client-config.yaml b/cmd/mobius-hotline-client/mobius-client-config.yaml new file mode 100644 index 0000000..978122b --- /dev/null +++ b/cmd/mobius-hotline-client/mobius-client-config.yaml @@ -0,0 +1,8 @@ +Username: unnamed +IconID: 414 +Tracker: hltracker.com:5498 +Bookmarks: + - Name: Example Server + Addr: localhost:5500 + Login: guest + Password: "" \ No newline at end of file diff --git a/cmd/mobius-hotline-client/mobius-hotline-server.service b/cmd/mobius-hotline-client/mobius-hotline-server.service new file mode 100644 index 0000000..ec2437c --- /dev/null +++ b/cmd/mobius-hotline-client/mobius-hotline-server.service @@ -0,0 +1,13 @@ +[Unit] +Description=Mobius Server +After=network.target +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=1 +ExecStart=/usr/local/bin/mobius-hotline-server + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go new file mode 100644 index 0000000..772625b --- /dev/null +++ b/cmd/mobius-hotline-server/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "context" + "flag" + "fmt" + "github.com/jhalter/mobius/hotline" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "os" +) + +const ( + defaultConfigPath = "/usr/local/var/mobius/config/" // matches Homebrew default config location + defaultPort = 5500 +) + +func main() { + ctx, cancelRoot := context.WithCancel(context.Background()) + + basePort := flag.Int("bind", defaultPort, "Bind address and port") + configDir := flag.String("config", defaultConfigPath, "Path to config root") + version := flag.Bool("version", false, "print version and exit") + logLevel := flag.String("log-level", "info", "Log level") + flag.Parse() + + if *version { + fmt.Printf("v%s\n", hotline.VERSION) + os.Exit(0) + } + + zapLvl, ok := zapLogLevel[*logLevel] + if !ok { + fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) + os.Exit(0) + } + + cores := []zapcore.Core{newStdoutCore(zapLvl)} + l := zap.New(zapcore.NewTee(cores...)) + defer func() { _ = l.Sync() }() + logger := l.Sugar() + + if _, err := os.Stat(*configDir); os.IsNotExist(err) { + logger.Fatalw("Configuration directory not found", "path", configDir) + } + + srv, err := hotline.NewServer(*configDir, "", *basePort, logger) + if err != nil { + logger.Fatal(err) + } + + // Serve Hotline requests until program exit + logger.Fatal(srv.ListenAndServe(ctx, cancelRoot)) +} + +func newStdoutCore(level zapcore.Level) zapcore.Core { + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + + return zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderCfg), + zapcore.Lock(os.Stdout), + level, + ) +} + +var zapLogLevel = map[string]zapcore.Level{ + "debug": zap.DebugLevel, + "info": zap.InfoLevel, + "warn": zap.WarnLevel, + "error": zap.ErrorLevel, +} diff --git a/cmd/mobius-hotline-server/mobius/config/Agreement.txt b/cmd/mobius-hotline-server/mobius/config/Agreement.txt new file mode 100644 index 0000000..5da4639 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/Agreement.txt @@ -0,0 +1 @@ +This is an agreement. Say you agree. diff --git a/cmd/mobius-hotline-server/mobius/config/Files/hello.txt b/cmd/mobius-hotline-server/mobius/config/Files/hello.txt new file mode 100644 index 0000000..271a837 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/Files/hello.txt @@ -0,0 +1 @@ +I'm a test file \ No newline at end of file diff --git a/cmd/mobius-hotline-server/mobius/config/MessageBoard.txt b/cmd/mobius-hotline-server/mobius/config/MessageBoard.txt new file mode 100644 index 0000000..cb36354 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/MessageBoard.txt @@ -0,0 +1 @@ +Welcome to Hotline diff --git a/cmd/mobius-hotline-server/mobius/config/ThreadedNews.yaml b/cmd/mobius-hotline-server/mobius/config/ThreadedNews.yaml new file mode 100644 index 0000000..59e0a68 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/ThreadedNews.yaml @@ -0,0 +1 @@ +Categories: {} diff --git a/cmd/mobius-hotline-server/mobius/config/Users/admin.yaml b/cmd/mobius-hotline-server/mobius/config/Users/admin.yaml new file mode 100644 index 0000000..5413735 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/Users/admin.yaml @@ -0,0 +1,12 @@ +Login: admin +Name: admin +Password: $2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a +Access: +- 255 +- 255 +- 255 +- 255 +- 255 +- 255 +- 255 +- 255 diff --git a/cmd/mobius-hotline-server/mobius/config/Users/guest.yaml b/cmd/mobius-hotline-server/mobius/config/Users/guest.yaml new file mode 100644 index 0000000..a1e4069 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/Users/guest.yaml @@ -0,0 +1,12 @@ +Login: guest +Name: guest +Password: $2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS +Access: +- 252 +- 240 +- 205 +- 201 +- 43 +- 128 +- 0 +- 0 diff --git a/cmd/mobius-hotline-server/mobius/config/config.yaml b/cmd/mobius-hotline-server/mobius/config/config.yaml new file mode 100644 index 0000000..5c8b202 --- /dev/null +++ b/cmd/mobius-hotline-server/mobius/config/config.yaml @@ -0,0 +1,12 @@ +Name: My Hotline server +Description: A default configured Hotline server running Mobius v0.0.1 +BannerID: 0 +FileRoot: Files/ +EnableTrackerRegistration: false +Trackers: +- hltracker.com:5499 +NewsDelimiter: "" +NewsDateFormat: "" +MaxDownloads: 0 +MaxDownloadsPerClient: 0 +MaxConnectionsPerIP: 0 diff --git a/server/main.go b/server/main.go deleted file mode 100644 index 772625b..0000000 --- a/server/main.go +++ /dev/null @@ -1,73 +0,0 @@ -package main - -import ( - "context" - "flag" - "fmt" - "github.com/jhalter/mobius/hotline" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "os" -) - -const ( - defaultConfigPath = "/usr/local/var/mobius/config/" // matches Homebrew default config location - defaultPort = 5500 -) - -func main() { - ctx, cancelRoot := context.WithCancel(context.Background()) - - basePort := flag.Int("bind", defaultPort, "Bind address and port") - configDir := flag.String("config", defaultConfigPath, "Path to config root") - version := flag.Bool("version", false, "print version and exit") - logLevel := flag.String("log-level", "info", "Log level") - flag.Parse() - - if *version { - fmt.Printf("v%s\n", hotline.VERSION) - os.Exit(0) - } - - zapLvl, ok := zapLogLevel[*logLevel] - if !ok { - fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) - os.Exit(0) - } - - cores := []zapcore.Core{newStdoutCore(zapLvl)} - l := zap.New(zapcore.NewTee(cores...)) - defer func() { _ = l.Sync() }() - logger := l.Sugar() - - if _, err := os.Stat(*configDir); os.IsNotExist(err) { - logger.Fatalw("Configuration directory not found", "path", configDir) - } - - srv, err := hotline.NewServer(*configDir, "", *basePort, logger) - if err != nil { - logger.Fatal(err) - } - - // Serve Hotline requests until program exit - logger.Fatal(srv.ListenAndServe(ctx, cancelRoot)) -} - -func newStdoutCore(level zapcore.Level) zapcore.Core { - encoderCfg := zap.NewProductionEncoderConfig() - encoderCfg.TimeKey = "timestamp" - encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder - - return zapcore.NewCore( - zapcore.NewConsoleEncoder(encoderCfg), - zapcore.Lock(os.Stdout), - level, - ) -} - -var zapLogLevel = map[string]zapcore.Level{ - "debug": zap.DebugLevel, - "info": zap.InfoLevel, - "warn": zap.WarnLevel, - "error": zap.ErrorLevel, -} diff --git a/server/mobius/config/Agreement.txt b/server/mobius/config/Agreement.txt deleted file mode 100644 index 5da4639..0000000 --- a/server/mobius/config/Agreement.txt +++ /dev/null @@ -1 +0,0 @@ -This is an agreement. Say you agree. diff --git a/server/mobius/config/Files/hello.txt b/server/mobius/config/Files/hello.txt deleted file mode 100644 index 271a837..0000000 --- a/server/mobius/config/Files/hello.txt +++ /dev/null @@ -1 +0,0 @@ -I'm a test file \ No newline at end of file diff --git a/server/mobius/config/MessageBoard.txt b/server/mobius/config/MessageBoard.txt deleted file mode 100644 index cb36354..0000000 --- a/server/mobius/config/MessageBoard.txt +++ /dev/null @@ -1 +0,0 @@ -Welcome to Hotline diff --git a/server/mobius/config/ThreadedNews.yaml b/server/mobius/config/ThreadedNews.yaml deleted file mode 100644 index 59e0a68..0000000 --- a/server/mobius/config/ThreadedNews.yaml +++ /dev/null @@ -1 +0,0 @@ -Categories: {} diff --git a/server/mobius/config/Users/admin.yaml b/server/mobius/config/Users/admin.yaml deleted file mode 100644 index 5413735..0000000 --- a/server/mobius/config/Users/admin.yaml +++ /dev/null @@ -1,12 +0,0 @@ -Login: admin -Name: admin -Password: $2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a -Access: -- 255 -- 255 -- 255 -- 255 -- 255 -- 255 -- 255 -- 255 diff --git a/server/mobius/config/Users/guest.yaml b/server/mobius/config/Users/guest.yaml deleted file mode 100644 index a1e4069..0000000 --- a/server/mobius/config/Users/guest.yaml +++ /dev/null @@ -1,12 +0,0 @@ -Login: guest -Name: guest -Password: $2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS -Access: -- 252 -- 240 -- 205 -- 201 -- 43 -- 128 -- 0 -- 0 diff --git a/server/mobius/config/config.yaml b/server/mobius/config/config.yaml deleted file mode 100644 index 5c8b202..0000000 --- a/server/mobius/config/config.yaml +++ /dev/null @@ -1,12 +0,0 @@ -Name: My Hotline server -Description: A default configured Hotline server running Mobius v0.0.1 -BannerID: 0 -FileRoot: Files/ -EnableTrackerRegistration: false -Trackers: -- hltracker.com:5499 -NewsDelimiter: "" -NewsDateFormat: "" -MaxDownloads: 0 -MaxDownloadsPerClient: 0 -MaxConnectionsPerIP: 0 -- cgit