github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api/api.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     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/mlog"
    12  	"github.com/mattermost/mattermost-server/model"
    13  
    14  	_ "github.com/nicksnyder/go-i18n/i18n"
    15  )
    16  
    17  type Routes struct {
    18  	Root    *mux.Router // ''
    19  	ApiRoot *mux.Router // 'api/v3'
    20  
    21  	Users    *mux.Router // 'api/v3/users'
    22  	NeedUser *mux.Router // 'api/v3/users/{user_id:[A-Za-z0-9]+}'
    23  
    24  	Teams    *mux.Router // 'api/v3/teams'
    25  	NeedTeam *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}'
    26  
    27  	Channels        *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels'
    28  	NeedChannel     *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}'
    29  	NeedChannelName *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/name/{channel_name:[A-Za-z0-9_-]+}'
    30  
    31  	Posts    *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}/posts'
    32  	NeedPost *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}'
    33  
    34  	Commands *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/commands'
    35  	Hooks    *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/hooks'
    36  
    37  	TeamFiles *mux.Router // 'api/v3/teams/{team_id:[A-Za-z0-9]+}/files'
    38  	Files     *mux.Router // 'api/v3/files'
    39  	NeedFile  *mux.Router // 'api/v3/files/{file_id:[A-Za-z0-9]+}'
    40  
    41  	OAuth *mux.Router // 'api/v3/oauth'
    42  
    43  	Admin *mux.Router // 'api/v3/admin'
    44  
    45  	General *mux.Router // 'api/v3/general'
    46  
    47  	Preferences *mux.Router // 'api/v3/preferences'
    48  
    49  	License *mux.Router // 'api/v3/license'
    50  
    51  	Public *mux.Router // 'api/v3/public'
    52  
    53  	Emoji *mux.Router // 'api/v3/emoji'
    54  
    55  	Webrtc *mux.Router // 'api/v3/webrtc'
    56  }
    57  
    58  type API struct {
    59  	App        *app.App
    60  	BaseRoutes *Routes
    61  }
    62  
    63  func Init(a *app.App, root *mux.Router) *API {
    64  	api := &API{
    65  		App:        a,
    66  		BaseRoutes: &Routes{},
    67  	}
    68  	api.BaseRoutes.Root = root
    69  	api.BaseRoutes.ApiRoot = root.PathPrefix(model.API_URL_SUFFIX_V3).Subrouter()
    70  	api.BaseRoutes.Users = api.BaseRoutes.ApiRoot.PathPrefix("/users").Subrouter()
    71  	api.BaseRoutes.NeedUser = api.BaseRoutes.Users.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter()
    72  	api.BaseRoutes.Teams = api.BaseRoutes.ApiRoot.PathPrefix("/teams").Subrouter()
    73  	api.BaseRoutes.NeedTeam = api.BaseRoutes.Teams.PathPrefix("/{team_id:[A-Za-z0-9]+}").Subrouter()
    74  	api.BaseRoutes.Channels = api.BaseRoutes.NeedTeam.PathPrefix("/channels").Subrouter()
    75  	api.BaseRoutes.NeedChannel = api.BaseRoutes.Channels.PathPrefix("/{channel_id:[A-Za-z0-9]+}").Subrouter()
    76  	api.BaseRoutes.NeedChannelName = api.BaseRoutes.Channels.PathPrefix("/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter()
    77  	api.BaseRoutes.Posts = api.BaseRoutes.NeedChannel.PathPrefix("/posts").Subrouter()
    78  	api.BaseRoutes.NeedPost = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
    79  	api.BaseRoutes.Commands = api.BaseRoutes.NeedTeam.PathPrefix("/commands").Subrouter()
    80  	api.BaseRoutes.TeamFiles = api.BaseRoutes.NeedTeam.PathPrefix("/files").Subrouter()
    81  	api.BaseRoutes.Files = api.BaseRoutes.ApiRoot.PathPrefix("/files").Subrouter()
    82  	api.BaseRoutes.NeedFile = api.BaseRoutes.Files.PathPrefix("/{file_id:[A-Za-z0-9]+}").Subrouter()
    83  	api.BaseRoutes.Hooks = api.BaseRoutes.NeedTeam.PathPrefix("/hooks").Subrouter()
    84  	api.BaseRoutes.OAuth = api.BaseRoutes.ApiRoot.PathPrefix("/oauth").Subrouter()
    85  	api.BaseRoutes.Admin = api.BaseRoutes.ApiRoot.PathPrefix("/admin").Subrouter()
    86  	api.BaseRoutes.General = api.BaseRoutes.ApiRoot.PathPrefix("/general").Subrouter()
    87  	api.BaseRoutes.Preferences = api.BaseRoutes.ApiRoot.PathPrefix("/preferences").Subrouter()
    88  	api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter()
    89  	api.BaseRoutes.Public = api.BaseRoutes.ApiRoot.PathPrefix("/public").Subrouter()
    90  	api.BaseRoutes.Emoji = api.BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter()
    91  	api.BaseRoutes.Webrtc = api.BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
    92  
    93  	api.InitUser()
    94  	api.InitTeam()
    95  	api.InitChannel()
    96  	api.InitPost()
    97  	api.InitWebSocket()
    98  	api.InitFile()
    99  	api.InitCommand()
   100  	api.InitAdmin()
   101  	api.InitGeneral()
   102  	api.InitOAuth()
   103  	api.InitWebhook()
   104  	api.InitPreference()
   105  	api.InitLicense()
   106  	api.InitEmoji()
   107  	api.InitStatus()
   108  	api.InitWebrtc()
   109  	api.InitReaction()
   110  
   111  	// 404 on any api route before web.go has a chance to serve it
   112  	root.Handle("/api/{anything:.*}", http.HandlerFunc(api.Handle404))
   113  
   114  	a.InitEmailBatching()
   115  
   116  	if *a.Config().ServiceSettings.EnableAPIv3 {
   117  		mlog.Info("API version 3 is scheduled for deprecation. Please see https://api.mattermost.com for details.")
   118  	}
   119  
   120  	return api
   121  }
   122  
   123  func (api *API) Handle404(w http.ResponseWriter, r *http.Request) {
   124  	Handle404(api.App, w, r)
   125  }
   126  
   127  func ReturnStatusOK(w http.ResponseWriter) {
   128  	m := make(map[string]string)
   129  	m[model.STATUS] = model.STATUS_OK
   130  	w.Write([]byte(model.MapToJson(m)))
   131  }