2016-10-20 20:30:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func request(c echo.Context) error {
|
2016-10-20 20:51:15 +02:00
|
|
|
req := c.Request()
|
2016-10-20 20:30:53 +02:00
|
|
|
format := "<pre><strong>Request Information</strong>\n\n<code>Protocol: %s\nHost: %s\nRemote Address: %s\nMethod: %s\nPath: %s\n</code></pre>"
|
|
|
|
return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func stream(c echo.Context) error {
|
2016-10-20 20:51:15 +02:00
|
|
|
res := c.Response()
|
|
|
|
gone := res.CloseNotify()
|
2016-10-20 20:30:53 +02:00
|
|
|
res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
fmt.Fprint(res, "<pre><strong>Clock Stream</strong>\n\n<code>")
|
|
|
|
for {
|
|
|
|
fmt.Fprintf(res, "%v\n", time.Now())
|
2016-10-20 20:51:15 +02:00
|
|
|
res.Flush()
|
2016-10-20 20:30:53 +02:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-gone:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
|
|
|
e.GET("/request", request)
|
|
|
|
e.GET("/stream", stream)
|
2016-10-20 20:51:15 +02:00
|
|
|
e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem"))
|
2016-10-20 20:30:53 +02:00
|
|
|
}
|