-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
service, net, cmd/microservice: add microservice example, simplify mi…
…croservice init code, move http request structure to cmd/flatend
- Loading branch information
Showing
8 changed files
with
161 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package flatend | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/lithdew/kademlia" | ||
"net" | ||
"os" | ||
"os/signal" | ||
) | ||
|
||
type Service struct { | ||
Addr string | ||
BindAddr string | ||
|
||
PrivateKey kademlia.PrivateKey | ||
Services map[string]Handler | ||
} | ||
|
||
func (s Service) Start() error { | ||
addr := s.Addr | ||
if addr == "" { | ||
return errors.New("address to flatend hub must be provided") | ||
} | ||
|
||
bindAddr := s.BindAddr | ||
if bindAddr == "" { | ||
bindAddr = ":0" | ||
} | ||
|
||
var err error | ||
|
||
privateKey := s.PrivateKey | ||
if privateKey == kademlia.ZeroPrivateKey { | ||
_, privateKey, err = kademlia.GenerateKeys(nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate keys: %w", err) | ||
} | ||
} | ||
|
||
ln, err := net.Listen("tcp", bindAddr) | ||
if err != nil { | ||
return fmt.Errorf("failed to listen on bind addr '%s': %w", bindAddr, err) | ||
} | ||
|
||
node, err := NewNode(privateKey, ln.Addr().String()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for service, handler := range s.Services { | ||
node.Register(service, handler) | ||
} | ||
|
||
errCh := make(chan error, 1) | ||
go func() { | ||
errCh <- node.Serve(ln) | ||
}() | ||
|
||
if err := node.Dial(addr); err != nil { | ||
return fmt.Errorf("failed to reach flatend hub: %w", err) | ||
} | ||
|
||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, os.Interrupt) | ||
<-ch | ||
|
||
node.Shutdown() | ||
ln.Close() | ||
|
||
return <-errCh | ||
} |