Skip to content

Instantly share code, notes, and snippets.

@ritesh
Created January 8, 2015 23:16
Show Gist options
  • Save ritesh/61edaf542324f942d15a to your computer and use it in GitHub Desktop.
Save ritesh/61edaf542324f942d15a to your computer and use it in GitHub Desktop.
Toy fortune server
package main
import (
"bytes"
"log"
"net/http"
"os/exec"
)
func fortune() []byte {
//Expects the fortune command in $PATH
cmd := exec.Command("fortune")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Printf(err.Error())
return []byte("THE SERVER IS HAVING A BAD DAY!")
}
return out.Bytes()
//fmt.Printf("%s", out.String())
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(fortune())
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment