func hupSignalHandler() { log.Info("Received HUP signal, log reopened") log.Reopen() }
func termSignalHandler() { log.Info("Received TERM signal, shutdown...") os.Exit(0) }
// Load load and parse all rules func (obs *Observer) Load() bool { var ok = true for _, r := range obs.uriMap { if !fsutil.IsExist(r.Path) { delete(obs.uriMap, r.Request.URI) delete(obs.wcMap, r.Path) delete(obs.errMap, r.Path) delete(obs.pathMap, r.Path) delete(obs.nameMap[r.Service], r.FullName) // If no one rule found for service, remove it's own map if len(obs.nameMap[r.Service]) == 0 { delete(obs.nameMap, r.Service) } log.Info("Rule %s unloaded (mock file deleted)", r.PrettyPath) continue } mtime, _ := fsutil.GetMTime(r.Path) if r.ModTime.UnixNano() != mtime.UnixNano() { rule, err := Parse(obs.ruleDir, r.Service, r.Dir, r.Name) if err != nil { log.Error(err.Error()) ok = false continue } // URI can be changed, remove rule from uri map anyway delete(obs.uriMap, r.Request.URI) delete(obs.errMap, r.Path) if r.IsWildcard { delete(obs.wcMap, r.Path) } obs.uriMap[rule.Request.URI] = rule obs.pathMap[rule.Path] = rule if rule.IsWildcard { obs.wcMap[rule.Path] = rule } log.Info("Rule %s reloaded", rule.PrettyPath) } } if !fsutil.CheckPerms("DRX", obs.ruleDir) { log.Error("Can't read directory with rules (%s)", obs.ruleDir) return false } rules := fsutil.ListAllFiles( obs.ruleDir, true, &fsutil.ListingFilter{ MatchPatterns: []string{"*.mock"}, }, ) if len(rules) == 0 { return ok } if !obs.checkRules(rules) { ok = false } return ok }
func intSignalHandler() { log.Info("Received INT signal, shutdown...") os.Exit(0) }
func (obs *Observer) checkRules(rules []string) bool { var ok = true RULELOOP: for _, rulePath := range rules { service, mockFile, dir := ParsePath(rulePath) if mockFile == "" { log.Error("Can't use rule %s: rule file must be placed inside service directory (e.g. rules/my-service/rule.mock)", rulePath) continue } fullPath := path.Join(obs.ruleDir, rulePath) mockName := strings.Replace(mockFile, ".mock", "", -1) if obs.pathMap[fullPath] != nil { continue } rule, err := Parse(obs.ruleDir, service, dir, mockName) if err != nil { if obs.errMap[fullPath] != true { log.Error("Can't parse rule %s: %v", path.Join(service, dir, mockName), err) obs.errMap[fullPath] = true ok = false } continue } for _, r := range obs.wcMap { if r.Request.Method != rule.Request.Method { continue } if r.Request.Host != rule.Request.Host { continue } if urlutil.EqualPatterns(r.Request.NURL, rule.Request.NURL) { if obs.errMap[rule.Path] != true { log.Error("Rule intersection: rule %s and rule %s have same result urls", r.PrettyPath, rule.PrettyPath) obs.errMap[rule.Path] = true ok = false } continue RULELOOP } } delete(obs.errMap, rule.Path) obs.uriMap[rule.Request.URI] = rule obs.pathMap[rule.Path] = rule obs.srvMap[service] = true if rule.IsWildcard { obs.wcMap[rule.Path] = rule } if obs.nameMap[service] == nil { obs.nameMap[service] = make(RuleMap) } obs.nameMap[service][rule.FullName] = rule log.Info("Rule %s loaded", rule.PrettyPath) } return ok }