github.com/spline-fu/mattermost-server@v4.10.10+incompatible/api4/elasticsearch.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/mattermost/mattermost-server/model"
    10  )
    11  
    12  func (api *API) InitElasticsearch() {
    13  	api.BaseRoutes.Elasticsearch.Handle("/test", api.ApiSessionRequired(testElasticsearch)).Methods("POST")
    14  	api.BaseRoutes.Elasticsearch.Handle("/purge_indexes", api.ApiSessionRequired(purgeElasticsearchIndexes)).Methods("POST")
    15  }
    16  
    17  func testElasticsearch(c *Context, w http.ResponseWriter, r *http.Request) {
    18  	cfg := model.ConfigFromJson(r.Body)
    19  	if cfg == nil {
    20  		cfg = c.App.Config()
    21  	}
    22  
    23  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    24  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    25  		return
    26  	}
    27  
    28  	if err := c.App.TestElasticsearch(cfg); err != nil {
    29  		c.Err = err
    30  		return
    31  	}
    32  
    33  	ReturnStatusOK(w)
    34  }
    35  
    36  func purgeElasticsearchIndexes(c *Context, w http.ResponseWriter, r *http.Request) {
    37  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    38  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    39  		return
    40  	}
    41  
    42  	if err := c.App.PurgeElasticsearchIndexes(); err != nil {
    43  		c.Err = err
    44  		return
    45  	}
    46  
    47  	ReturnStatusOK(w)
    48  }