moth/src/mothd.go

75 lines
1.4 KiB
Go
Raw Normal View History

2018-09-14 18:24:48 -06:00
package main
import (
"flag"
"log"
2018-09-19 17:56:26 -06:00
"mime"
2018-09-14 18:24:48 -06:00
"net/http"
"time"
)
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP %s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func setup() error {
return nil
}
func main() {
2018-09-17 17:00:08 -06:00
base := flag.String(
"base",
"/",
"Base URL of this instance",
)
2018-09-14 18:24:48 -06:00
mothballDir := flag.String(
"mothballs",
"/mothballs",
2018-09-14 18:24:48 -06:00
"Path to read mothballs",
)
stateDir := flag.String(
"state",
"/state",
2018-09-14 18:24:48 -06:00
"Path to write state",
)
themeDir := flag.String(
"theme",
"/theme",
"Path to static theme resources (HTML, images, css, ...)",
2018-09-17 17:00:08 -06:00
)
2018-09-14 18:24:48 -06:00
maintenanceInterval := flag.Duration(
"maint",
2018-09-17 17:00:08 -06:00
20*time.Second,
2018-09-14 18:24:48 -06:00
"Maintenance interval",
)
listen := flag.String(
"listen",
2018-09-20 10:15:34 -06:00
":8080",
2018-09-14 18:24:48 -06:00
"[host]:port to bind and listen",
)
flag.Parse()
2018-09-17 17:00:08 -06:00
2018-09-14 18:24:48 -06:00
if err := setup(); err != nil {
log.Fatal(err)
}
ctx, err := NewInstance(*base, *mothballDir, *stateDir, *themeDir)
2018-09-14 18:24:48 -06:00
if err != nil {
log.Fatal(err)
}
ctx.BindHandlers(http.DefaultServeMux)
2018-09-19 17:56:26 -06:00
// Add some MIME extensions
// Doing this avoids decompressing a mothball entry twice per request
mime.AddExtensionType(".json", "application/json")
mime.AddExtensionType(".zip", "application/zip")
2018-09-14 18:24:48 -06:00
go ctx.Maintenance(*maintenanceInterval)
log.Printf("Listening on %s", *listen)
log.Fatal(http.ListenAndServe(*listen, logRequest(http.DefaultServeMux)))
}