client.go (5881B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package api 19 20 import ( 21 "time" 22 23 "github.com/gin-gonic/gin" 24 "github.com/superseriousbusiness/gotosocial/internal/api/client/accounts" 25 "github.com/superseriousbusiness/gotosocial/internal/api/client/admin" 26 "github.com/superseriousbusiness/gotosocial/internal/api/client/apps" 27 "github.com/superseriousbusiness/gotosocial/internal/api/client/blocks" 28 "github.com/superseriousbusiness/gotosocial/internal/api/client/bookmarks" 29 "github.com/superseriousbusiness/gotosocial/internal/api/client/customemojis" 30 "github.com/superseriousbusiness/gotosocial/internal/api/client/favourites" 31 "github.com/superseriousbusiness/gotosocial/internal/api/client/featuredtags" 32 filter "github.com/superseriousbusiness/gotosocial/internal/api/client/filters" 33 "github.com/superseriousbusiness/gotosocial/internal/api/client/followrequests" 34 "github.com/superseriousbusiness/gotosocial/internal/api/client/instance" 35 "github.com/superseriousbusiness/gotosocial/internal/api/client/lists" 36 "github.com/superseriousbusiness/gotosocial/internal/api/client/media" 37 "github.com/superseriousbusiness/gotosocial/internal/api/client/notifications" 38 "github.com/superseriousbusiness/gotosocial/internal/api/client/preferences" 39 "github.com/superseriousbusiness/gotosocial/internal/api/client/reports" 40 "github.com/superseriousbusiness/gotosocial/internal/api/client/search" 41 "github.com/superseriousbusiness/gotosocial/internal/api/client/statuses" 42 "github.com/superseriousbusiness/gotosocial/internal/api/client/streaming" 43 "github.com/superseriousbusiness/gotosocial/internal/api/client/timelines" 44 "github.com/superseriousbusiness/gotosocial/internal/api/client/user" 45 "github.com/superseriousbusiness/gotosocial/internal/db" 46 "github.com/superseriousbusiness/gotosocial/internal/middleware" 47 "github.com/superseriousbusiness/gotosocial/internal/processing" 48 "github.com/superseriousbusiness/gotosocial/internal/router" 49 ) 50 51 type Client struct { 52 processor *processing.Processor 53 db db.DB 54 55 accounts *accounts.Module // api/v1/accounts 56 admin *admin.Module // api/v1/admin 57 apps *apps.Module // api/v1/apps 58 blocks *blocks.Module // api/v1/blocks 59 bookmarks *bookmarks.Module // api/v1/bookmarks 60 customEmojis *customemojis.Module // api/v1/custom_emojis 61 favourites *favourites.Module // api/v1/favourites 62 featuredTags *featuredtags.Module // api/v1/featured_tags 63 filters *filter.Module // api/v1/filters 64 followRequests *followrequests.Module // api/v1/follow_requests 65 instance *instance.Module // api/v1/instance 66 lists *lists.Module // api/v1/lists 67 media *media.Module // api/v1/media, api/v2/media 68 notifications *notifications.Module // api/v1/notifications 69 preferences *preferences.Module // api/v1/preferences 70 reports *reports.Module // api/v1/reports 71 search *search.Module // api/v1/search, api/v2/search 72 statuses *statuses.Module // api/v1/statuses 73 streaming *streaming.Module // api/v1/streaming 74 timelines *timelines.Module // api/v1/timelines 75 user *user.Module // api/v1/user 76 } 77 78 func (c *Client) Route(r router.Router, m ...gin.HandlerFunc) { 79 // create a new group on the top level client 'api' prefix 80 apiGroup := r.AttachGroup("api") 81 82 // attach non-global middlewares appropriate to the client api 83 apiGroup.Use(m...) 84 apiGroup.Use( 85 middleware.TokenCheck(c.db, c.processor.OAuthValidateBearerToken), 86 middleware.CacheControl("no-store"), // never cache api responses 87 ) 88 89 // for each client api module, pass it the Handle function 90 // so that the module can attach its routes to this group 91 h := apiGroup.Handle 92 c.accounts.Route(h) 93 c.admin.Route(h) 94 c.apps.Route(h) 95 c.blocks.Route(h) 96 c.bookmarks.Route(h) 97 c.customEmojis.Route(h) 98 c.favourites.Route(h) 99 c.featuredTags.Route(h) 100 c.filters.Route(h) 101 c.followRequests.Route(h) 102 c.instance.Route(h) 103 c.lists.Route(h) 104 c.media.Route(h) 105 c.notifications.Route(h) 106 c.preferences.Route(h) 107 c.reports.Route(h) 108 c.search.Route(h) 109 c.statuses.Route(h) 110 c.streaming.Route(h) 111 c.timelines.Route(h) 112 c.user.Route(h) 113 } 114 115 func NewClient(db db.DB, p *processing.Processor) *Client { 116 return &Client{ 117 processor: p, 118 db: db, 119 120 accounts: accounts.New(p), 121 admin: admin.New(p), 122 apps: apps.New(p), 123 blocks: blocks.New(p), 124 bookmarks: bookmarks.New(p), 125 customEmojis: customemojis.New(p), 126 favourites: favourites.New(p), 127 featuredTags: featuredtags.New(p), 128 filters: filter.New(p), 129 followRequests: followrequests.New(p), 130 instance: instance.New(p), 131 lists: lists.New(p), 132 media: media.New(p), 133 notifications: notifications.New(p), 134 preferences: preferences.New(p), 135 reports: reports.New(p), 136 search: search.New(p), 137 statuses: statuses.New(p), 138 streaming: streaming.New(p, time.Second*30, 4096), 139 timelines: timelines.New(p), 140 user: user.New(p), 141 } 142 }