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