Echo!
This commit is contained in:
@ -3,11 +3,9 @@ package api
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/uptrace/bunrouter"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Ping(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Pong!"))
|
||||
return nil
|
||||
func Ping(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "Pong!")
|
||||
}
|
||||
|
24
api/sse.go
24
api/sse.go
@ -3,28 +3,26 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bunrouter"
|
||||
"github.com/labstack/echo/v4"
|
||||
"goth.stack/lib"
|
||||
)
|
||||
|
||||
func SSE(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
queryParams := req.URL.Query()
|
||||
channel := queryParams.Get("channel")
|
||||
func SSE(c echo.Context) error {
|
||||
channel := c.QueryParam("channel")
|
||||
if channel == "" {
|
||||
channel = "default"
|
||||
}
|
||||
|
||||
// Use the request context, which is cancelled when the client disconnects
|
||||
ctx := req.Context()
|
||||
ctx := c.Request().Context()
|
||||
|
||||
pubsub, _ := lib.Subscribe(lib.RedisClient, channel)
|
||||
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream")
|
||||
c.Response().Header().Set(echo.HeaderConnection, "keep-alive")
|
||||
c.Response().Header().Set(echo.HeaderCacheControl, "no-cache")
|
||||
|
||||
// Create a ticker that fires every 15 seconds
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
@ -37,10 +35,10 @@ func SSE(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
// Every 30 seconds, send a comment to keep the connection alive
|
||||
if _, err := w.Write([]byte(": keep-alive\n\n")); err != nil {
|
||||
if _, err := c.Response().Write([]byte(": keep-alive\n\n")); err != nil {
|
||||
return err
|
||||
}
|
||||
w.(http.Flusher).Flush()
|
||||
c.Response().Flush()
|
||||
default:
|
||||
// Handle incoming messages as before
|
||||
msg, err := pubsub.ReceiveMessage(ctx)
|
||||
@ -50,11 +48,11 @@ func SSE(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
}
|
||||
|
||||
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
|
||||
if _, err := w.Write([]byte(data)); err != nil {
|
||||
if _, err := c.Response().Write([]byte(data)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w.(http.Flusher).Flush()
|
||||
c.Response().Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +1,25 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/uptrace/bunrouter"
|
||||
"github.com/labstack/echo/v4"
|
||||
"goth.stack/lib"
|
||||
)
|
||||
|
||||
func SSEDemoSend(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
// Get query parameters
|
||||
queryParams := req.URL.Query()
|
||||
|
||||
// Get channel from query parameters
|
||||
channel := queryParams.Get("channel")
|
||||
func SSEDemoSend(c echo.Context) error {
|
||||
channel := c.QueryParam("channel")
|
||||
if channel == "" {
|
||||
channel = "default"
|
||||
}
|
||||
|
||||
// Get message from query parameters, form value, or request body
|
||||
message := queryParams.Get("message")
|
||||
message := c.QueryParam("message")
|
||||
if message == "" {
|
||||
message = req.PostFormValue("message")
|
||||
message = c.FormValue("message")
|
||||
if message == "" {
|
||||
var body map[string]string
|
||||
err := json.NewDecoder(req.Body).Decode(&body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
if err := c.Bind(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
message = body["message"]
|
||||
@ -34,18 +27,11 @@ func SSEDemoSend(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
}
|
||||
|
||||
if message == "" {
|
||||
errMsg := map[string]string{"error": "message parameter is required"}
|
||||
errMsgBytes, _ := json.Marshal(errMsg)
|
||||
http.Error(w, string(errMsgBytes), http.StatusBadRequest)
|
||||
return nil
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "message parameter is required"})
|
||||
}
|
||||
|
||||
// Send message
|
||||
lib.SendSSE("default", message)
|
||||
|
||||
statusMsg := map[string]string{"status": "message sent"}
|
||||
statusMsgBytes, _ := json.Marshal(statusMsg)
|
||||
w.Write(statusMsgBytes)
|
||||
|
||||
return nil
|
||||
return c.JSON(http.StatusOK, map[string]string{"status": "message sent"})
|
||||
}
|
||||
|
Reference in New Issue
Block a user