diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/config.go | 123 |
1 files changed, 77 insertions, 46 deletions
diff --git a/utils/config.go b/utils/config.go index f49840453..0eb8329d1 100644 --- a/utils/config.go +++ b/utils/config.go @@ -4,34 +4,38 @@ package utils import ( - l4g "code.google.com/p/log4go" "encoding/json" - "net/mail" + "fmt" "os" "path/filepath" + "strconv" + + l4g "code.google.com/p/log4go" ) const ( - MODE_DEV = "dev" - MODE_BETA = "beta" - MODE_PROD = "prod" + MODE_DEV = "dev" + MODE_BETA = "beta" + MODE_PROD = "prod" + LOG_ROTATE_SIZE = 10000 ) type ServiceSettings struct { - SiteName string - Mode string - AllowTesting bool - UseSSL bool - Port string - Version string - InviteSalt string - PublicLinkSalt string - ResetSalt string - AnalyticsUrl string - UseLocalStorage bool - StorageDirectory string - AllowedLoginAttempts int - DisableEmailSignUp bool + SiteName string + Mode string + AllowTesting bool + UseSSL bool + Port string + Version string + InviteSalt string + PublicLinkSalt string + ResetSalt string + AnalyticsUrl string + UseLocalStorage bool + StorageDirectory string + AllowedLoginAttempts int + DisableEmailSignUp bool + EnableOAuthServiceProvider bool } type SSOSetting struct { @@ -109,15 +113,15 @@ type PrivacySettings struct { ShowFullName bool } +type ClientSettings struct { + SegmentDeveloperKey string + GoogleDeveloperKey string +} + type TeamSettings struct { MaxUsersPerTeam int AllowPublicLink bool AllowValetDefault bool - TermsLink string - PrivacyLink string - AboutLink string - HelpLink string - ReportProblemLink string TourLink string DefaultThemeColor string DisableTeamCreation bool @@ -133,6 +137,7 @@ type Config struct { EmailSettings EmailSettings RateLimitSettings RateLimitSettings PrivacySettings PrivacySettings + ClientSettings ClientSettings TeamSettings TeamSettings SSOSettings map[string]SSOSetting } @@ -147,9 +152,11 @@ func (o *Config) ToJson() string { } var Cfg *Config = &Config{} +var CfgLastModified int64 = 0 +var ClientProperties map[string]string = map[string]string{} var SanitizeOptions map[string]bool = map[string]bool{} -func findConfigFile(fileName string) string { +func FindConfigFile(fileName string) string { if _, err := os.Stat("/tmp/" + fileName); err == nil { fileName, _ = filepath.Abs("/tmp/" + fileName) } else if _, err := os.Stat("./config/" + fileName); err == nil { @@ -176,7 +183,15 @@ func FindDir(dir string) string { return fileName + "/" } -func configureLog(s LogSettings) { +func ConfigureCmdLineLog() { + ls := LogSettings{} + ls.ConsoleEnable = true + ls.ConsoleLevel = "ERROR" + ls.FileEnable = false + configureLog(&ls) +} + +func configureLog(s *LogSettings) { l4g.Close() @@ -210,7 +225,7 @@ func configureLog(s LogSettings) { flw := l4g.NewFileLogWriter(s.FileLocation, false) flw.SetFormat(s.FileFormat) flw.SetRotate(true) - flw.SetRotateLines(100000) + flw.SetRotateLines(LOG_ROTATE_SIZE) l4g.AddFilter("file", level, flw) } } @@ -220,8 +235,7 @@ func configureLog(s LogSettings) { // then ../config/fileName and last it will look at fileName func LoadConfig(fileName string) { - fileName = findConfigFile(fileName) - l4g.Info("Loading config file at " + fileName) + fileName = FindConfigFile(fileName) file, err := os.Open(fileName) if err != nil { @@ -232,36 +246,53 @@ func LoadConfig(fileName string) { config := Config{} err = decoder.Decode(&config) if err != nil { - panic("Error decoding configuration " + err.Error()) + panic("Error decoding config file=" + fileName + ", err=" + err.Error()) } - // Check for a valid email for feedback, if not then do feedback@domain - if _, err := mail.ParseAddress(config.EmailSettings.FeedbackEmail); err != nil { - l4g.Error("Misconfigured feedback email setting: %s", config.EmailSettings.FeedbackEmail) - config.EmailSettings.FeedbackEmail = "feedback@localhost" + if info, err := file.Stat(); err != nil { + panic("Error getting config info file=" + fileName + ", err=" + err.Error()) + } else { + CfgLastModified = info.ModTime().Unix() } - configureLog(config.LogSettings) + configureLog(&config.LogSettings) Cfg = &config - SanitizeOptions = getSanitizeOptions() - - // Validates our mail settings - if err := CheckMailSettings(); err != nil { - l4g.Error("Email settings are not valid err=%v", err) - } + SanitizeOptions = getSanitizeOptions(Cfg) + ClientProperties = getClientProperties(Cfg) } -func getSanitizeOptions() map[string]bool { +func getSanitizeOptions(c *Config) map[string]bool { options := map[string]bool{} - options["fullname"] = Cfg.PrivacySettings.ShowFullName - options["email"] = Cfg.PrivacySettings.ShowEmailAddress - options["skypeid"] = Cfg.PrivacySettings.ShowSkypeId - options["phonenumber"] = Cfg.PrivacySettings.ShowPhoneNumber + options["fullname"] = c.PrivacySettings.ShowFullName + options["email"] = c.PrivacySettings.ShowEmailAddress + options["skypeid"] = c.PrivacySettings.ShowSkypeId + options["phonenumber"] = c.PrivacySettings.ShowPhoneNumber return options } +func getClientProperties(c *Config) map[string]string { + props := make(map[string]string) + + props["Version"] = c.ServiceSettings.Version + props["SiteName"] = c.ServiceSettings.SiteName + props["ByPassEmail"] = strconv.FormatBool(c.EmailSettings.ByPassEmail) + props["FeedbackEmail"] = c.EmailSettings.FeedbackEmail + props["ShowEmailAddress"] = strconv.FormatBool(c.PrivacySettings.ShowEmailAddress) + props["AllowPublicLink"] = strconv.FormatBool(c.TeamSettings.AllowPublicLink) + props["SegmentDeveloperKey"] = c.ClientSettings.SegmentDeveloperKey + props["GoogleDeveloperKey"] = c.ClientSettings.GoogleDeveloperKey + props["AnalyticsUrl"] = c.ServiceSettings.AnalyticsUrl + props["ByPassEmail"] = strconv.FormatBool(c.EmailSettings.ByPassEmail) + props["ProfileHeight"] = fmt.Sprintf("%v", c.ImageSettings.ProfileHeight) + props["ProfileWidth"] = fmt.Sprintf("%v", c.ImageSettings.ProfileWidth) + props["ProfileWidth"] = fmt.Sprintf("%v", c.ImageSettings.ProfileWidth) + props["EnableOAuthServiceProvider"] = strconv.FormatBool(c.ServiceSettings.EnableOAuthServiceProvider) + + return props +} + func IsS3Configured() bool { if Cfg.AWSSettings.S3AccessKeyId == "" || Cfg.AWSSettings.S3SecretAccessKey == "" || Cfg.AWSSettings.S3Region == "" || Cfg.AWSSettings.S3Bucket == "" { return false |