Updated to BunRouter

This commit is contained in:
2024-01-24 11:22:33 -07:00
parent ab639dec20
commit 8d3f803474
17 changed files with 387 additions and 160 deletions

View File

@ -3,9 +3,11 @@ package api
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/uptrace/bunrouter"
)
func Ping(c echo.Context) error {
return c.String(http.StatusOK, "Pong!")
func Ping(w http.ResponseWriter, req bunrouter.Request) error {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Pong!"))
return nil
}

View File

@ -3,26 +3,28 @@ package api
import (
"fmt"
"log"
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/uptrace/bunrouter"
"goth.stack/lib"
)
func SSEDemo(c echo.Context) error {
channel := c.QueryParam("channel")
func SSE(w http.ResponseWriter, req bunrouter.Request) error {
queryParams := req.URL.Query()
channel := queryParams.Get("channel")
if channel == "" {
channel = "default"
}
// Use the request context, which is cancelled when the client disconnects
ctx := c.Request().Context()
ctx := req.Context()
pubsub, _ := lib.Subscribe(lib.RedisClient, channel)
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")
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Cache-Control", "no-cache")
// Create a ticker that fires every 15 seconds
ticker := time.NewTicker(30 * time.Second)
@ -35,10 +37,10 @@ func SSEDemo(c echo.Context) error {
return nil
case <-ticker.C:
// Every 30 seconds, send a comment to keep the connection alive
if _, err := c.Response().Write([]byte(": keep-alive\n\n")); err != nil {
if _, err := w.Write([]byte(": keep-alive\n\n")); err != nil {
return err
}
c.Response().Flush()
w.(http.Flusher).Flush()
default:
// Handle incoming messages as before
msg, err := pubsub.ReceiveMessage(ctx)
@ -48,11 +50,11 @@ func SSEDemo(c echo.Context) error {
}
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
if _, err := c.Response().Write([]byte(data)); err != nil {
if _, err := w.Write([]byte(data)); err != nil {
return err
}
c.Response().Flush()
w.(http.Flusher).Flush()
}
}
}

View File

@ -1,25 +1,32 @@
package api
import (
"encoding/json"
"net/http"
"github.com/labstack/echo/v4"
"github.com/uptrace/bunrouter"
"goth.stack/lib"
)
func SSEDemoSend(c echo.Context) error {
channel := c.QueryParam("channel")
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")
if channel == "" {
channel = "default"
}
// Get message from query parameters, form value, or request body
message := c.QueryParam("message")
message := queryParams.Get("message")
if message == "" {
message = c.FormValue("message")
message = req.PostFormValue("message")
if message == "" {
var body map[string]string
if err := c.Bind(&body); err != nil {
err := json.NewDecoder(req.Body).Decode(&body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
message = body["message"]
@ -27,11 +34,18 @@ func SSEDemoSend(c echo.Context) error {
}
if message == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "message parameter is required"})
errMsg := map[string]string{"error": "message parameter is required"}
errMsgBytes, _ := json.Marshal(errMsg)
http.Error(w, string(errMsgBytes), http.StatusBadRequest)
return nil
}
// Send message
lib.SendSSE("default", message)
return c.JSON(http.StatusOK, map[string]string{"status": "message sent"})
statusMsg := map[string]string{"status": "message sent"}
statusMsgBytes, _ := json.Marshal(statusMsg)
w.Write(statusMsgBytes)
return nil
}