From 09863c0b80610f2f3a35cf3caa7c5b66a0c3878e Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Tue, 24 May 2016 14:31:30 -0700 Subject: Adding APIs to reload config, recycle db connections and ping server (#3096) * Adding APIs to reload config, recycle db connections and ping server * Fixing unit test * Adding unit tests --- api/admin.go | 61 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'api/admin.go') diff --git a/api/admin.go b/api/admin.go index c2dfa37d0..52e412976 100644 --- a/api/admin.go +++ b/api/admin.go @@ -10,11 +10,13 @@ import ( "os" "strconv" "strings" + "time" l4g "github.com/alecthomas/log4go" "github.com/gorilla/mux" "github.com/mattermost/platform/einterfaces" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" "github.com/mssola/user_agent" ) @@ -26,9 +28,9 @@ func InitAdmin() { BaseRoutes.Admin.Handle("/audits", ApiUserRequired(getAllAudits)).Methods("GET") BaseRoutes.Admin.Handle("/config", ApiUserRequired(getConfig)).Methods("GET") BaseRoutes.Admin.Handle("/save_config", ApiUserRequired(saveConfig)).Methods("POST") + BaseRoutes.Admin.Handle("/reload_config", ApiUserRequired(reloadConfig)).Methods("GET") BaseRoutes.Admin.Handle("/test_email", ApiUserRequired(testEmail)).Methods("POST") - BaseRoutes.Admin.Handle("/client_props", ApiAppHandler(getClientConfig)).Methods("GET") - BaseRoutes.Admin.Handle("/log_client", ApiAppHandler(logClient)).Methods("POST") + BaseRoutes.Admin.Handle("/recycle_db_conn", ApiUserRequired(recycleDatabaseConnection)).Methods("GET") BaseRoutes.Admin.Handle("/analytics/{id:[A-Za-z0-9]+}/{name:[A-Za-z0-9_]+}", ApiUserRequired(getAnalytics)).Methods("GET") BaseRoutes.Admin.Handle("/analytics/{name:[A-Za-z0-9_]+}", ApiUserRequired(getAnalytics)).Methods("GET") BaseRoutes.Admin.Handle("/save_compliance_report", ApiUserRequired(saveComplianceReport)).Methods("POST") @@ -94,32 +96,6 @@ func getAllAudits(c *Context, w http.ResponseWriter, r *http.Request) { } } -func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) { - w.Write([]byte(model.MapToJson(utils.ClientCfg))) -} - -func logClient(c *Context, w http.ResponseWriter, r *http.Request) { - m := model.MapFromJson(r.Body) - - lvl := m["level"] - msg := m["message"] - - if len(msg) > 400 { - msg = msg[0:399] - } - - if lvl == "ERROR" { - err := &model.AppError{} - err.Message = msg - err.Where = "client" - c.LogError(err) - } - - rm := make(map[string]string) - rm["SUCCESS"] = "true" - w.Write([]byte(model.MapToJson(rm))) -} - func getConfig(c *Context, w http.ResponseWriter, r *http.Request) { if !c.HasSystemAdminPermissions("getConfig") { return @@ -134,6 +110,16 @@ func getConfig(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(cfg.ToJson())) } +func reloadConfig(c *Context, w http.ResponseWriter, r *http.Request) { + if !c.HasSystemAdminPermissions("reloadConfig") { + return + } + + utils.LoadConfig(utils.CfgFileName) + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + ReturnStatusOK(w) +} + func saveConfig(c *Context, w http.ResponseWriter, r *http.Request) { if !c.HasSystemAdminPermissions("getConfig") { return @@ -168,6 +154,25 @@ func saveConfig(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.MapToJson(rdata))) } +func recycleDatabaseConnection(c *Context, w http.ResponseWriter, r *http.Request) { + if !c.HasSystemAdminPermissions("recycleDatabaseConnection") { + return + } + + oldStore := Srv.Store + + l4g.Warn(utils.T("api.admin.recycle_db_start.warn")) + Srv.Store = store.NewSqlStore() + + time.Sleep(20 * time.Second) + oldStore.Close() + + l4g.Warn(utils.T("api.admin.recycle_db_end.warn")) + + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + ReturnStatusOK(w) +} + func testEmail(c *Context, w http.ResponseWriter, r *http.Request) { if !c.HasSystemAdminPermissions("testEmail") { return -- cgit v1.2.3-1-g7c22