-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmain.go
49 lines (44 loc) · 1.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.Use(
func(c *gin.Context) {
c.Writer.Header().Add("Vary", "Origin")
},
gzip.Gzip(
gzip.DefaultCompression,
gzip.WithExcludedPaths([]string{"/ping2"}),
))
r.Use(func(c *gin.Context) {
log.Println("Request received")
c.Next()
log.Println("Response sent")
})
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})
r.GET("/ping2", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})
r.GET("/stream", func(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Connection", "keep-alive")
for i := 0; i < 10; i++ {
fmt.Fprintf(c.Writer, "id: %d\ndata: tick %d\n\n", i, time.Now().Unix())
c.Writer.Flush()
time.Sleep(1 * time.Second)
}
})
// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}