hswaw/site: serve static
This adds a minimum serving Go binary, and static/template file
embedding.
The templates are not yet served or even loaded. The static files are
served at /static/..., eg.:
$ curl 127.0.0.1:8080/static/mapka.png
Change-Id: Iedd8696db2c2e5d434dc2e7fbd0199d0f6ee5fff
diff --git a/hswaw/site/main.go b/hswaw/site/main.go
new file mode 100644
index 0000000..b68482a
--- /dev/null
+++ b/hswaw/site/main.go
@@ -0,0 +1,69 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "mime"
+ "net/http"
+ "strings"
+
+ "code.hackerspace.pl/hscloud/go/mirko"
+ "github.com/golang/glog"
+
+ "code.hackerspace.pl/hscloud/hswaw/site/static"
+)
+
+var (
+ flagSitePublic string
+)
+
+type service struct {
+}
+
+func main() {
+ flag.StringVar(&flagSitePublic, "site_public", "0.0.0.0:8080", "Address at which to serve public HTTP requests")
+ flag.Parse()
+
+ mi := mirko.New()
+ if err := mi.Listen(); err != nil {
+ glog.Exitf("Listen failed: %v", err)
+ }
+
+ s := &service{}
+
+ mux := http.NewServeMux()
+ s.registerHTTP(mux)
+
+ go func() {
+ glog.Infof("Listening for public HTTP at %v", flagSitePublic)
+ if err := http.ListenAndServe(flagSitePublic, mux); err != nil {
+ glog.Exit(err)
+ }
+ }()
+
+ if err := mi.Serve(); err != nil {
+ glog.Exitf("Serve failed: %v", err)
+ }
+
+ <-mi.Done()
+}
+
+func (s *service) handleHTTPStatic(w http.ResponseWriter, r *http.Request) {
+ path := strings.TrimPrefix(r.URL.Path, "/")
+
+ staticPath := fmt.Sprintf("hswaw/site/%s", path)
+ glog.Infof("%q", staticPath)
+ if data, ok := static.Data[staticPath]; ok {
+ parts := strings.Split(path, ".")
+ ext := fmt.Sprintf(".%s", parts[len(parts)-1])
+ t := mime.TypeByExtension(ext)
+ w.Header().Set("Content-Type", t)
+ w.Write(data)
+ } else {
+ http.NotFound(w, r)
+ }
+}
+
+func (s *service) registerHTTP(mux *http.ServeMux) {
+ mux.HandleFunc("/static/", s.handleHTTPStatic)
+}