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

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"strings"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func (api *API) InitGeneral() {
    15  	api.BaseRoutes.General.Handle("/client_props", api.ApiAppHandler(getClientConfig)).Methods("GET")
    16  	api.BaseRoutes.General.Handle("/log_client", api.ApiAppHandler(logClient)).Methods("POST")
    17  	api.BaseRoutes.General.Handle("/ping", api.ApiAppHandler(ping)).Methods("GET")
    18  }
    19  
    20  func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
    21  	w.Write([]byte(model.MapToJson(c.App.ClientConfig())))
    22  }
    23  
    24  func logClient(c *Context, w http.ResponseWriter, r *http.Request) {
    25  	forceToDebug := false
    26  
    27  	if !*c.App.Config().ServiceSettings.EnableDeveloper {
    28  		if c.Session.UserId == "" {
    29  			c.Err = model.NewAppError("Permissions", "api.context.permissions.app_error", nil, "", http.StatusForbidden)
    30  			return
    31  		}
    32  
    33  		if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
    34  			forceToDebug = true
    35  		}
    36  	}
    37  
    38  	m := model.MapFromJson(r.Body)
    39  
    40  	lvl := m["level"]
    41  	msg := m["message"]
    42  
    43  	// filter out javascript errors from franz that are polluting the log files
    44  	if strings.Contains(msg, "/franz") {
    45  		forceToDebug = true
    46  	}
    47  
    48  	if len(msg) > 400 {
    49  		msg = msg[0:399]
    50  	}
    51  
    52  	if lvl == "ERROR" {
    53  		err := &model.AppError{}
    54  		err.Message = msg
    55  		err.Id = msg
    56  		err.Where = "client"
    57  
    58  		if forceToDebug {
    59  			c.LogDebug(err)
    60  		} else {
    61  			c.LogError(err)
    62  		}
    63  	}
    64  
    65  	ReturnStatusOK(w)
    66  }
    67  
    68  func ping(c *Context, w http.ResponseWriter, r *http.Request) {
    69  	m := make(map[string]string)
    70  	m["version"] = model.CurrentVersion
    71  	m["server_time"] = fmt.Sprintf("%v", model.GetMillis())
    72  	w.Write([]byte(model.MapToJson(m)))
    73  }