| 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) |
| 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) |
| mux.HandleFunc("/about", s.handleAbout) |
| mux.HandleFunc("/about_en", s.handleAboutEn) |
| } |