diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..9051185 --- /dev/null +++ b/handlers.go @@ -0,0 +1,7 @@ +package main + +import ( + "fmt" + "net/http" +) + diff --git a/maintenance.go b/maintenance.go new file mode 100644 index 0000000..1406f38 --- /dev/null +++ b/maintenance.go @@ -0,0 +1,71 @@ +package main + +import ( + "log" + "io/ioutil" + "time" + "strings" +) + +func allfiles(dirpath) []string { + files, err := ioutil.ReadDir(dirpath) + if (err != nil) { + log.Printf("Error reading directory %s: %s", dirpath, err) + return [] + } + return files +} + +// maintenance runs +func tidy() { + // Skip if we've been disabled + if exists(statePath("disabled")) { + log.Print("disabled file found, suspending maintenance") + return + } + + // Skip if we've expired + untilspec, _ := ioutil.ReadFile(statePath("until")) + until, err := time.Parse(time.RFC3339, string(untilspec)) + if err == nil { + if until.Before(time.Now()) { + log.Print("until file time reached, suspending maintenance") + return + } + } + + log.Print("Hello, I'm maintaining!") + + // + // Get current list of categories + // + newCategories := []string{} + for f := range(allfiles(mothPath("packages"))) { + filename := f.Name() + filepath := mothPath(path.Join("packages", filename)) + if ! strings.HasSuffix(filename, ".mb") { + continue + } + + categoryName := strings.TrimSuffix(filename, ".mb") + newCategories = append(newCategories, categoryName) + + // Uncompress into cache directory + cacheMothball(filepath, categoryName) + } + categories = newCategories + + // + // Collect new points + // + for f := range allfiles(statePath("points.new")) { + + } +} + +// maintenance is the goroutine that runs a periodic maintenance task +func maintenance() { + for ;; time.Sleep(maintenanceInterval) { + tidy() + } +} diff --git a/mothd.go b/mothd.go new file mode 100644 index 0000000..8d6c08c --- /dev/null +++ b/mothd.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "html" + "io/ioutil" + "log" + "net/http" + "os" + "path" + "strings" + "time" +) + +var basePath = "." +var maintenanceInterval = 20 * time.Second +var categories = []string{} + +func mooHandler(w http.ResponseWriter, req *http.Request) { + moo := req.FormValue("moo") + fmt.Fprintf(w, "Hello, %q. %s", html.EscapeString(req.URL.Path), html.EscapeString(moo)) +} + +func rootHandler(w http.ResponseWriter, req *http.Request) { + if req.URL.Path != "/" { + http.NotFound(w, req) + return + } +} + +func mothPath(filename string) string { + return path.Join(basePath, filename) +} + +func statePath(filename string) string { + return path.Join(basePath, "state", filename) +} + +func exists(filename string) bool { + if _, err := os.Stat(filename); os.IsNotExist(err) { + return false; + } + return true; +} + +func main() { + log.Print("Sup") + go maintenance(); + http.HandleFunc("/", rootHandler) + http.HandleFunc("/moo/", mooHandler) + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +// docker run --rm -it -p 5880:8080 -v $HOME:$HOME:ro -w $(pwd) golang go run mothd.go