started the bg server stuff
parent
8431fc1f27
commit
a68425d1fb
@ -0,0 +1,30 @@
|
||||
package bg
|
||||
|
||||
import (
|
||||
"github.com/jordanorelli/dws/events"
|
||||
)
|
||||
|
||||
func Run(out chan events.BackgroundEvent, in chan events.UserEvent) {
|
||||
bg := &background{
|
||||
in: in,
|
||||
out: out,
|
||||
server: newServer(),
|
||||
}
|
||||
go bg.listen()
|
||||
bg.run()
|
||||
}
|
||||
|
||||
type background struct {
|
||||
in chan events.UserEvent
|
||||
out chan events.BackgroundEvent
|
||||
*server
|
||||
}
|
||||
|
||||
func (bg *background) run() {
|
||||
for e := range bg.in {
|
||||
switch v := e.(type) {
|
||||
case events.UserSelectedDirectory:
|
||||
bg.setRoot(v.Path)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package bg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
port int
|
||||
root string
|
||||
}
|
||||
|
||||
func newServer() *server {
|
||||
return &server{port: 8000}
|
||||
}
|
||||
|
||||
func (s *server) listen() {
|
||||
addr := fmt.Sprintf("0.0.0.0:%d", s.port)
|
||||
log.Printf("server listening on addr: %s\n", addr)
|
||||
if err := http.ListenAndServe(addr, s); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) setRoot(path string) {
|
||||
log.Printf("server setting root to %s\n", path)
|
||||
s.root = path
|
||||
}
|
||||
|
||||
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if s.root == "" {
|
||||
writeNotInitializedResponse(w)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "root: %s", s.root)
|
||||
}
|
||||
|
||||
func writeNotInitializedResponse(w http.ResponseWriter) {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprintln(w, "no root directory selected")
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package events
|
||||
|
||||
type BackgroundEvent interface {
|
||||
Event
|
||||
isBackgroundEvent()
|
||||
}
|
||||
|
||||
type backgroundEvent struct{ event }
|
||||
|
||||
func (e backgroundEvent) isBackgroundEvent() {}
|
@ -0,0 +1,9 @@
|
||||
package events
|
||||
|
||||
type Event interface {
|
||||
isEvent()
|
||||
}
|
||||
|
||||
type event struct{}
|
||||
|
||||
func (e event) isEvent() {}
|
@ -0,0 +1,15 @@
|
||||
package events
|
||||
|
||||
type UserEvent interface {
|
||||
Event
|
||||
isUserEvent()
|
||||
}
|
||||
|
||||
type userEvent struct{ event }
|
||||
|
||||
func (e userEvent) isUserEvent() {}
|
||||
|
||||
type UserSelectedDirectory struct {
|
||||
Path string
|
||||
userEvent
|
||||
}
|
@ -1,18 +1,9 @@
|
||||
package ui
|
||||
|
||||
type UI interface {
|
||||
Run(chan Event) error
|
||||
}
|
||||
|
||||
type Event interface {
|
||||
isUIEvent()
|
||||
}
|
||||
import (
|
||||
"github.com/jordanorelli/dws/events"
|
||||
)
|
||||
|
||||
type event struct{}
|
||||
|
||||
func (e event) isUIEvent() {}
|
||||
|
||||
type SelectDirectoryEvent struct {
|
||||
Path string
|
||||
event
|
||||
type UI interface {
|
||||
Run(chan events.UserEvent, chan events.BackgroundEvent) error
|
||||
}
|
||||
|
Loading…
Reference in New Issue