github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/api.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "net/http" 8 9 "github.com/gorilla/mux" 10 "github.com/mattermost/mattermost-server/app" 11 "github.com/mattermost/mattermost-server/model" 12 "github.com/mattermost/mattermost-server/services/configservice" 13 "github.com/mattermost/mattermost-server/web" 14 15 _ "github.com/nicksnyder/go-i18n/i18n" 16 ) 17 18 type Routes struct { 19 Root *mux.Router // '' 20 ApiRoot *mux.Router // 'api/v4' 21 22 Users *mux.Router // 'api/v4/users' 23 User *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}' 24 UserByUsername *mux.Router // 'api/v4/users/username/{username:[A-Za-z0-9_-\.]+}' 25 UserByEmail *mux.Router // 'api/v4/users/email/{email}' 26 27 Bots *mux.Router // 'api/v4/bots' 28 Bot *mux.Router // 'api/v4/bots/{bot_user_id:[A-Za-z0-9]+}' 29 30 Teams *mux.Router // 'api/v4/teams' 31 TeamsForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/teams' 32 Team *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9]+}' 33 TeamForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/teams/{team_id:[A-Za-z0-9]+}' 34 TeamByName *mux.Router // 'api/v4/teams/name/{team_name:[A-Za-z0-9_-]+}' 35 TeamMembers *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9_-]+}/members' 36 TeamMember *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9_-]+}/members/{user_id:[A-Za-z0-9_-]+}' 37 TeamMembersForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/teams/members' 38 39 Channels *mux.Router // 'api/v4/channels' 40 Channel *mux.Router // 'api/v4/channels/{channel_id:[A-Za-z0-9]+}' 41 ChannelForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}' 42 ChannelByName *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9]+}/channels/name/{channel_name:[A-Za-z0-9_-]+}' 43 ChannelByNameForTeamName *mux.Router // 'api/v4/teams/name/{team_name:[A-Za-z0-9_-]+}/channels/name/{channel_name:[A-Za-z0-9_-]+}' 44 ChannelsForTeam *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9]+}/channels' 45 ChannelMembers *mux.Router // 'api/v4/channels/{channel_id:[A-Za-z0-9]+}/members' 46 ChannelMember *mux.Router // 'api/v4/channels/{channel_id:[A-Za-z0-9]+}/members/{user_id:[A-Za-z0-9]+}' 47 ChannelMembersForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/teams/{team_id:[A-Za-z0-9]+}/channels/members' 48 49 Posts *mux.Router // 'api/v4/posts' 50 Post *mux.Router // 'api/v4/posts/{post_id:[A-Za-z0-9]+}' 51 PostsForChannel *mux.Router // 'api/v4/channels/{channel_id:[A-Za-z0-9]+}/posts' 52 PostsForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts' 53 PostForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}' 54 55 Files *mux.Router // 'api/v4/files' 56 File *mux.Router // 'api/v4/files/{file_id:[A-Za-z0-9]+}' 57 58 Plugins *mux.Router // 'api/v4/plugins' 59 Plugin *mux.Router // 'api/v4/plugins/{plugin_id:[A-Za-z0-9_-]+}' 60 61 PublicFile *mux.Router // 'files/{file_id:[A-Za-z0-9]+}/public' 62 63 Commands *mux.Router // 'api/v4/commands' 64 Command *mux.Router // 'api/v4/commands/{command_id:[A-Za-z0-9]+}' 65 66 Hooks *mux.Router // 'api/v4/hooks' 67 IncomingHooks *mux.Router // 'api/v4/hooks/incoming' 68 IncomingHook *mux.Router // 'api/v4/hooks/incoming/{hook_id:[A-Za-z0-9]+}' 69 OutgoingHooks *mux.Router // 'api/v4/hooks/outgoing' 70 OutgoingHook *mux.Router // 'api/v4/hooks/outgoing/{hook_id:[A-Za-z0-9]+}' 71 72 OAuth *mux.Router // 'api/v4/oauth' 73 OAuthApps *mux.Router // 'api/v4/oauth/apps' 74 OAuthApp *mux.Router // 'api/v4/oauth/apps/{app_id:[A-Za-z0-9]+}' 75 76 OpenGraph *mux.Router // 'api/v4/opengraph' 77 78 SAML *mux.Router // 'api/v4/saml' 79 Compliance *mux.Router // 'api/v4/compliance' 80 Cluster *mux.Router // 'api/v4/cluster' 81 82 Image *mux.Router // 'api/v4/image' 83 84 LDAP *mux.Router // 'api/v4/ldap' 85 86 Elasticsearch *mux.Router // 'api/v4/elasticsearch' 87 88 DataRetention *mux.Router // 'api/v4/data_retention' 89 90 Brand *mux.Router // 'api/v4/brand' 91 92 System *mux.Router // 'api/v4/system' 93 94 Jobs *mux.Router // 'api/v4/jobs' 95 96 Preferences *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/preferences' 97 98 License *mux.Router // 'api/v4/license' 99 100 Public *mux.Router // 'api/v4/public' 101 102 Reactions *mux.Router // 'api/v4/reactions' 103 104 Roles *mux.Router // 'api/v4/roles' 105 Schemes *mux.Router // 'api/v4/schemes' 106 107 Emojis *mux.Router // 'api/v4/emoji' 108 Emoji *mux.Router // 'api/v4/emoji/{emoji_id:[A-Za-z0-9]+}' 109 EmojiByName *mux.Router // 'api/v4/emoji/name/{emoji_name:[A-Za-z0-9_-\.]+}' 110 111 ReactionByNameForPostForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}/reactions/{emoji_name:[A-Za-z0-9_-+]+}' 112 113 TermsOfService *mux.Router // 'api/v4/terms_of_service 114 Groups *mux.Router // 'api/v4/groups' 115 } 116 117 type API struct { 118 ConfigService configservice.ConfigService 119 GetGlobalAppOptions app.AppOptionCreator 120 BaseRoutes *Routes 121 } 122 123 func Init(configservice configservice.ConfigService, globalOptionsFunc app.AppOptionCreator, root *mux.Router) *API { 124 api := &API{ 125 ConfigService: configservice, 126 GetGlobalAppOptions: globalOptionsFunc, 127 BaseRoutes: &Routes{}, 128 } 129 130 api.BaseRoutes.Root = root 131 api.BaseRoutes.ApiRoot = root.PathPrefix(model.API_URL_SUFFIX).Subrouter() 132 133 api.BaseRoutes.Users = api.BaseRoutes.ApiRoot.PathPrefix("/users").Subrouter() 134 api.BaseRoutes.User = api.BaseRoutes.ApiRoot.PathPrefix("/users/{user_id:[A-Za-z0-9]+}").Subrouter() 135 api.BaseRoutes.UserByUsername = api.BaseRoutes.Users.PathPrefix("/username/{username:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() 136 api.BaseRoutes.UserByEmail = api.BaseRoutes.Users.PathPrefix("/email/{email}").Subrouter() 137 138 api.BaseRoutes.Bots = api.BaseRoutes.ApiRoot.PathPrefix("/bots").Subrouter() 139 api.BaseRoutes.Bot = api.BaseRoutes.ApiRoot.PathPrefix("/bots/{bot_user_id:[A-Za-z0-9]+}").Subrouter() 140 141 api.BaseRoutes.Teams = api.BaseRoutes.ApiRoot.PathPrefix("/teams").Subrouter() 142 api.BaseRoutes.TeamsForUser = api.BaseRoutes.User.PathPrefix("/teams").Subrouter() 143 api.BaseRoutes.Team = api.BaseRoutes.Teams.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter() 144 api.BaseRoutes.TeamForUser = api.BaseRoutes.TeamsForUser.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter() 145 api.BaseRoutes.TeamByName = api.BaseRoutes.Teams.PathPrefix("/name/{team_name:[A-Za-z0-9_-]+}").Subrouter() 146 api.BaseRoutes.TeamMembers = api.BaseRoutes.Team.PathPrefix("/members").Subrouter() 147 api.BaseRoutes.TeamMember = api.BaseRoutes.TeamMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter() 148 api.BaseRoutes.TeamMembersForUser = api.BaseRoutes.User.PathPrefix("/teams/members").Subrouter() 149 150 api.BaseRoutes.Channels = api.BaseRoutes.ApiRoot.PathPrefix("/channels").Subrouter() 151 api.BaseRoutes.Channel = api.BaseRoutes.Channels.PathPrefix("/{channel_id:[A-Za-z0-9]+}").Subrouter() 152 api.BaseRoutes.ChannelForUser = api.BaseRoutes.User.PathPrefix("/channels/{channel_id:[A-Za-z0-9]+}").Subrouter() 153 api.BaseRoutes.ChannelByName = api.BaseRoutes.Team.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter() 154 api.BaseRoutes.ChannelByNameForTeamName = api.BaseRoutes.TeamByName.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter() 155 api.BaseRoutes.ChannelsForTeam = api.BaseRoutes.Team.PathPrefix("/channels").Subrouter() 156 api.BaseRoutes.ChannelMembers = api.BaseRoutes.Channel.PathPrefix("/members").Subrouter() 157 api.BaseRoutes.ChannelMember = api.BaseRoutes.ChannelMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter() 158 api.BaseRoutes.ChannelMembersForUser = api.BaseRoutes.User.PathPrefix("/teams/{team_id:[A-Za-z0-9]+}/channels/members").Subrouter() 159 160 api.BaseRoutes.Posts = api.BaseRoutes.ApiRoot.PathPrefix("/posts").Subrouter() 161 api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter() 162 api.BaseRoutes.PostsForChannel = api.BaseRoutes.Channel.PathPrefix("/posts").Subrouter() 163 api.BaseRoutes.PostsForUser = api.BaseRoutes.User.PathPrefix("/posts").Subrouter() 164 api.BaseRoutes.PostForUser = api.BaseRoutes.PostsForUser.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter() 165 166 api.BaseRoutes.Files = api.BaseRoutes.ApiRoot.PathPrefix("/files").Subrouter() 167 api.BaseRoutes.File = api.BaseRoutes.Files.PathPrefix("/{file_id:[A-Za-z0-9]+}").Subrouter() 168 api.BaseRoutes.PublicFile = api.BaseRoutes.Root.PathPrefix("/files/{file_id:[A-Za-z0-9]+}/public").Subrouter() 169 170 api.BaseRoutes.Plugins = api.BaseRoutes.ApiRoot.PathPrefix("/plugins").Subrouter() 171 api.BaseRoutes.Plugin = api.BaseRoutes.Plugins.PathPrefix("/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() 172 173 api.BaseRoutes.Commands = api.BaseRoutes.ApiRoot.PathPrefix("/commands").Subrouter() 174 api.BaseRoutes.Command = api.BaseRoutes.Commands.PathPrefix("/{command_id:[A-Za-z0-9]+}").Subrouter() 175 176 api.BaseRoutes.Hooks = api.BaseRoutes.ApiRoot.PathPrefix("/hooks").Subrouter() 177 api.BaseRoutes.IncomingHooks = api.BaseRoutes.Hooks.PathPrefix("/incoming").Subrouter() 178 api.BaseRoutes.IncomingHook = api.BaseRoutes.IncomingHooks.PathPrefix("/{hook_id:[A-Za-z0-9]+}").Subrouter() 179 api.BaseRoutes.OutgoingHooks = api.BaseRoutes.Hooks.PathPrefix("/outgoing").Subrouter() 180 api.BaseRoutes.OutgoingHook = api.BaseRoutes.OutgoingHooks.PathPrefix("/{hook_id:[A-Za-z0-9]+}").Subrouter() 181 182 api.BaseRoutes.SAML = api.BaseRoutes.ApiRoot.PathPrefix("/saml").Subrouter() 183 184 api.BaseRoutes.OAuth = api.BaseRoutes.ApiRoot.PathPrefix("/oauth").Subrouter() 185 api.BaseRoutes.OAuthApps = api.BaseRoutes.OAuth.PathPrefix("/apps").Subrouter() 186 api.BaseRoutes.OAuthApp = api.BaseRoutes.OAuthApps.PathPrefix("/{app_id:[A-Za-z0-9]+}").Subrouter() 187 188 api.BaseRoutes.Compliance = api.BaseRoutes.ApiRoot.PathPrefix("/compliance").Subrouter() 189 api.BaseRoutes.Cluster = api.BaseRoutes.ApiRoot.PathPrefix("/cluster").Subrouter() 190 api.BaseRoutes.LDAP = api.BaseRoutes.ApiRoot.PathPrefix("/ldap").Subrouter() 191 api.BaseRoutes.Brand = api.BaseRoutes.ApiRoot.PathPrefix("/brand").Subrouter() 192 api.BaseRoutes.System = api.BaseRoutes.ApiRoot.PathPrefix("/system").Subrouter() 193 api.BaseRoutes.Preferences = api.BaseRoutes.User.PathPrefix("/preferences").Subrouter() 194 api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter() 195 api.BaseRoutes.Public = api.BaseRoutes.ApiRoot.PathPrefix("/public").Subrouter() 196 api.BaseRoutes.Reactions = api.BaseRoutes.ApiRoot.PathPrefix("/reactions").Subrouter() 197 api.BaseRoutes.Jobs = api.BaseRoutes.ApiRoot.PathPrefix("/jobs").Subrouter() 198 api.BaseRoutes.Elasticsearch = api.BaseRoutes.ApiRoot.PathPrefix("/elasticsearch").Subrouter() 199 api.BaseRoutes.DataRetention = api.BaseRoutes.ApiRoot.PathPrefix("/data_retention").Subrouter() 200 201 api.BaseRoutes.Emojis = api.BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter() 202 api.BaseRoutes.Emoji = api.BaseRoutes.ApiRoot.PathPrefix("/emoji/{emoji_id:[A-Za-z0-9]+}").Subrouter() 203 api.BaseRoutes.EmojiByName = api.BaseRoutes.Emojis.PathPrefix("/name/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter() 204 205 api.BaseRoutes.ReactionByNameForPostForUser = api.BaseRoutes.PostForUser.PathPrefix("/reactions/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter() 206 207 api.BaseRoutes.OpenGraph = api.BaseRoutes.ApiRoot.PathPrefix("/opengraph").Subrouter() 208 209 api.BaseRoutes.Roles = api.BaseRoutes.ApiRoot.PathPrefix("/roles").Subrouter() 210 api.BaseRoutes.Schemes = api.BaseRoutes.ApiRoot.PathPrefix("/schemes").Subrouter() 211 212 api.BaseRoutes.Image = api.BaseRoutes.ApiRoot.PathPrefix("/image").Subrouter() 213 214 api.BaseRoutes.TermsOfService = api.BaseRoutes.ApiRoot.PathPrefix("/terms_of_service").Subrouter() 215 api.BaseRoutes.Groups = api.BaseRoutes.ApiRoot.PathPrefix("/groups").Subrouter() 216 217 api.InitUser() 218 api.InitBot() 219 api.InitTeam() 220 api.InitChannel() 221 api.InitPost() 222 api.InitFile() 223 api.InitSystem() 224 api.InitLicense() 225 api.InitConfig() 226 api.InitWebhook() 227 api.InitPreference() 228 api.InitSaml() 229 api.InitCompliance() 230 api.InitCluster() 231 api.InitLdap() 232 api.InitElasticsearch() 233 api.InitDataRetention() 234 api.InitBrand() 235 api.InitJob() 236 api.InitCommand() 237 api.InitStatus() 238 api.InitWebSocket() 239 api.InitEmoji() 240 api.InitOAuth() 241 api.InitReaction() 242 api.InitOpenGraph() 243 api.InitPlugin() 244 api.InitRole() 245 api.InitScheme() 246 api.InitImage() 247 api.InitTermsOfService() 248 api.InitGroup() 249 api.InitAction() 250 251 root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404)) 252 253 return api 254 } 255 256 func (api *API) Handle404(w http.ResponseWriter, r *http.Request) { 257 web.Handle404(api.ConfigService, w, r) 258 } 259 260 var ReturnStatusOK = web.ReturnStatusOK