github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  
   112  type API struct {
   113  	App        *app.App
   114  	BaseRoutes *Routes
   115  }
   116  
   117  func Init(a *app.App, root *mux.Router) *API {
   118  	api := &API{
   119  		App:        a,
   120  		BaseRoutes: &Routes{},
   121  	}
   122  
   123  	api.BaseRoutes.Root = root
   124  	api.BaseRoutes.ApiRoot = root.PathPrefix(model.API_URL_SUFFIX).Subrouter()
   125  
   126  	api.BaseRoutes.Users = api.BaseRoutes.ApiRoot.PathPrefix("/users").Subrouter()
   127  	api.BaseRoutes.User = api.BaseRoutes.ApiRoot.PathPrefix("/users/{user_id:[A-Za-z0-9]+}").Subrouter()
   128  	api.BaseRoutes.UserByUsername = api.BaseRoutes.Users.PathPrefix("/username/{username:[A-Za-z0-9\\_\\-\\.]+}").Subrouter()
   129  	api.BaseRoutes.UserByEmail = api.BaseRoutes.Users.PathPrefix("/email/{email}").Subrouter()
   130  
   131  	api.BaseRoutes.Teams = api.BaseRoutes.ApiRoot.PathPrefix("/teams").Subrouter()
   132  	api.BaseRoutes.TeamsForUser = api.BaseRoutes.User.PathPrefix("/teams").Subrouter()
   133  	api.BaseRoutes.Team = api.BaseRoutes.Teams.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter()
   134  	api.BaseRoutes.TeamForUser = api.BaseRoutes.TeamsForUser.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter()
   135  	api.BaseRoutes.TeamByName = api.BaseRoutes.Teams.PathPrefix("/name/{team_name:[A-Za-z0-9_-]+}").Subrouter()
   136  	api.BaseRoutes.TeamMembers = api.BaseRoutes.Team.PathPrefix("/members").Subrouter()
   137  	api.BaseRoutes.TeamMember = api.BaseRoutes.TeamMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter()
   138  	api.BaseRoutes.TeamMembersForUser = api.BaseRoutes.User.PathPrefix("/teams/members").Subrouter()
   139  
   140  	api.BaseRoutes.Channels = api.BaseRoutes.ApiRoot.PathPrefix("/channels").Subrouter()
   141  	api.BaseRoutes.Channel = api.BaseRoutes.Channels.PathPrefix("/{channel_id:[A-Za-z0-9]+}").Subrouter()
   142  	api.BaseRoutes.ChannelForUser = api.BaseRoutes.User.PathPrefix("/channels/{channel_id:[A-Za-z0-9]+}").Subrouter()
   143  	api.BaseRoutes.ChannelByName = api.BaseRoutes.Team.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter()
   144  	api.BaseRoutes.ChannelByNameForTeamName = api.BaseRoutes.TeamByName.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter()
   145  	api.BaseRoutes.ChannelsForTeam = api.BaseRoutes.Team.PathPrefix("/channels").Subrouter()
   146  	api.BaseRoutes.ChannelMembers = api.BaseRoutes.Channel.PathPrefix("/members").Subrouter()
   147  	api.BaseRoutes.ChannelMember = api.BaseRoutes.ChannelMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter()
   148  	api.BaseRoutes.ChannelMembersForUser = api.BaseRoutes.User.PathPrefix("/teams/{team_id:[A-Za-z0-9]+}/channels/members").Subrouter()
   149  
   150  	api.BaseRoutes.Posts = api.BaseRoutes.ApiRoot.PathPrefix("/posts").Subrouter()
   151  	api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
   152  	api.BaseRoutes.PostsForChannel = api.BaseRoutes.Channel.PathPrefix("/posts").Subrouter()
   153  	api.BaseRoutes.PostsForUser = api.BaseRoutes.User.PathPrefix("/posts").Subrouter()
   154  	api.BaseRoutes.PostForUser = api.BaseRoutes.PostsForUser.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
   155  
   156  	api.BaseRoutes.Files = api.BaseRoutes.ApiRoot.PathPrefix("/files").Subrouter()
   157  	api.BaseRoutes.File = api.BaseRoutes.Files.PathPrefix("/{file_id:[A-Za-z0-9]+}").Subrouter()
   158  	api.BaseRoutes.PublicFile = api.BaseRoutes.Root.PathPrefix("/files/{file_id:[A-Za-z0-9]+}/public").Subrouter()
   159  
   160  	api.BaseRoutes.Plugins = api.BaseRoutes.ApiRoot.PathPrefix("/plugins").Subrouter()
   161  	api.BaseRoutes.Plugin = api.BaseRoutes.Plugins.PathPrefix("/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter()
   162  
   163  	api.BaseRoutes.Commands = api.BaseRoutes.ApiRoot.PathPrefix("/commands").Subrouter()
   164  	api.BaseRoutes.Command = api.BaseRoutes.Commands.PathPrefix("/{command_id:[A-Za-z0-9]+}").Subrouter()
   165  
   166  	api.BaseRoutes.Hooks = api.BaseRoutes.ApiRoot.PathPrefix("/hooks").Subrouter()
   167  	api.BaseRoutes.IncomingHooks = api.BaseRoutes.Hooks.PathPrefix("/incoming").Subrouter()
   168  	api.BaseRoutes.IncomingHook = api.BaseRoutes.IncomingHooks.PathPrefix("/{hook_id:[A-Za-z0-9]+}").Subrouter()
   169  	api.BaseRoutes.OutgoingHooks = api.BaseRoutes.Hooks.PathPrefix("/outgoing").Subrouter()
   170  	api.BaseRoutes.OutgoingHook = api.BaseRoutes.OutgoingHooks.PathPrefix("/{hook_id:[A-Za-z0-9]+}").Subrouter()
   171  
   172  	api.BaseRoutes.SAML = api.BaseRoutes.ApiRoot.PathPrefix("/saml").Subrouter()
   173  
   174  	api.BaseRoutes.OAuth = api.BaseRoutes.ApiRoot.PathPrefix("/oauth").Subrouter()
   175  	api.BaseRoutes.OAuthApps = api.BaseRoutes.OAuth.PathPrefix("/apps").Subrouter()
   176  	api.BaseRoutes.OAuthApp = api.BaseRoutes.OAuthApps.PathPrefix("/{app_id:[A-Za-z0-9]+}").Subrouter()
   177  
   178  	api.BaseRoutes.Compliance = api.BaseRoutes.ApiRoot.PathPrefix("/compliance").Subrouter()
   179  	api.BaseRoutes.Cluster = api.BaseRoutes.ApiRoot.PathPrefix("/cluster").Subrouter()
   180  	api.BaseRoutes.LDAP = api.BaseRoutes.ApiRoot.PathPrefix("/ldap").Subrouter()
   181  	api.BaseRoutes.Brand = api.BaseRoutes.ApiRoot.PathPrefix("/brand").Subrouter()
   182  	api.BaseRoutes.System = api.BaseRoutes.ApiRoot.PathPrefix("/system").Subrouter()
   183  	api.BaseRoutes.Preferences = api.BaseRoutes.User.PathPrefix("/preferences").Subrouter()
   184  	api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter()
   185  	api.BaseRoutes.Public = api.BaseRoutes.ApiRoot.PathPrefix("/public").Subrouter()
   186  	api.BaseRoutes.Reactions = api.BaseRoutes.ApiRoot.PathPrefix("/reactions").Subrouter()
   187  	api.BaseRoutes.Jobs = api.BaseRoutes.ApiRoot.PathPrefix("/jobs").Subrouter()
   188  	api.BaseRoutes.Elasticsearch = api.BaseRoutes.ApiRoot.PathPrefix("/elasticsearch").Subrouter()
   189  	api.BaseRoutes.DataRetention = api.BaseRoutes.ApiRoot.PathPrefix("/data_retention").Subrouter()
   190  
   191  	api.BaseRoutes.Emojis = api.BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter()
   192  	api.BaseRoutes.Emoji = api.BaseRoutes.ApiRoot.PathPrefix("/emoji/{emoji_id:[A-Za-z0-9]+}").Subrouter()
   193  	api.BaseRoutes.EmojiByName = api.BaseRoutes.Emojis.PathPrefix("/name/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter()
   194  
   195  	api.BaseRoutes.ReactionByNameForPostForUser = api.BaseRoutes.PostForUser.PathPrefix("/reactions/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter()
   196  
   197  	api.BaseRoutes.Webrtc = api.BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
   198  
   199  	api.BaseRoutes.OpenGraph = api.BaseRoutes.ApiRoot.PathPrefix("/opengraph").Subrouter()
   200  
   201  	api.BaseRoutes.Roles = api.BaseRoutes.ApiRoot.PathPrefix("/roles").Subrouter()
   202  	api.BaseRoutes.Schemes = api.BaseRoutes.ApiRoot.PathPrefix("/schemes").Subrouter()
   203  
   204  	api.BaseRoutes.Image = api.BaseRoutes.ApiRoot.PathPrefix("/image").Subrouter()
   205  
   206  	api.InitUser()
   207  	api.InitTeam()
   208  	api.InitChannel()
   209  	api.InitPost()
   210  	api.InitFile()
   211  	api.InitSystem()
   212  	api.InitWebhook()
   213  	api.InitPreference()
   214  	api.InitSaml()
   215  	api.InitCompliance()
   216  	api.InitCluster()
   217  	api.InitLdap()
   218  	api.InitElasticsearch()
   219  	api.InitDataRetention()
   220  	api.InitBrand()
   221  	api.InitJob()
   222  	api.InitCommand()
   223  	api.InitStatus()
   224  	api.InitWebSocket()
   225  	api.InitEmoji()
   226  	api.InitOAuth()
   227  	api.InitReaction()
   228  	api.InitWebrtc()
   229  	api.InitOpenGraph()
   230  	api.InitPlugin()
   231  	api.InitRole()
   232  	api.InitScheme()
   233  	api.InitImage()
   234  
   235  	root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
   236  
   237  	a.InitEmailBatching()
   238  
   239  	return api
   240  }
   241  
   242  func (api *API) Handle404(w http.ResponseWriter, r *http.Request) {
   243  	web.Handle404(api.App, w, r)
   244  }
   245  
   246  var ReturnStatusOK = web.ReturnStatusOK