forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'routing_refactor' into next
- Loading branch information
Showing
24 changed files
with
1,442 additions
and
502 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
FROM golang:1.8 | ||
|
||
# Copy the runtime dockerfile into the context as Dockerfile | ||
COPY . /go/src/github.com/play-with-docker/play-with-docker | ||
|
||
WORKDIR /go/src/github.com/play-with-docker/play-with-docker | ||
|
||
RUN go get -v -d ./... | ||
|
||
RUN ssh-keygen -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key >/dev/null | ||
|
||
WORKDIR /go/src/github.com/play-with-docker/play-with-docker/router/l2 | ||
|
||
RUN CGO_ENABLED=0 go build -a -installsuffix nocgo -o /go/bin/play-with-docker-l2 . | ||
|
||
|
||
FROM alpine | ||
|
||
RUN apk --update add ca-certificates | ||
RUN mkdir /app | ||
|
||
COPY --from=0 /go/bin/play-with-docker-l2 /app/play-with-docker-l2 | ||
COPY --from=0 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_rsa_key | ||
|
||
WORKDIR /app | ||
CMD ["./play-with-docker-l2", "-ssh_key_path", "/etc/ssh/ssh_host_rsa_key"] | ||
|
||
EXPOSE 22 | ||
EXPOSE 53 | ||
EXPOSE 443 |
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,25 @@ | ||
package event | ||
|
||
type EventType string | ||
|
||
func (e EventType) String() string { | ||
return string(e) | ||
} | ||
|
||
const INSTANCE_VIEWPORT_RESIZE EventType = "instance viewport resize" | ||
const INSTANCE_DELETE EventType = "instance delete" | ||
const INSTANCE_NEW EventType = "instance new" | ||
const INSTANCE_STATS EventType = "instance stats" | ||
const INSTANCE_TERMINAL_OUT EventType = "instance terminal out" | ||
const SESSION_END EventType = "session end" | ||
const SESSION_READY EventType = "session ready" | ||
const SESSION_BUILDER_OUT EventType = "session builder out" | ||
|
||
type Handler func(sessionId string, args ...interface{}) | ||
type AnyHandler func(eventType EventType, sessionId string, args ...interface{}) | ||
|
||
type EventApi interface { | ||
Emit(name EventType, sessionId string, args ...interface{}) | ||
On(name EventType, handler Handler) | ||
OnAny(handler AnyHandler) | ||
} |
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,47 @@ | ||
package event | ||
|
||
import "sync" | ||
|
||
type localBroker struct { | ||
sync.Mutex | ||
|
||
handlers map[EventType][]Handler | ||
anyHandlers []AnyHandler | ||
} | ||
|
||
func NewLocalBroker() *localBroker { | ||
return &localBroker{handlers: map[EventType][]Handler{}, anyHandlers: []AnyHandler{}} | ||
} | ||
|
||
func (b *localBroker) On(name EventType, handler Handler) { | ||
b.Lock() | ||
defer b.Unlock() | ||
|
||
if b.handlers[name] == nil { | ||
b.handlers[name] = []Handler{} | ||
} | ||
b.handlers[name] = append(b.handlers[name], handler) | ||
} | ||
|
||
func (b *localBroker) OnAny(handler AnyHandler) { | ||
b.Lock() | ||
defer b.Unlock() | ||
|
||
b.anyHandlers = append(b.anyHandlers, handler) | ||
} | ||
|
||
func (b *localBroker) Emit(name EventType, sessionId string, args ...interface{}) { | ||
go func() { | ||
b.Lock() | ||
defer b.Unlock() | ||
|
||
for _, handler := range b.anyHandlers { | ||
handler(name, sessionId, args...) | ||
} | ||
if b.handlers[name] != nil { | ||
for _, handler := range b.handlers[name] { | ||
handler(sessionId, args...) | ||
} | ||
} | ||
}() | ||
} |
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,60 @@ | ||
package event | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLocalBroker_On(t *testing.T) { | ||
broker := NewLocalBroker() | ||
|
||
called := 0 | ||
receivedSessionId := "" | ||
receivedArgs := []interface{}{} | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
|
||
broker.On(INSTANCE_NEW, func(sessionId string, args ...interface{}) { | ||
called++ | ||
receivedSessionId = sessionId | ||
receivedArgs = args | ||
wg.Done() | ||
}) | ||
broker.Emit(SESSION_READY, "1") | ||
broker.Emit(INSTANCE_NEW, "2", "foo", "bar") | ||
|
||
wg.Wait() | ||
|
||
assert.Equal(t, 1, called) | ||
assert.Equal(t, "2", receivedSessionId) | ||
assert.Equal(t, []interface{}{"foo", "bar"}, receivedArgs) | ||
} | ||
|
||
func TestLocalBroker_OnAny(t *testing.T) { | ||
broker := NewLocalBroker() | ||
|
||
var receivedEvent EventType | ||
receivedSessionId := "" | ||
receivedArgs := []interface{}{} | ||
|
||
wg := sync.WaitGroup{} | ||
wg.Add(1) | ||
|
||
broker.OnAny(func(eventType EventType, sessionId string, args ...interface{}) { | ||
receivedSessionId = sessionId | ||
receivedArgs = args | ||
receivedEvent = eventType | ||
wg.Done() | ||
}) | ||
broker.Emit(SESSION_READY, "1") | ||
|
||
wg.Wait() | ||
|
||
var expectedArgs []interface{} | ||
assert.Equal(t, SESSION_READY, receivedEvent) | ||
assert.Equal(t, "1", receivedSessionId) | ||
assert.Equal(t, expectedArgs, receivedArgs) | ||
} |
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
Oops, something went wrong.