started the bg server stuff

master
Jordan Orelli 8 years ago
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
}

@ -4,8 +4,11 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime"
"strings" "strings"
"github.com/jordanorelli/dws/bg"
"github.com/jordanorelli/dws/events"
"github.com/jordanorelli/dws/ui" "github.com/jordanorelli/dws/ui"
) )
@ -22,18 +25,17 @@ func exit(status int, t string, args ...interface{}) {
} }
func main() { func main() {
runtime.LockOSThread()
log.Println("Creating new Desktop UI") log.Println("Creating new Desktop UI")
desktop := ui.Desktop() desktop := ui.Desktop()
c := make(chan ui.Event, 1) uiEvents := make(chan events.UserEvent, 1)
go func() { bgEvents := make(chan events.BackgroundEvent, 1)
for e := range c {
log.Printf("UI Event: %v\n", e) go bg.Run(bgEvents, uiEvents)
}
}()
log.Println("Running Desktop UI") if err := desktop.Run(uiEvents, bgEvents); err != nil {
if err := desktop.Run(c); err != nil {
exit(1, "UI Error: %v", err) exit(1, "UI Error: %v", err)
} }
} }

@ -6,6 +6,7 @@
@property (nonatomic, strong) NSOpenPanel *selectDirectoryPanel; @property (nonatomic, strong) NSOpenPanel *selectDirectoryPanel;
@property (nonatomic, strong) NSButton *selectDirectoryButton; @property (nonatomic, strong) NSButton *selectDirectoryButton;
@property (nonatomic, strong) NSTextField *selectedDirectoryText;
@end @end
@ -36,7 +37,11 @@
[self.selectDirectoryButton setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.selectDirectoryButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.selectDirectoryButton]; [self.view addSubview:self.selectDirectoryButton];
// setup button constraints // create label to show selected directory
self.selectedDirectoryText = [NSTextField labelWithString:@"(none)"];
[self.view addSubview:self.selectedDirectoryText];
// setup constraints
[self.selectDirectoryButton.rightAnchor [self.selectDirectoryButton.rightAnchor
constraintEqualToAnchor:self.view.rightAnchor constraintEqualToAnchor:self.view.rightAnchor
constant:-8.0].active = YES; constant:-8.0].active = YES;
@ -53,8 +58,8 @@
return; return;
} }
NSURL *selected = [[self.selectDirectoryPanel URLs] objectAtIndex:0]; NSURL *selected = [[self.selectDirectoryPanel URLs] objectAtIndex:0];
NSString *path = [selected path]; char *path = (char *)[[selected path] UTF8String];
selectDirectory((GoString){path.UTF8String, path.length}); selectDirectory(path);
}]; }];
} }

@ -1,18 +1,9 @@
package ui package ui
type UI interface { import (
Run(chan Event) error "github.com/jordanorelli/dws/events"
} )
type Event interface {
isUIEvent()
}
type event struct{} type UI interface {
Run(chan events.UserEvent, chan events.BackgroundEvent) error
func (e event) isUIEvent() {}
type SelectDirectoryEvent struct {
Path string
event
} }

@ -2,7 +2,8 @@ package ui
import ( import (
"log" "log"
"runtime"
"github.com/jordanorelli/dws/events"
) )
/* /*
@ -19,24 +20,27 @@ func Desktop() UI {
return desktopUI return desktopUI
} }
log.Println("Creating new cocoaUI") log.Println("Creating new Cocoa UI")
runtime.LockOSThread()
C.Initialize() C.Initialize()
desktopUI = new(cocoaUI) desktopUI = new(cocoaUI)
return desktopUI return desktopUI
} }
type cocoaUI struct { type cocoaUI struct {
out chan Event in chan events.BackgroundEvent
out chan events.UserEvent
} }
func (ui *cocoaUI) Run(out chan Event) error { func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent) error {
log.Println("Running Desktop UI")
ui.in = in
ui.out = out ui.out = out
C.Run() C.Run()
return nil return nil
} }
//export selectDirectory //export selectDirectory
func selectDirectory(path string) { func selectDirectory(cpath *C.char) {
desktopUI.out <- SelectDirectoryEvent{Path: path} path := C.GoString(cpath)
desktopUI.out <- events.UserSelectedDirectory{Path: path}
} }

Loading…
Cancel
Save