diff options
author | Harrison Healey <harrisonmhealey@gmail.com> | 2015-09-18 18:11:01 -0400 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2015-09-18 18:11:01 -0400 |
commit | 679e20aa4b74b468c7e372c77219142599f2385d (patch) | |
tree | 544b2a5f932dc2a2cbd27a800fddcb4ce07b057e /utils/config.go | |
parent | 676f2a63999e51a03f2f57209ce4c9f106683da4 (diff) | |
parent | cdf813f07b207c14bb9fa03f38d734b45e40823e (diff) | |
download | chat-679e20aa4b74b468c7e372c77219142599f2385d.tar.gz chat-679e20aa4b74b468c7e372c77219142599f2385d.tar.bz2 chat-679e20aa4b74b468c7e372c77219142599f2385d.zip |
Merge pull request #709 from mattermost/PLT-11
PLT-11 adding config for logs to UI
Diffstat (limited to 'utils/config.go')
-rw-r--r-- | utils/config.go | 180 |
1 files changed, 37 insertions, 143 deletions
diff --git a/utils/config.go b/utils/config.go index 6fd3a9ca7..dd2c17977 100644 --- a/utils/config.go +++ b/utils/config.go @@ -6,6 +6,7 @@ package utils import ( "encoding/json" "fmt" + "io/ioutil" "os" "path/filepath" "strconv" @@ -22,138 +23,9 @@ const ( LOG_ROTATE_SIZE = 10000 ) -type ServiceSettings struct { - SiteName string - Mode string - AllowTesting bool - UseSSL bool - Port string - InviteSalt string - PublicLinkSalt string - ResetSalt string - AnalyticsUrl string - UseLocalStorage bool - StorageDirectory string - AllowedLoginAttempts int - DisableEmailSignUp bool - EnableOAuthServiceProvider bool -} - -type SSOSetting struct { - Allow bool - Secret string - Id string - Scope string - AuthEndpoint string - TokenEndpoint string - UserApiEndpoint string -} - -type SqlSettings struct { - DriverName string - DataSource string - DataSourceReplicas []string - MaxIdleConns int - MaxOpenConns int - Trace bool - AtRestEncryptKey string -} - -type LogSettings struct { - ConsoleEnable bool - ConsoleLevel string - FileEnable bool - FileLevel string - FileFormat string - FileLocation string -} - -type AWSSettings struct { - S3AccessKeyId string - S3SecretAccessKey string - S3Bucket string - S3Region string -} - -type ImageSettings struct { - ThumbnailWidth uint - ThumbnailHeight uint - PreviewWidth uint - PreviewHeight uint - ProfileWidth uint - ProfileHeight uint - InitialFont string -} - -type EmailSettings struct { - ByPassEmail bool - SMTPUsername string - SMTPPassword string - SMTPServer string - UseTLS bool - UseStartTLS bool - FeedbackEmail string - FeedbackName string - ApplePushServer string - ApplePushCertPublic string - ApplePushCertPrivate string -} - -type RateLimitSettings struct { - UseRateLimiter bool - PerSec int - MemoryStoreSize int - VaryByRemoteAddr bool - VaryByHeader string -} - -type PrivacySettings struct { - ShowEmailAddress bool - ShowPhoneNumber bool - ShowSkypeId bool - ShowFullName bool -} - -type ClientSettings struct { - SegmentDeveloperKey string - GoogleDeveloperKey string -} - -type TeamSettings struct { - MaxUsersPerTeam int - AllowPublicLink bool - AllowValetDefault bool - TourLink string - DefaultThemeColor string - DisableTeamCreation bool - RestrictCreationToDomains string -} - -type Config struct { - LogSettings LogSettings - ServiceSettings ServiceSettings - SqlSettings SqlSettings - AWSSettings AWSSettings - ImageSettings ImageSettings - EmailSettings EmailSettings - RateLimitSettings RateLimitSettings - PrivacySettings PrivacySettings - ClientSettings ClientSettings - TeamSettings TeamSettings - SSOSettings map[string]SSOSetting -} - -func (o *Config) ToJson() string { - b, err := json.Marshal(o) - if err != nil { - return "" - } else { - return string(b) - } -} - -var Cfg *Config = &Config{} +var Cfg *model.Config = &model.Config{} var CfgLastModified int64 = 0 +var CfgFileName string = "" var ClientProperties map[string]string = map[string]string{} var SanitizeOptions map[string]bool = map[string]bool{} @@ -185,14 +57,14 @@ func FindDir(dir string) string { } func ConfigureCmdLineLog() { - ls := LogSettings{} + ls := model.LogSettings{} ls.ConsoleEnable = true ls.ConsoleLevel = "ERROR" ls.FileEnable = false configureLog(&ls) } -func configureLog(s *LogSettings) { +func configureLog(s *model.LogSettings) { l4g.Close() @@ -208,12 +80,11 @@ func configureLog(s *LogSettings) { } if s.FileEnable { - if s.FileFormat == "" { - s.FileFormat = "[%D %T] [%L] %M" - } - if s.FileLocation == "" { - s.FileLocation = FindDir("logs") + "mattermost.log" + var fileFormat = s.FileFormat + + if fileFormat == "" { + fileFormat = "[%D %T] [%L] %M" } level := l4g.DEBUG @@ -223,14 +94,36 @@ func configureLog(s *LogSettings) { level = l4g.ERROR } - flw := l4g.NewFileLogWriter(s.FileLocation, false) - flw.SetFormat(s.FileFormat) + flw := l4g.NewFileLogWriter(GetLogFileLocation(s.FileLocation), false) + flw.SetFormat(fileFormat) flw.SetRotate(true) flw.SetRotateLines(LOG_ROTATE_SIZE) l4g.AddFilter("file", level, flw) } } +func GetLogFileLocation(fileLocation string) string { + if fileLocation == "" { + return FindDir("logs") + "mattermost.log" + } else { + return fileLocation + } +} + +func SaveConfig(fileName string, config *model.Config) *model.AppError { + b, err := json.MarshalIndent(config, "", " ") + if err != nil { + return model.NewAppError("SaveConfig", "An error occurred while saving the file to "+fileName, err.Error()) + } + + err = ioutil.WriteFile(fileName, b, 0644) + if err != nil { + return model.NewAppError("SaveConfig", "An error occurred while saving the file to "+fileName, err.Error()) + } + + return nil +} + // LoadConfig will try to search around for the corresponding config file. // It will search /tmp/fileName then attempt ./config/fileName, // then ../config/fileName and last it will look at fileName @@ -244,7 +137,7 @@ func LoadConfig(fileName string) { } decoder := json.NewDecoder(file) - config := Config{} + config := model.Config{} err = decoder.Decode(&config) if err != nil { panic("Error decoding config file=" + fileName + ", err=" + err.Error()) @@ -254,6 +147,7 @@ func LoadConfig(fileName string) { panic("Error getting config info file=" + fileName + ", err=" + err.Error()) } else { CfgLastModified = info.ModTime().Unix() + CfgFileName = fileName } configureLog(&config.LogSettings) @@ -263,7 +157,7 @@ func LoadConfig(fileName string) { ClientProperties = getClientProperties(Cfg) } -func getSanitizeOptions(c *Config) map[string]bool { +func getSanitizeOptions(c *model.Config) map[string]bool { options := map[string]bool{} options["fullname"] = c.PrivacySettings.ShowFullName options["email"] = c.PrivacySettings.ShowEmailAddress @@ -273,7 +167,7 @@ func getSanitizeOptions(c *Config) map[string]bool { return options } -func getClientProperties(c *Config) map[string]string { +func getClientProperties(c *model.Config) map[string]string { props := make(map[string]string) props["Version"] = model.CurrentVersion |