Created
January 8, 2015 23:16
-
-
Save ritesh/61edaf542324f942d15a to your computer and use it in GitHub Desktop.
Toy fortune server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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