diff --git a/examples/realtime-advanced/limit.go b/examples/realtime-advanced/limit.go deleted file mode 100644 index 022c936c..00000000 --- a/examples/realtime-advanced/limit.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "log" - - "github.com/gin-gonic/gin" - "github.com/manucorporat/stats" -) - -var ips = stats.New() - -func ratelimit(c *gin.Context) { - ip := c.ClientIP() - value := uint64(ips.Add(ip, 1)) - if value >= 1000 { - if value%1000 == 0 { - log.Printf("BlockedIP:%s Requests:%d\n", ip, value) - } - c.AbortWithStatus(401) - } -} diff --git a/examples/realtime-advanced/main.go b/examples/realtime-advanced/main.go index a9cce4c6..76022b26 100644 --- a/examples/realtime-advanced/main.go +++ b/examples/realtime-advanced/main.go @@ -2,9 +2,7 @@ package main import ( "fmt" - "io" "runtime" - "time" "github.com/gin-gonic/gin" "github.com/manucorporat/stats" @@ -13,86 +11,31 @@ import ( var messages = stats.New() func main() { + ConfigRuntime() + StartWorkers() + StartGin() +} + +func ConfigRuntime() { nuCPU := runtime.NumCPU() runtime.GOMAXPROCS(nuCPU) fmt.Printf("Running with %d CPUs\n", nuCPU) +} +func StartWorkers() { + go statsWorker() +} + +func StartGin() { gin.SetMode(gin.ReleaseMode) - router := gin.New() - router.Use(ratelimit, gin.Recovery(), gin.Logger()) - + router := gin.Default() router.LoadHTMLGlob("resources/*.templ.html") router.Static("/static", "resources/static") router.GET("/", index) router.GET("/room/:roomid", roomGET) router.POST("/room-post/:roomid", roomPOST) - //router.DELETE("/room/:roomid", roomDELETE) router.GET("/stream/:roomid", streamRoom) router.Run("127.0.0.1:8080") } - -func index(c *gin.Context) { - c.Redirect(301, "/room/hn") -} - -func roomGET(c *gin.Context) { - roomid := c.ParamValue("roomid") - userid := c.FormValue("nick") - if len(userid) > 13 { - userid = userid[0:12] + "..." - } - c.HTML(200, "room_login.templ.html", gin.H{ - "roomid": roomid, - "nick": userid, - "timestamp": time.Now().Unix(), - }) - -} - -func roomPOST(c *gin.Context) { - roomid := c.ParamValue("roomid") - nick := c.FormValue("nick") - message := c.PostFormValue("message") - - if len(message) > 200 || len(nick) > 13 { - c.JSON(400, gin.H{ - "status": "failed", - "error": "the message or nickname is too long", - }) - return - } - - post := gin.H{ - "nick": nick, - "message": message, - } - messages.Add("inbound", 1) - room(roomid).Submit(post) - c.JSON(200, post) -} - -func roomDELETE(c *gin.Context) { - roomid := c.ParamValue("roomid") - deleteBroadcast(roomid) -} - -func streamRoom(c *gin.Context) { - roomid := c.ParamValue("roomid") - listener := openListener(roomid) - ticker := time.NewTicker(1 * time.Second) - defer closeListener(roomid, listener) - defer ticker.Stop() - - c.Stream(func(w io.Writer) bool { - select { - case msg := <-listener: - messages.Add("outbound", 1) - c.SSEvent("message", msg) - case <-ticker.C: - c.SSEvent("stats", Stats()) - } - return true - }) -} diff --git a/examples/realtime-advanced/resources/room_login.templ.html b/examples/realtime-advanced/resources/room_login.templ.html index 67a8e2e9..8a4be68f 100644 --- a/examples/realtime-advanced/resources/room_login.templ.html +++ b/examples/realtime-advanced/resources/room_login.templ.html @@ -4,7 +4,7 @@ - Login in Room "{{.roomid}}" + Server-Sent Events. Room "{{.roomid}}" @@ -32,6 +32,15 @@ +