Skip to content

Commit

Permalink
Add server command
Browse files Browse the repository at this point in the history
This command is intended to demonstrate setting up
a SOAP server to receive data from SdI.

Co-authored-by: Alex Malaszkiewicz <[email protected]>
Co-authored-by: Grzeg Lisowski <[email protected]>
  • Loading branch information
torrocus and noplisu committed May 24, 2024
1 parent 1d73b4d commit f5fdd13
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/gobl.fatturapa/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (o *rootOpts) cmd() *cobra.Command {

cmd.AddCommand(versionCmd())
cmd.AddCommand(convert(o).cmd())
cmd.AddCommand(server(o).cmd())
cmd.AddCommand(transmit(o).cmd())

return cmd
Expand Down
72 changes: 72 additions & 0 deletions cmd/gobl.fatturapa/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"

sdi "github.com/invopop/gobl.fatturapa/sdi"
"github.com/spf13/cobra"
)

type serverOpts struct {
*rootOpts
}

func server(o *rootOpts) *serverOpts {
return &serverOpts{rootOpts: o}
}

func (c *serverOpts) cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "server [host] [port]",
Short: "Server for communication with SdI in selected environment",
RunE: c.runE,
}

cmd.Flags().BoolP("verbose", "v", false, "Logs all requests into the console")

// f := cmd.Flags()

return cmd
}

func (c *serverOpts) runE(cmd *cobra.Command, args []string) error {
host := inputHost(args)
port := inputPort(args)

verbose, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
}

if verbose {
fmt.Printf("Server start: %s:%s\n", host, port)
}

// err := sdi.DemoListener(host, port)
config := &sdi.ServerConfig{
Host: host,
Port: port,
Verbose: verbose,
}

err = sdi.RunServer(config)
if err != nil {
return fmt.Errorf("sending error: %w", err)
}

return nil
}

func inputHost(args []string) string {
if len(args) > 0 && args[0] != "-" {
return args[0]
}
return ""
}

func inputPort(args []string) string {
if len(args) > 1 && args[1] != "-" {
return args[1]
}
return ""
}

0 comments on commit f5fdd13

Please sign in to comment.