github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/api4/api.go (about)

     1  // Copyright (c) 2015-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/vnforks/kid/v5/app"
    11  	"github.com/vnforks/kid/v5/model"
    12  	"github.com/vnforks/kid/v5/services/configservice"
    13  	"github.com/vnforks/kid/v5/web"
    14  
    15  	_ "github.com/mattermost/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  	Branches             *mux.Router // 'api/v4/branches'
    28  	BranchesForUser      *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/branches'
    29  	Branch               *mux.Router // 'api/v4/branches/{branch_id:[A-Za-z0-9]+}'
    30  	BranchForUser        *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/branches/{branch_id:[A-Za-z0-9]+}'
    31  	BranchByName         *mux.Router // 'api/v4/branches/name/{branch_name:[A-Za-z0-9_-]+}'
    32  	BranchMembers        *mux.Router // 'api/v4/branches/{branch_id:[A-Za-z0-9_-]+}/members'
    33  	BranchMember         *mux.Router // 'api/v4/branches/{branch_id:[A-Za-z0-9_-]+}/members/{user_id:[A-Za-z0-9_-]+}'
    34  	BranchMembersForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/branches/members'
    35  
    36  	Classes                  *mux.Router // 'api/v4/classes'
    37  	Class                    *mux.Router // 'api/v4/classes/{class_id:[A-Za-z0-9]+}'
    38  	ClassForUser             *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/classes/{class_id:[A-Za-z0-9]+}'
    39  	ClassByName              *mux.Router // 'api/v4/branches/{branch_id:[A-Za-z0-9]+}/classes/name/{class_name:[A-Za-z0-9_-]+}'
    40  	ClassByNameForBranchName *mux.Router // 'api/v4/branches/name/{branch_name:[A-Za-z0-9_-]+}/classes/name/{class_name:[A-Za-z0-9_-]+}'
    41  	ClassesForBranch         *mux.Router // 'api/v4/branches/{branch_id:[A-Za-z0-9]+}/classes'
    42  	ClassMembers             *mux.Router // 'api/v4/classes/{class_id:[A-Za-z0-9]+}/members'
    43  	ClassMember              *mux.Router // 'api/v4/classes/{class_id:[A-Za-z0-9]+}/members/{user_id:[A-Za-z0-9]+}'
    44  	ClassMembersForUser      *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/branches/{branch_id:[A-Za-z0-9]+}/classes/members'
    45  	ClassModerations         *mux.Router // 'api/v4/classes/{class_id:[A-Za-z0-9]+}/moderations'
    46  
    47  	Posts         *mux.Router // 'api/v4/posts'
    48  	Post          *mux.Router // 'api/v4/posts/{post_id:[A-Za-z0-9]+}'
    49  	PostsForClass *mux.Router // 'api/v4/classes/{class_id:[A-Za-z0-9]+}/posts'
    50  	PostsForUser  *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts'
    51  	PostForUser   *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}'
    52  
    53  	Files *mux.Router // 'api/v4/files'
    54  	File  *mux.Router // 'api/v4/files/{file_id:[A-Za-z0-9]+}'
    55  
    56  	Plugins *mux.Router // 'api/v4/plugins'
    57  	Plugin  *mux.Router // 'api/v4/plugins/{plugin_id:[A-Za-z0-9_-]+}'
    58  
    59  	PublicFile *mux.Router // 'files/{file_id:[A-Za-z0-9]+}/public'
    60  
    61  	Commands *mux.Router // 'api/v4/commands'
    62  	Command  *mux.Router // 'api/v4/commands/{command_id:[A-Za-z0-9]+}'
    63  
    64  	Hooks         *mux.Router // 'api/v4/hooks'
    65  	IncomingHooks *mux.Router // 'api/v4/hooks/incoming'
    66  	IncomingHook  *mux.Router // 'api/v4/hooks/incoming/{hook_id:[A-Za-z0-9]+}'
    67  	OutgoingHooks *mux.Router // 'api/v4/hooks/outgoing'
    68  	OutgoingHook  *mux.Router // 'api/v4/hooks/outgoing/{hook_id:[A-Za-z0-9]+}'
    69  
    70  	OAuth     *mux.Router // 'api/v4/oauth'
    71  	OAuthApps *mux.Router // 'api/v4/oauth/apps'
    72  	OAuthApp  *mux.Router // 'api/v4/oauth/apps/{app_id:[A-Za-z0-9]+}'
    73  
    74  	OpenGraph *mux.Router // 'api/v4/opengraph'
    75  
    76  	SAML       *mux.Router // 'api/v4/saml'
    77  	Compliance *mux.Router // 'api/v4/compliance'
    78  	Cluster    *mux.Router // 'api/v4/cluster'
    79  
    80  	Image *mux.Router // 'api/v4/image'
    81  
    82  	LDAP *mux.Router // 'api/v4/ldap'
    83  
    84  	Elasticsearch *mux.Router // 'api/v4/elasticsearch'
    85  
    86  	DataRetention *mux.Router // 'api/v4/data_retention'
    87  
    88  	Brand *mux.Router // 'api/v4/brand'
    89  
    90  	System *mux.Router // 'api/v4/system'
    91  
    92  	Jobs *mux.Router // 'api/v4/jobs'
    93  
    94  	Preferences *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/preferences'
    95  
    96  	License *mux.Router // 'api/v4/license'
    97  
    98  	Public *mux.Router // 'api/v4/public'
    99  
   100  	Reactions *mux.Router // 'api/v4/reactions'
   101  
   102  	Roles   *mux.Router // 'api/v4/roles'
   103  	Schemes *mux.Router // 'api/v4/schemes'
   104  
   105  	Emojis      *mux.Router // 'api/v4/emoji'
   106  	Emoji       *mux.Router // 'api/v4/emoji/{emoji_id:[A-Za-z0-9]+}'
   107  	EmojiByName *mux.Router // 'api/v4/emoji/name/{emoji_name:[A-Za-z0-9_-\.]+}'
   108  
   109  	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_-+]+}'
   110  
   111  	TermsOfService *mux.Router // 'api/v4/terms_of_service
   112  	Groups         *mux.Router // 'api/v4/groups'
   113  }
   114  
   115  type API struct {
   116  	ConfigService       configservice.ConfigService
   117  	GetGlobalAppOptions app.AppOptionCreator
   118  	BaseRoutes          *Routes
   119  }
   120  
   121  func Init(configservice configservice.ConfigService, globalOptionsFunc app.AppOptionCreator, root *mux.Router) *API {
   122  	api := &API{
   123  		ConfigService:       configservice,
   124  		GetGlobalAppOptions: globalOptionsFunc,
   125  		BaseRoutes:          &Routes{},
   126  	}
   127  
   128  	api.BaseRoutes.Root = root
   129  	api.BaseRoutes.ApiRoot = root.PathPrefix(model.API_URL_SUFFIX).Subrouter()
   130  
   131  	api.BaseRoutes.Users = api.BaseRoutes.ApiRoot.PathPrefix("/users").Subrouter()
   132  	api.BaseRoutes.User = api.BaseRoutes.ApiRoot.PathPrefix("/users/{user_id:[A-Za-z0-9]+}").Subrouter()
   133  	api.BaseRoutes.UserByUsername = api.BaseRoutes.Users.PathPrefix("/username/{username:[A-Za-z0-9\\_\\-\\.]+}").Subrouter()
   134  	api.BaseRoutes.UserByEmail = api.BaseRoutes.Users.PathPrefix("/email/{email:.+}").Subrouter()
   135  
   136  	api.BaseRoutes.Branches = api.BaseRoutes.ApiRoot.PathPrefix("/branches").Subrouter()
   137  	api.BaseRoutes.BranchesForUser = api.BaseRoutes.User.PathPrefix("/branches").Subrouter()
   138  	api.BaseRoutes.Branch = api.BaseRoutes.Branches.PathPrefix("/{branch_id:[A-Za-z0-9]+}").Subrouter()
   139  	api.BaseRoutes.BranchForUser = api.BaseRoutes.BranchesForUser.PathPrefix("/{branch_id:[A-Za-z0-9]+}").Subrouter()
   140  	api.BaseRoutes.BranchByName = api.BaseRoutes.Branches.PathPrefix("/name/{branch_name:[A-Za-z0-9_-]+}").Subrouter()
   141  	api.BaseRoutes.BranchMembers = api.BaseRoutes.Branch.PathPrefix("/members").Subrouter()
   142  	api.BaseRoutes.BranchMember = api.BaseRoutes.BranchMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter()
   143  	api.BaseRoutes.BranchMembersForUser = api.BaseRoutes.User.PathPrefix("/branches/members").Subrouter()
   144  
   145  	api.BaseRoutes.Classes = api.BaseRoutes.ApiRoot.PathPrefix("/classes").Subrouter()
   146  	api.BaseRoutes.Class = api.BaseRoutes.Classes.PathPrefix("/{class_id:[A-Za-z0-9]+}").Subrouter()
   147  	api.BaseRoutes.ClassForUser = api.BaseRoutes.User.PathPrefix("/classes/{class_id:[A-Za-z0-9]+}").Subrouter()
   148  	api.BaseRoutes.ClassByName = api.BaseRoutes.Branch.PathPrefix("/classes/name/{class_name:[A-Za-z0-9_-]+}").Subrouter()
   149  	api.BaseRoutes.ClassByNameForBranchName = api.BaseRoutes.BranchByName.PathPrefix("/classes/name/{class_name:[A-Za-z0-9_-]+}").Subrouter()
   150  	api.BaseRoutes.ClassesForBranch = api.BaseRoutes.Branch.PathPrefix("/classes").Subrouter()
   151  	api.BaseRoutes.ClassMembers = api.BaseRoutes.Class.PathPrefix("/members").Subrouter()
   152  	api.BaseRoutes.ClassMember = api.BaseRoutes.ClassMembers.PathPrefix("/{user_id:[A-Za-z0-9]+}").Subrouter()
   153  	api.BaseRoutes.ClassMembersForUser = api.BaseRoutes.User.PathPrefix("/branches/{branch_id:[A-Za-z0-9]+}/classes/members").Subrouter()
   154  	api.BaseRoutes.ClassModerations = api.BaseRoutes.Class.PathPrefix("/moderations").Subrouter()
   155  
   156  	api.BaseRoutes.Posts = api.BaseRoutes.ApiRoot.PathPrefix("/posts").Subrouter()
   157  	api.BaseRoutes.Post = api.BaseRoutes.Posts.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
   158  	api.BaseRoutes.PostsForClass = api.BaseRoutes.Class.PathPrefix("/posts").Subrouter()
   159  	api.BaseRoutes.PostsForUser = api.BaseRoutes.User.PathPrefix("/posts").Subrouter()
   160  	api.BaseRoutes.PostForUser = api.BaseRoutes.PostsForUser.PathPrefix("/{post_id:[A-Za-z0-9]+}").Subrouter()
   161  
   162  	api.BaseRoutes.Files = api.BaseRoutes.ApiRoot.PathPrefix("/files").Subrouter()
   163  	api.BaseRoutes.File = api.BaseRoutes.Files.PathPrefix("/{file_id:[A-Za-z0-9]+}").Subrouter()
   164  	api.BaseRoutes.PublicFile = api.BaseRoutes.Root.PathPrefix("/files/{file_id:[A-Za-z0-9]+}/public").Subrouter()
   165  
   166  	api.BaseRoutes.Plugins = api.BaseRoutes.ApiRoot.PathPrefix("/plugins").Subrouter()
   167  	api.BaseRoutes.Plugin = api.BaseRoutes.Plugins.PathPrefix("/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter()
   168  
   169  	api.BaseRoutes.Commands = api.BaseRoutes.ApiRoot.PathPrefix("/commands").Subrouter()
   170  	api.BaseRoutes.Command = api.BaseRoutes.Commands.PathPrefix("/{command_id:[A-Za-z0-9]+}").Subrouter()
   171  
   172  	api.BaseRoutes.Hooks = api.BaseRoutes.ApiRoot.PathPrefix("/hooks").Subrouter()
   173  	api.BaseRoutes.IncomingHooks = api.BaseRoutes.Hooks.PathPrefix("/incoming").Subrouter()
   174  	api.BaseRoutes.IncomingHook = api.BaseRoutes.IncomingHooks.PathPrefix("/{hook_id:[A-Za-z0-9]+}").Subrouter()
   175  	api.BaseRoutes.OutgoingHooks = api.BaseRoutes.Hooks.PathPrefix("/outgoing").Subrouter()
   176  	api.BaseRoutes.OutgoingHook = api.BaseRoutes.OutgoingHooks.PathPrefix("/{hook_id:[A-Za-z0-9]+}").Subrouter()
   177  
   178  	api.BaseRoutes.SAML = api.BaseRoutes.ApiRoot.PathPrefix("/saml").Subrouter()
   179  
   180  	api.BaseRoutes.OAuth = api.BaseRoutes.ApiRoot.PathPrefix("/oauth").Subrouter()
   181  	api.BaseRoutes.OAuthApps = api.BaseRoutes.OAuth.PathPrefix("/apps").Subrouter()
   182  	api.BaseRoutes.OAuthApp = api.BaseRoutes.OAuthApps.PathPrefix("/{app_id:[A-Za-z0-9]+}").Subrouter()
   183  
   184  	api.BaseRoutes.Compliance = api.BaseRoutes.ApiRoot.PathPrefix("/compliance").Subrouter()
   185  	api.BaseRoutes.Cluster = api.BaseRoutes.ApiRoot.PathPrefix("/cluster").Subrouter()
   186  	api.BaseRoutes.LDAP = api.BaseRoutes.ApiRoot.PathPrefix("/ldap").Subrouter()
   187  	api.BaseRoutes.Brand = api.BaseRoutes.ApiRoot.PathPrefix("/brand").Subrouter()
   188  	api.BaseRoutes.System = api.BaseRoutes.ApiRoot.PathPrefix("/system").Subrouter()
   189  	api.BaseRoutes.Preferences = api.BaseRoutes.User.PathPrefix("/preferences").Subrouter()
   190  	api.BaseRoutes.License = api.BaseRoutes.ApiRoot.PathPrefix("/license").Subrouter()
   191  	api.BaseRoutes.Public = api.BaseRoutes.ApiRoot.PathPrefix("/public").Subrouter()
   192  	api.BaseRoutes.Reactions = api.BaseRoutes.ApiRoot.PathPrefix("/reactions").Subrouter()
   193  	api.BaseRoutes.Jobs = api.BaseRoutes.ApiRoot.PathPrefix("/jobs").Subrouter()
   194  	api.BaseRoutes.Elasticsearch = api.BaseRoutes.ApiRoot.PathPrefix("/elasticsearch").Subrouter()
   195  	api.BaseRoutes.DataRetention = api.BaseRoutes.ApiRoot.PathPrefix("/data_retention").Subrouter()
   196  
   197  	api.BaseRoutes.Emojis = api.BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter()
   198  	api.BaseRoutes.Emoji = api.BaseRoutes.ApiRoot.PathPrefix("/emoji/{emoji_id:[A-Za-z0-9]+}").Subrouter()
   199  	api.BaseRoutes.EmojiByName = api.BaseRoutes.Emojis.PathPrefix("/name/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter()
   200  
   201  	api.BaseRoutes.ReactionByNameForPostForUser = api.BaseRoutes.PostForUser.PathPrefix("/reactions/{emoji_name:[A-Za-z0-9\\_\\-\\+]+}").Subrouter()
   202  
   203  	api.BaseRoutes.OpenGraph = api.BaseRoutes.ApiRoot.PathPrefix("/opengraph").Subrouter()
   204  
   205  	api.BaseRoutes.Roles = api.BaseRoutes.ApiRoot.PathPrefix("/roles").Subrouter()
   206  	api.BaseRoutes.Schemes = api.BaseRoutes.ApiRoot.PathPrefix("/schemes").Subrouter()
   207  
   208  	api.BaseRoutes.Image = api.BaseRoutes.ApiRoot.PathPrefix("/image").Subrouter()
   209  
   210  	api.BaseRoutes.TermsOfService = api.BaseRoutes.ApiRoot.PathPrefix("/terms_of_service").Subrouter()
   211  	api.BaseRoutes.Groups = api.BaseRoutes.ApiRoot.PathPrefix("/groups").Subrouter()
   212  
   213  	api.InitUser()
   214  	api.InitBranch()
   215  	api.InitClass()
   216  	api.InitFile()
   217  	api.InitSystem()
   218  	api.InitLicense()
   219  	api.InitConfig()
   220  	//api.InitWebhook()
   221  	api.InitPreference()
   222  	api.InitSaml()
   223  	api.InitCompliance()
   224  	api.InitCluster()
   225  	api.InitLdap()
   226  	api.InitElasticsearch()
   227  	api.InitDataRetention()
   228  	api.InitBrand()
   229  	api.InitJob()
   230  	api.InitCommand()
   231  	api.InitStatus()
   232  	api.InitWebSocket()
   233  	api.InitEmoji()
   234  	api.InitOAuth()
   235  	api.InitReaction()
   236  	api.InitOpenGraph()
   237  	api.InitRole()
   238  	api.InitScheme()
   239  	api.InitImage()
   240  	api.InitTermsOfService()
   241  
   242  	root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
   243  
   244  	return api
   245  }
   246  
   247  func (api *API) Handle404(w http.ResponseWriter, r *http.Request) {
   248  	web.Handle404(api.ConfigService, w, r)
   249  }
   250  
   251  var ReturnStatusOK = web.ReturnStatusOK