changeset 99:3c583603f261

start switch to goweb, for improved incoming payload handling Ignore-this: 18f1a3f3fef04a22a1d4e84a6e5e09ca
author drewp@bigasterisk.com
date Sat, 31 Aug 2013 11:24:09 -0700
parents d620e60bd397
children e53b06257173
files service/laundry/laundry.go service/laundry/makefile service/laundry/static/gui.js
diffstat 3 files changed, 62 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/service/laundry/laundry.go	Sat Aug 31 10:39:04 2013 -0700
+++ b/service/laundry/laundry.go	Sat Aug 31 11:24:09 2013 -0700
@@ -6,9 +6,13 @@
 	"net/http"
 	"strconv"
 	"time"
+	"net"
+	"os"
 	"encoding/json"
-	"github.com/bmizerany/pat"
+	"os/signal"
 	"github.com/mrmorphic/hwio"
+	"github.com/stretchr/goweb"
+	"github.com/stretchr/goweb/context"
 )
 
 /*
@@ -98,19 +102,15 @@
 func main() {
 	pins := SetupIo()
 
-	m := pat.New()
+	goweb.MapStatic("/static", "static")
+
+	// this one needs to fail if the hardware is broken in
+	// any way that we can determine, though I'm not sure
+	// what that will mean on rpi
+	goweb.MapStaticFile("/", "index.html")
 	
-	m.Get("/", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
-		// this one needs to fail if the hardware is broken in
-		// any way that we can determine, though I'm not sure
-		// what that will mean on rpi
-		http.ServeFile(w, r, "index.html")
-	}));
-
-	m.Get("/static/:any", http.FileServer(http.Dir("./")));
-
-	m.Get("/status", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
-		jsonEncode := json.NewEncoder(w)
+	goweb.Map("GET", "/status", func(c context.Context) error {
+		jsonEncode := json.NewEncoder(c.HttpResponseWriter())
 		jsonEncode.Encode(map[string]int{
 			"motion": DigitalRead(pins.InMotion),
 			"switch1": DigitalRead(pins.InSwitch1),
@@ -120,8 +120,10 @@
 			"led": pins.LastOutLed,
 			"strike": pins.LastOutStrike,
 		})
-	}));
-	
+		return nil
+	})
+
+	/*
 	m.Put("/led", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
 		body, err := ioutil.ReadAll(r.Body)
 		if err != nil {
@@ -182,11 +184,49 @@
 		// queue a beep
 		http.Error(w, "", http.StatusAccepted)
 	}))
+*/
+
+	address := ":8081"
 	
-	http.Handle("/", m)
-	log.Printf("Listening on port 8081")
-	err := http.ListenAndServe(":8081", nil)
-	if err != nil {
-		log.Fatal("ListenAndServe: ", err)
+	s := &http.Server{
+		Addr:           address,
+		Handler:        goweb.DefaultHttpHandler(),
+		ReadTimeout:    10 * time.Second,
+		WriteTimeout:   10 * time.Second,
+		MaxHeaderBytes: 1 << 20,
+	}
+	
+	log.Printf("Listening on port %s", address)
+	listener, listenErr := net.Listen("tcp", address)
+
+	log.Printf("%s", goweb.DefaultHttpHandler())
+	
+	if listenErr != nil {
+		log.Fatalf("Could not listen: %s", listenErr)
 	}
+
+	c := make(chan os.Signal, 1)
+	signal.Notify(c, os.Interrupt)
+	go func() {
+		for _ = range c {
+
+			// sig is a ^C, handle it
+
+			// stop the HTTP server
+			log.Print("Stopping the server...")
+			listener.Close()
+
+			/*
+			   Tidy up and tear down
+			*/
+			log.Print("Tearing down...")
+
+			// TODO: tidy code up here
+
+			log.Fatal("Finished - bye bye.  ;-)")
+
+		}
+	}()
+	log.Fatalf("Error in Serve: %s", s.Serve(listener))
+
 }
--- a/service/laundry/makefile	Sat Aug 31 10:39:04 2013 -0700
+++ b/service/laundry/makefile	Sat Aug 31 11:24:09 2013 -0700
@@ -7,4 +7,4 @@
 
 deps:
 	GOPATH=`pwd` /opt/go/bin/go get github.com/mrmorphic/hwio
-	GOPATH=`pwd` /opt/go/bin/go get github.com/bmizerany/pat
+	GOPATH=`pwd` /opt/go/bin/go get github.com/stretchr/goweb
--- a/service/laundry/static/gui.js	Sat Aug 31 10:39:04 2013 -0700
+++ b/service/laundry/static/gui.js	Sat Aug 31 11:24:09 2013 -0700
@@ -1,5 +1,6 @@
 'use strict';
 
+
 function Ctrl($scope, $http) {
     function refresh() {
         $http.get("status").success(function (data) {