-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Checkpointing initial static filesystem * Add some simple config to specify hosting * Panic on bad config, don't default * Check config first * Whitespace * Use gonsen for pages * Move execution code into its own package so we can directly access it from http server in next commit * Basic working http endpoint with actual configured endpoints
- Loading branch information
Showing
19 changed files
with
445 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cyn | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/evertras/cynomys/pkg/httpserver" | ||
"github.com/evertras/cynomys/pkg/listener" | ||
"github.com/evertras/cynomys/pkg/sender" | ||
) | ||
|
||
type Cyn struct { | ||
mu sync.RWMutex | ||
|
||
tcpListeners []*listener.TCPListener | ||
udpListeners []*listener.UDPListener | ||
tcpSenders []*sender.TCPSender | ||
udpSenders []*sender.UDPSender | ||
|
||
httpServer *httpserver.Server | ||
} | ||
|
||
func New() *Cyn { | ||
return &Cyn{} | ||
} | ||
|
||
func (c *Cyn) AddTCPListener(tcpListener *listener.TCPListener) { | ||
c.mu.Lock() | ||
c.tcpListeners = append(c.tcpListeners, tcpListener) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *Cyn) TCPListeners() []*listener.TCPListener { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
|
||
listeners := make([]*listener.TCPListener, len(c.tcpListeners)) | ||
copy(listeners, c.tcpListeners) | ||
|
||
return listeners | ||
} | ||
|
||
func (c *Cyn) AddUDPListener(udpListener *listener.UDPListener) { | ||
c.mu.Lock() | ||
c.udpListeners = append(c.udpListeners, udpListener) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *Cyn) UDPListeners() []*listener.UDPListener { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
|
||
listeners := make([]*listener.UDPListener, len(c.udpListeners)) | ||
copy(listeners, c.udpListeners) | ||
|
||
return listeners | ||
} | ||
|
||
func (c *Cyn) AddTCPSender(tcpSender *sender.TCPSender) { | ||
c.mu.Lock() | ||
c.tcpSenders = append(c.tcpSenders, tcpSender) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *Cyn) AddUDPSender(udpSender *sender.UDPSender) { | ||
c.mu.Lock() | ||
c.udpSenders = append(c.udpSenders, udpSender) | ||
c.mu.Unlock() | ||
} | ||
|
||
func (c *Cyn) AddHTTPServer(addr string) { | ||
server := httpserver.NewServer(addr, c) | ||
|
||
c.mu.Lock() | ||
c.httpServer = server | ||
c.mu.Unlock() | ||
} |
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,51 @@ | ||
package cyn | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func (c *Cyn) Run() error { | ||
eg := errgroup.Group{} | ||
|
||
listenOrSendCount := 0 | ||
|
||
c.mu.RLock() | ||
|
||
if c.httpServer != nil { | ||
eg.Go(c.httpServer.ServeAndListen) | ||
} | ||
|
||
for _, tcpListener := range c.tcpListeners { | ||
listenOrSendCount++ | ||
listen := tcpListener.Listen | ||
eg.Go(listen) | ||
} | ||
|
||
for _, udpListener := range c.udpListeners { | ||
listenOrSendCount++ | ||
listen := udpListener.Listen | ||
eg.Go(listen) | ||
} | ||
|
||
for _, tcpSender := range c.tcpSenders { | ||
listenOrSendCount++ | ||
run := tcpSender.Run | ||
eg.Go(run) | ||
} | ||
|
||
for _, udpSender := range c.udpSenders { | ||
listenOrSendCount++ | ||
run := udpSender.Run | ||
eg.Go(run) | ||
} | ||
|
||
c.mu.RUnlock() | ||
|
||
if listenOrSendCount == 0 { | ||
return fmt.Errorf("no listeners or senders specified") | ||
} | ||
|
||
return eg.Wait() | ||
} |
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,32 @@ | ||
package httpserver | ||
|
||
type ListenerInfo struct { | ||
Address string | ||
} | ||
|
||
type OverallStatus struct { | ||
TCPListeners []ListenerInfo | ||
UDPListeners []ListenerInfo | ||
} | ||
|
||
type IndexData struct { | ||
OverallStatus | ||
} | ||
|
||
func overallStatusFromGetter(getter OverallStatusGetter) OverallStatus { | ||
s := OverallStatus{} | ||
|
||
for _, tcpListener := range getter.TCPListeners() { | ||
s.TCPListeners = append(s.TCPListeners, ListenerInfo{ | ||
Address: tcpListener.Addr(), | ||
}) | ||
} | ||
|
||
for _, udpListener := range getter.UDPListeners() { | ||
s.UDPListeners = append(s.UDPListeners, ListenerInfo{ | ||
Address: udpListener.Addr(), | ||
}) | ||
} | ||
|
||
return s | ||
} |
Oops, something went wrong.