Skip to content

Instantly share code, notes, and snippets.

@rossdylan
Last active January 1, 2016 19:29
Show Gist options
  • Save rossdylan/8190941 to your computer and use it in GitHub Desktop.
Save rossdylan/8190941 to your computer and use it in GitHub Desktop.
Check to see if Harlan's fucking bogosort from hell is still running. I probably didn't need to use gorilla/mux but who the hell cares.
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<title>Is Harlan's BogoSort Running?</title>
<style> h1 { font-size: 100px; } </style>
</head>
<body>
<div class="page-header">
<h1 class="text-center"> Harlan's Bogo Sort is: {{.Status}}</h1>
<h2 class="text-center"> {{.Hours}}</h2>
</div>
</body>
</html>
package main
import "net/http"
import "github.com/gorilla/mux"
import "os/exec"
import "bytes"
import "log"
import "html/template"
import "strings"
import "fmt"
type Output struct {
Status string
Hours string
}
func GetBogoPID() string {
cmd := exec.Command("pgrep", "BogoSort")
var stdout bytes.Buffer
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
return ""
} else {
return strings.TrimSpace(stdout.String())
}
}
func GetRunTime(pid string) string {
//11-20:52:29
cmd := exec.Command("ps", "-p", pid, "-o", "etime=")
var stdout bytes.Buffer
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
panic(err)
}
outstr := stdout.String()
daysSplit := strings.Split(outstr, "-")
days := daysSplit[0]
timeSplit := strings.Split(daysSplit[1], ":")
return fmt.Sprintf("%s Days %s Hours %s Minutes and %s Seconds", days, timeSplit[0], timeSplit[1], timeSplit[2])
}
func BogoHandler(writer http.ResponseWriter, req *http.Request) {
temp, terr := template.ParseFiles("bogo.template")
if terr != nil {
panic(terr)
}
pid := GetBogoPID()
out := Output{"",""}
if pid == "" {
out.Status = "Not Running"
out.Hours = "Thank Fucking God, Its Over"
} else {
out.Status = "Running"
out.Hours = fmt.Sprintf("%s of 100%% Pure Fuck You", GetRunTime(pid))
}
eerr := temp.Execute(writer, out)
if eerr != nil {
panic(eerr)
}
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", BogoHandler)
log.Fatal(http.ListenAndServe(":9989", router))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment