Skip to content

Instantly share code, notes, and snippets.

@shonenada
Last active July 11, 2018 18:01
Show Gist options
  • Save shonenada/288f3a50ccaae66da0cbae4d8956b2fa to your computer and use it in GitHub Desktop.
Save shonenada/288f3a50ccaae66da0cbae4d8956b2fa to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/hpcloud/tail"
)
func write(w http.ResponseWriter, s string) {
flusher, ok := w.(http.Flusher)
if !ok {
panic("Expected http.ResponseWriter to be an http.CloseNotifier")
}
fmt.Fprintf(w, s)
flusher.Flush()
}
func stream(w http.ResponseWriter, r *http.Request) {
t, _ := tail.TailFile(os.Args[1], tail.Config{Follow: true})
for line := range t.Lines {
fmt.Println(line.Text)
write(w, line.Text+"\n")
}
}
func main() {
http.HandleFunc("/", stream)
err := http.ListenAndServe(":53290", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment