diff options
| author | Mel <einebeere@gmail.com> | 2024-12-29 18:06:59 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2024-12-29 18:06:59 +0100 |
| commit | 18c168e2debe952e932e360ec3dbc9c58ad3cd3f (patch) | |
| tree | 2d7a118a18e5edc17b06fe7a313811711c45ad50 /application/cmd | |
| parent | 5025fab41ac37665ad35967f1f03c16588461605 (diff) | |
| download | specimen-step-1.tar.zst specimen-step-1.zip | |
Read configuration from flags and reply with configured name step-1
Signed-off-by: Mel <einebeere@gmail.com>
Diffstat (limited to 'application/cmd')
| -rw-r--r-- | application/cmd/specimen/cfg/config.go | 65 | ||||
| -rw-r--r-- | application/cmd/specimen/main.go | 15 |
2 files changed, 77 insertions, 3 deletions
diff --git a/application/cmd/specimen/cfg/config.go b/application/cmd/specimen/cfg/config.go new file mode 100644 index 0000000..713ad96 --- /dev/null +++ b/application/cmd/specimen/cfg/config.go @@ -0,0 +1,65 @@ +package cfg + +import ( + "errors" + "flag" + "fmt" + "os" +) + +type Config struct { + Port int + Address string + + // since the server will be restarted when the name file changes, + // we can read the file directly after parsing the arguments, + // and pass the name to the server. no need to pass the path to the server. + Name string +} + +func ParseFromArguments() (Config, error) { + c := Config{} + var namePath string + + defineFlags(&c, &namePath) + flag.Parse() + + if err := validateAndSet(&c, namePath); err != nil { + return Config{}, err + } + + return c, nil +} + +func defineFlags(c *Config, namePath *string) { + flag.IntVar(&c.Port, "port", 4444, "port to listen on") + flag.StringVar(&c.Address, "address", "127.0.0.1", "address to listen on") + flag.StringVar(namePath, "name", "", "path to the name file") +} + +func validateAndSet(c *Config, namePath string) error { + if c.Port < 1 || c.Port > 65535 { + return errors.New("port is out of range") + } + + if namePath == "" { + return errors.New("name file path is required") + } + + file, err := os.Open(namePath) + if err != nil { + return fmt.Errorf("failed to open the name file. %w", err) + } + + name := make([]byte, 1024) + n, err := file.Read(name) + if err != nil { + return fmt.Errorf("failed to read the name file. %w", err) + } + + fmt.Printf("read %d bytes from the name file\n", n) + fmt.Printf("name: %s\n", string(name[:n])) + + c.Name = string(name[:n]) + return nil +} diff --git a/application/cmd/specimen/main.go b/application/cmd/specimen/main.go index cf7ff36..9b3cc03 100644 --- a/application/cmd/specimen/main.go +++ b/application/cmd/specimen/main.go @@ -2,11 +2,13 @@ package main import ( "context" + "flag" "log/slog" "os" "os/signal" "time" + "git.rnrd.eu/specimen/cmd/specimen/cfg" "git.rnrd.eu/specimen/pkg/specimen" "git.rnrd.eu/specimen/pkg/util" ) @@ -17,13 +19,20 @@ func main() { ctx := context.Background() logger := createLogger() - if err := start(ctx, logger); err != nil { + config, err := cfg.ParseFromArguments() + if err != nil { + logger.Error("argument error!", util.SlogErr(err)) + flag.Usage() + os.Exit(1) + } + + if err := start(ctx, logger, config); err != nil { logger.Error("failed to start the specimen application :(", util.SlogErr(err)) os.Exit(1) } } -func start(ctx context.Context, logger *slog.Logger) error { +func start(ctx context.Context, logger *slog.Logger, config cfg.Config) error { // listen for SIGINT (or others on other systems, but who cares about those.. :3) ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) defer cancel() @@ -31,7 +40,7 @@ func start(ctx context.Context, logger *slog.Logger) error { logger.Info("starting the specimen application!", slog.String("version", version)) // start the specimen server! - server := specimen.NewServer(logger) + server := specimen.NewServer(logger, config) go server.Start(ctx) // listen for signals to gracefully shutdown the server |
