Skip to content

Commit

Permalink
test feed create
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Oct 12, 2024
1 parent a6e2fbe commit d0315d8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 25 deletions.
60 changes: 38 additions & 22 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path"
"time"

ws "github.com/gorilla/websocket"
"golang.org/x/exp/slog"

"github.com/Appboy/webpush-go"
Expand Down Expand Up @@ -188,14 +189,14 @@ func (api *ApiHandler) GetServer() *chi.Mux {
})

r.Route("/api/feeds", func(r chi.Router) {
r.Get("/{feedName}", api.feedHandlerFunc)
r.Post("/{feedName}", api.feedPostHandlerFunc)
r.Patch("/{feedName}", api.feedPatchHandlerFunc)
r.Post("/{feedName}/subscription", api.feedSubscriptionHandlerFunc)
r.Delete("/{feedName}/subscription", api.feedUnsubscribeHandlerFunc)
r.Delete("/{feedName}/items", api.feedItemEmptyHandlerFunc)
r.Get("/{feedName}/items/{itemName}", api.feedItemHandlerFunc)
r.Delete("/{feedName}/items/{itemName}", api.feedItemDeleteHandlerFunc)
r.Get("/{feedName}", api.feedGetFunc)
r.Post("/{feedName}", api.feedPostFunc)
r.Patch("/{feedName}", api.feedPatchFunc)
r.Post("/{feedName}/subscription", api.subscriptionPostFunc)
r.Delete("/{feedName}/subscription", api.subscriptionDeleteFunc)
r.Delete("/{feedName}/items", api.itemsDeleteFunc)
r.Get("/{feedName}/items/{itemName}", api.itemGetFunc)
r.Delete("/{feedName}/items/{itemName}", api.itemDeleteFunc)
})
r.Get("/*", RootHandlerFunc)

Expand All @@ -216,33 +217,46 @@ func (api *ApiHandler) feedWSHandler(w http.ResponseWriter, r *http.Request) {
feedName, _ := url.QueryUnescape(chi.URLParam(r, "feedName"))

if feedName == "" {
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
WriteError(w, http.StatusBadRequest, "Unable to obtain feed name")
return
}

_, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
var upgrader = ws.Upgrader{}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
// utils.CloseWithCodeAndMessage(w, 500, "Unable to upgrade WebSocket")
}
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
//WriteError(w, http.StatusNotFound, "feed '%s' not found", feedName)
c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusNotFound+4000, ""), time.Now().Add(time.Second))

case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
//WriteError(w, http.StatusUnauthorized, "Invalid secret")
c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusUnauthorized+4000, ""), time.Now().Add(time.Second))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
//WriteError(w, http.StatusInternalServerError, "Error while getting feed: %s", err.Error())
c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusInternalServerError+4000, ""), time.Now().Add(time.Second))
}
c.Close()
return
}

api.WebSocketManager.RunSocketForFeed(feedName, w, r)
}

func (api *ApiHandler) feedHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) feedGetFunc(w http.ResponseWriter, r *http.Request) {
hL.Logger.Debug("Feed API request", slog.Any("request_uri", r.RequestURI))

feedName, _ := url.QueryUnescape(chi.URLParam(r, "feedName"))

if feedName == "" {
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
WriteError(w, http.StatusBadRequest, "Unable to obtain feed name")
return
}

var f *feed.Feed
Expand All @@ -252,16 +266,18 @@ func (api *ApiHandler) feedHandlerFunc(w http.ResponseWriter, r *http.Request) {

if err != nil {
if errors.Is(err, feed.FeedErrorNotFound) {
f, err = feed.NewFeed(path.Join(api.BasePath, feedName))
_, err = feed.NewFeed(path.Join(api.BasePath, feedName))
if err != nil {
utils.CloseWithCodeAndMessage(w, 500, err.Error())
}
f, _ = api.FeedManager.GetFeed(feedName)
} else {
utils.CloseWithCodeAndMessage(w, 500, err.Error())
return
}
} else {
secret, _ := utils.GetSecret(r)
hL.Logger.Debug("secret", slog.String("secret", secret))

err = f.IsSecretValid(secret)
if err != nil {
Expand Down Expand Up @@ -298,7 +314,7 @@ func (api *ApiHandler) feedHandlerFunc(w http.ResponseWriter, r *http.Request) {
}
}

func (api *ApiHandler) feedPatchHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) feedPatchFunc(w http.ResponseWriter, r *http.Request) {
hL.Logger.Debug("Feed API Set PIN request", slog.Any("request", r))
secret, _ := utils.GetSecret(r)

Expand Down Expand Up @@ -338,7 +354,7 @@ func (api *ApiHandler) feedPatchHandlerFunc(w http.ResponseWriter, r *http.Reque
return
}
}
func (api *ApiHandler) feedItemEmptyHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) itemsDeleteFunc(w http.ResponseWriter, r *http.Request) {
hL.Logger.Debug("Item API EMPTY request", slog.Any("request", r))

secret, _ := utils.GetSecret(r)
Expand Down Expand Up @@ -368,7 +384,7 @@ func (api *ApiHandler) feedItemEmptyHandlerFunc(w http.ResponseWriter, r *http.R
}
}

func (api *ApiHandler) feedItemHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) itemGetFunc(w http.ResponseWriter, r *http.Request) {
hL.Logger.Debug("Item API GET request", slog.Any("request", r))

secret, _ := utils.GetSecret(r)
Expand Down Expand Up @@ -414,7 +430,7 @@ func (api *ApiHandler) feedItemHandlerFunc(w http.ResponseWriter, r *http.Reques
}
}

func (api *ApiHandler) feedPostHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) feedPostFunc(w http.ResponseWriter, r *http.Request) {
hL.Logger.Debug("Item API POST request", slog.Any("request", r))

secret, _ := utils.GetSecret(r)
Expand Down Expand Up @@ -471,7 +487,7 @@ func (api *ApiHandler) feedPostHandlerFunc(w http.ResponseWriter, r *http.Reques
}
}

func (api *ApiHandler) feedItemDeleteHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) itemDeleteFunc(w http.ResponseWriter, r *http.Request) {
hL.Logger.Debug("Item API DELETE request", slog.Any("request", r))

secret, _ := utils.GetSecret(r)
Expand Down Expand Up @@ -516,7 +532,7 @@ func (api *ApiHandler) feedItemDeleteHandlerFunc(w http.ResponseWriter, r *http.
}
}

func (api *ApiHandler) feedSubscriptionHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) subscriptionPostFunc(w http.ResponseWriter, r *http.Request) {

hL.Logger.Debug("Feed subscription request", slog.Any("request", r))

Expand Down Expand Up @@ -558,7 +574,7 @@ func (api *ApiHandler) feedSubscriptionHandlerFunc(w http.ResponseWriter, r *htt
}
}

func (api *ApiHandler) feedUnsubscribeHandlerFunc(w http.ResponseWriter, r *http.Request) {
func (api *ApiHandler) subscriptionDeleteFunc(w http.ResponseWriter, r *http.Request) {

hL.Logger.Debug("Feed subscription request", slog.Any("request", r))

Expand Down
28 changes: 28 additions & 0 deletions internal/handlers/http_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package handlers

import (
"encoding/json"
"fmt"
"net/http"
)

type APIResult struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
}

func WriteError(w http.ResponseWriter, code int, msg string, args ...interface{}) {
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(APIResult{Status: "error", Message: fmt.Sprintf(msg, args...)})
}

func WriteSuccessJSON(w http.ResponseWriter, body any) {
err := json.NewEncoder(w).Encode(body)
if err != nil {
WriteError(w, http.StatusInternalServerError, err.Error())
}
}

func WriteSuccess(w http.ResponseWriter, message string) {
WriteSuccessJSON(w, APIResult{Status: "success", Message: message})
}
20 changes: 17 additions & 3 deletions web/ui/src/YBFeed/Components/YBFeedItemsComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createContext, useEffect, useRef, useState } from 'react'
import { Space } from "@mantine/core"
import { YBFeedItemComponent } from '.'
import { Connector, YBFeed, YBFeedItem } from '../'
import { useNavigate } from 'react-router-dom';

export const FeedItemContext = createContext<undefined|YBFeedItem>(undefined);

Expand All @@ -14,6 +15,7 @@ export interface YBFeedItemsComponentProps {
export function YBFeedItemsComponent(props: YBFeedItemsComponentProps) {
const { feedName, secret } = props

const navigate = useNavigate()
const [feedItems, setFeedItems] = useState<YBFeedItem[]>([])

// Setup websocket to receive feed events
Expand All @@ -28,7 +30,16 @@ export function YBFeedItemsComponent(props: YBFeedItemsComponentProps) {
useEffect(() => {
const webSocketURL = window.location.protocol.replace("http","ws") + "//" + window.location.host + "/ws/" + feedName + "?secret=" + secret

function disconnect() {
if (ws.current === null) {
return
}
ws.current.close()
ws.current = null
}

function connect() {
disconnect()
ws.current = new WebSocket(webSocketURL)
if (ws.current === null) {
return
Expand Down Expand Up @@ -63,11 +74,14 @@ export function YBFeedItemsComponent(props: YBFeedItemsComponentProps) {
}
}

ws.current.onclose = () => {
console.log("websocket closed")
ws.current.onclose = (e) => {
console.log("websocket closed : ",e)

if (e.code > 4000) {
navigate("/")
return
}
// Try to reconnect
ws.current = null
setTimeout(() => {
console.log("reconnecting")
connect()
Expand Down

0 comments on commit d0315d8

Please sign in to comment.