diff options
author | Julio Capote <jcapote@gmail.com> | 2023-01-02 13:07:21 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2023-01-02 13:07:21 +0000 |
commit | 8d23867d2c6d3c6f0dd9681192b9b8642ceea254 (patch) | |
tree | bb2a1609ebd188d2902fe9cf7d2590527e17c55b /http | |
parent | 81873a1c6786b834a50486137f7b92b60e901d28 (diff) | |
download | communique-8d23867d2c6d3c6f0dd9681192b9b8642ceea254.tar.gz |
clean up error/response/not found handling
Diffstat (limited to 'http')
-rw-r--r-- | http/router.go | 74 |
1 files changed, 26 insertions, 48 deletions
diff --git a/http/router.go b/http/router.go index 9730dbf..23843c4 100644 --- a/http/router.go +++ b/http/router.go @@ -16,6 +16,18 @@ func NewRouter(registry *registry.Registry) *Router { return &Router{registry: registry} } +func render(c *gin.Context, resource map[string]interface{}, err error) { + if resource == nil && err == nil { + c.JSON(http.StatusNotFound, map[string]interface{}{}) + } else if err != nil { + jsonErr := c.Error(err) + c.JSON(http.StatusInternalServerError, jsonErr.JSON()) + } else { + c.Writer.Header().Set("Content-Type", "application/activity+json") + c.JSON(http.StatusOK, resource) + } +} + func (s *Router) Start(zapWriter io.Writer) { router := gin.Default() router.SetTrustedProxies(nil) @@ -37,36 +49,20 @@ func (s *Router) Start(zapWriter io.Writer) { // "User" endpoint router.GET("/actors/:actor", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.Profile(actorParam) - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } - + resource, err := s.registry.Profile(actorParam) + render(c, resource, err) }) router.GET("/actors/:actor/followers", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.Followers(actorParam) - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.Followers(actorParam) + render(c, resource, err) }) router.GET("/actors/:actor/following", func(c *gin.Context) { actorParam := c.Param("actor") - resource, _ := s.registry.Following(actorParam) - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.Following(actorParam) + render(c, resource, err) }) // // Inbox @@ -85,47 +81,29 @@ func (s *Router) Start(zapWriter io.Writer) { router.GET("/actors/:actor/outbox", func(c *gin.Context) { actorParam := c.Param("actor") var resource map[string]interface{} + var err error if c.Query("page") == "true" { - resource, _ = s.registry.OutboxCollection(actorParam) + resource, err = s.registry.OutboxCollection(actorParam) } else { - resource, _ = s.registry.Outbox(actorParam) - } - if resource != nil { - c.JSON(http.StatusOK, resource) - c.Writer.Header().Set("Content-Type", "application/activity+json") - } else { - c.JSON(http.StatusNotFound, nil) + resource, err = s.registry.Outbox(actorParam) } + render(c, resource, err) }) // Single activity router.GET("/actors/:actor/activity/:id", func(c *gin.Context) { actorParam := c.Param("actor") idParam := c.Param("id") - var resource map[string]interface{} - resource, _ = s.registry.ActivityOrNote("activity", actorParam, idParam) - - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.ActivityOrNote("activity", actorParam, idParam) + render(c, resource, err) }) // Single note router.GET("/actors/:actor/activity/:id/note", func(c *gin.Context) { actorParam := c.Param("actor") idParam := c.Param("id") - var resource map[string]interface{} - resource, _ = s.registry.ActivityOrNote("note", actorParam, idParam) - - if resource != nil { - c.Writer.Header().Set("Content-Type", "application/activity+json") - c.JSON(http.StatusOK, resource) - } else { - c.JSON(http.StatusNotFound, nil) - } + resource, err := s.registry.ActivityOrNote("note", actorParam, idParam) + render(c, resource, err) }) router.Run() |