Beispiel #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
}
Beispiel #2
0
// prepend servers specified
func NewServer(conf *model.NgxConfWrapper) error {
	file := buildFile(conf)
	exist := util.Exist(file)

	switch exist {
	case true: // prepend server
		return insertServer(conf)
	case false: // new app and backend servers
		return newApp(conf)
	}
	return nil
}
Beispiel #3
0
func DeleteServer(conf *model.NgxConfWrapper) (int, error) {
	file := buildFile(conf)
	exist := util.Exist(file)
	if !exist {
		return 0, errors.New("no such app domain")
	}

	// servers to deleted
	servers := genServers(conf)

	fileMutex.Lock()
	defer fileMutex.Unlock()

	fil, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		return 0, errors.New("open file failed")
	}
	defer fil.Close()

	buffer.Reset()

	scanner := bufio.NewScanner(fil)
	flag := false
	for scanner.Scan() {
		line := scanner.Text()
		for _, server := range servers {
			if strings.Contains(line, server) {
				flag = true
				break
			}
		}
		if !flag {
			// write other lines to buffer
			buffer.Write([]byte(line))
			buffer.Write([]byte{'\n'})
		}
		flag = false
	}

	fil2, err := os.OpenFile(file+".tmp", os.O_RDWR|os.O_CREATE, 0644)
	fil2.Write(buffer.Bytes())
	fil2.Sync()
	fil2.Close()

	os.Rename(file, file+".bak")
	os.Rename(file+".tmp", file)

	return len(servers), nil
}