Ejemplo n.º 1
0
func Delete(domain string, dir string) error {
	if domain == "" {
		return errors.New("empty domain")
	}

	fileMutex.Lock()
	defer fileMutex.Unlock()

	if dir == "" {
		dir = NGX_CONF_PATH
	}

	file := path.Join(dir, "conf.d", fmt.Sprintf("%s.conf", domain))
	filebak := path.Join(dir, "conf.d", fmt.Sprintf("%s.conf.bak", domain))

	exist := util.Exist(filebak)
	if exist {
		os.Remove(filebak)
	}

	err := os.Rename(file, file+".bak")
	if err != nil {
		util.Log("rename app conf file error, name: " + file)
		return errors.New("delete file failed")
	}

	return nil
}
Ejemplo n.º 2
0
func Servers(domain string, confpath string) []model.RealServer {
	if domain == "" {
		return nil
	}

	if confpath == "" {
		confpath = NGX_CONF_PATH
	}

	fileMutex.Lock()
	defer fileMutex.Unlock()

	file := path.Join(confpath, "conf.d", fmt.Sprintf("%s.conf", domain))

	// read all server lines in upstream block
	fil, err := os.Open(file)
	if err != nil {
		util.Log("open app conf file error, name: " + file)
		return nil
	}
	defer fil.Close()

	lines := make([]string, 0)
	match := fmt.Sprintf("backend_%s", domain)
	flag := false

	scanner := bufio.NewScanner(fil)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Contains(line, "}") {
			flag = false
			break
		}
		if flag {
			lines = append(lines, line)
		}
		if strings.Contains(line, "upstream") && strings.Contains(line, match) {
			flag = true
		}
	}

	res := make([]model.RealServer, 0)
	for _, line := range lines {
		tline := strings.Trim(line, " ")
		if tline == "" {
			break
		}
		var s1, s2, s3, s4, s5 string
		// var port, weight, timeout string
		// fmt.Sscanf(tline, "    server %s:%d weight=%d max_fails=2 fail_timeout=%ds", &ip, &port, &weight, &timeout)
		fmt.Sscanf(line, "%s %s %s %s %s", &s1, &s2, &s3, &s4, &s5)
		s6 := strings.Split(s2, ":")
		ip := s6[0]
		port, _ := strconv.Atoi(s6[1])
		s7 := strings.Split(s3, "=")
		weight, _ := strconv.Atoi(s7[1])
		s8 := strings.Split(s5, "=")
		timeout, _ := strconv.Atoi(strings.Trim(s8[1], "s"))
		rs := model.RealServer{ip, port, weight, timeout}
		res = append(res, rs)
	}

	return res
}