Last active
July 11, 2018 18:01
-
-
Save shonenada/288f3a50ccaae66da0cbae4d8956b2fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"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