예제 #1
0
파일: config.go 프로젝트: ZiRo-/srndv2
// read config files
func ReadConfig() *SRNdConfig {

	// begin read srnd.ini

	fname := "srnd.ini"
	var s *configparser.Section
	conf, err := configparser.Read(fname)
	if err != nil {
		log.Fatal("cannot read config file", fname)
		return nil
	}
	var sconf SRNdConfig

	s, err = conf.Section("pprof")
	if err == nil {
		opts := s.Options()
		sconf.pprof = new(ProfilingConfig)
		sconf.pprof.enable = opts["enable"] == "1"
		sconf.pprof.bind = opts["bind"]
	}

	s, err = conf.Section("crypto")
	if err == nil {
		opts := s.Options()
		sconf.crypto = new(CryptoConfig)
		k := opts["tls-keyname"]
		h := opts["tls-hostname"]
		if strings.HasPrefix(h, "!") || len(h) == 0 {
			log.Fatal("please set tls-hostname to be the hostname or ip address of your server")
		} else {
			sconf.crypto.hostname = h
			sconf.crypto.privkey_file = k + "-" + h + ".key"
			sconf.crypto.cert_dir = opts["tls-trust-dir"]
			sconf.crypto.cert_file = filepath.Join(sconf.crypto.cert_dir, k+"-"+h+".crt")
		}
	} else {
		// we have no crypto section
		log.Println("!!! we will not use encryption for nntp as no crypto section is specified in srnd.ini")
	}
	s, err = conf.Section("nntp")
	if err != nil {
		log.Println("no section 'nntp' in srnd.ini")
		return nil
	}

	sconf.daemon = s.Options()

	s, err = conf.Section("database")
	if err != nil {
		log.Println("no section 'database' in srnd.ini")
		return nil
	}

	sconf.database = s.Options()

	s, err = conf.Section("cache")
	if err != nil {
		log.Println("no section 'cache' in srnd.ini")
		log.Println("falling back to default cache config")
		sconf.cache = make(map[string]string)
		sconf.cache["type"] = "file"
	} else {
		sconf.cache = s.Options()
	}

	s, err = conf.Section("articles")
	if err != nil {
		log.Println("no section 'articles' in srnd.ini")
		return nil
	}

	sconf.store = s.Options()

	// frontend config

	s, err = conf.Section("frontend")

	if err != nil {
		log.Println("no frontend section in srnd.ini, disabling frontend")
		sconf.frontend = make(map[string]string)
		sconf.frontend["enable"] = "0"
	} else {
		log.Println("frontend configured in srnd.ini")
		sconf.frontend = s.Options()
		_, ok := sconf.frontend["enable"]
		if !ok {
			// default to "0"
			sconf.frontend["enable"] = "0"
		}
		enable, _ := sconf.frontend["enable"]
		if enable == "1" {
			log.Println("frontend enabled in srnd.ini")
		} else {
			log.Println("frontend not enabled in srnd.ini, disabling frontend")
		}
	}

	// begin load feeds.ini

	fname = "feeds.ini"
	conf, err = configparser.Read(fname)

	if err != nil {
		log.Fatal("cannot read config file", fname)
		return nil
	}

	sections, err := conf.Find("feed-*")
	if err != nil {
		log.Fatal("failed to load feeds.ini", err)
	}

	var num_sections int
	num_sections = len(sections)

	if num_sections > 0 {
		sconf.feeds = make([]FeedConfig, num_sections)
		idx := 0

		// load feeds
		for _, sect := range sections {
			var fconf FeedConfig
			// check for proxy settings
			val := sect.ValueOf("proxy-type")
			if len(val) > 0 && strings.ToLower(val) != "none" {
				fconf.proxy_type = strings.ToLower(val)
				proxy_host := sect.ValueOf("proxy-host")
				proxy_port := sect.ValueOf("proxy-port")
				fconf.proxy_addr = strings.Trim(proxy_host, " ") + ":" + strings.Trim(proxy_port, " ")
			}

			host := sect.ValueOf("host")
			port := sect.ValueOf("port")

			// check to see if we want to sync with them first
			val = sect.ValueOf("sync")
			if val == "1" {
				fconf.sync = true
				// sync interval in seconds
				i := mapGetInt(sect.Options(), "sync-interval", 60)
				if i < 60 {
					i = 60
				}
				fconf.sync_interval = time.Second * time.Duration(i)
			}

			// username / password auth
			fconf.username = sect.ValueOf("username")
			fconf.passwd = sect.ValueOf("password")
			fconf.tls_off = sect.ValueOf("disabletls") == "1"

			// load feed polcies
			sect_name := sect.Name()[5:]
			fconf.Name = sect_name
			if len(host) > 0 && len(port) > 0 {
				// host port specified
				fconf.Addr = host + ":" + port
			} else {
				// no host / port specified
				fconf.Addr = strings.Trim(sect_name, " ")
			}
			feed_sect, err := conf.Section(sect_name)
			if err != nil {
				log.Fatal("no section", sect_name, "in feeds.ini")
			}
			opts := feed_sect.Options()
			fconf.policy.rules = make(map[string]string)
			for k, v := range opts {
				fconf.policy.rules[k] = v
			}
			sconf.feeds[idx] = fconf
			idx += 1
		}
	}

	// feed quarks
	sections, err = conf.Find("quarks-*")
	if err == nil {
		// we have quarks? neat, let's load them
		// does not check for anything specific
		for _, sect := range sections {
			sect_name := sect.Name()[7:]
			// find the feed for this quark
			for idx, fconf := range sconf.feeds {
				if fconf.Addr == sect_name {
					// yup this is the one
					sconf.feeds[idx].quarks = sect.Options()
				}
			}
		}
	}

	return &sconf
}
예제 #2
0
파일: config.go 프로젝트: kurtcoke/srndv2
// read config files
func ReadConfig() *SRNdConfig {

	// begin read srnd.ini

	fname := "srnd.ini"
	var s *configparser.Section
	conf, err := configparser.Read(fname)
	if err != nil {
		log.Fatal("cannot read config file", fname)
		return nil
	}
	var sconf SRNdConfig

	s, err = conf.Section("nntp")
	if err != nil {
		log.Println("no section 'nntp' in srnd.ini")
		return nil
	}

	sconf.daemon = s.Options()

	s, err = conf.Section("database")
	if err != nil {
		log.Println("no section 'database' in srnd.ini")
		return nil
	}

	sconf.database = s.Options()

	s, err = conf.Section("articles")
	if err != nil {
		log.Println("no section 'articles' in srnd.ini")
		return nil
	}

	sconf.store = s.Options()

	// frontend config

	s, err = conf.Section("frontend")

	if err != nil {
		log.Println("no frontend section in srnd.ini, disabling frontend")
		sconf.frontend = make(map[string]string)
		sconf.frontend["enable"] = "0"
	} else {
		log.Println("frontend configured in srnd.ini")
		sconf.frontend = s.Options()
		_, ok := sconf.frontend["enable"]
		if !ok {
			// default to "0"
			sconf.frontend["enable"] = "0"
		}
		enable, _ := sconf.frontend["enable"]
		if enable == "1" {
			log.Println("frontend enabled in srnd.ini")
		} else {
			log.Println("frontend not enabled in srnd.ini, disabling frontend")
		}
	}

	// begin load feeds.ini

	fname = "feeds.ini"
	conf, err = configparser.Read(fname)

	if err != nil {
		log.Fatal("cannot read config file", fname)
		return nil
	}

	sections, err := conf.Find("feed-*")
	if err != nil {
		log.Fatal("failed to load feeds.ini", err)
	}

	var num_sections int
	num_sections = len(sections)

	if num_sections > 0 {
		sconf.feeds = make([]FeedConfig, num_sections)
		idx := 0

		// load feeds
		for _, sect := range sections {
			var fconf FeedConfig
			// check for proxy settings
			val := sect.ValueOf("proxy-type")
			if len(val) > 0 && strings.ToLower(val) != "none" {
				fconf.proxy_type = strings.ToLower(val)
				proxy_host := sect.ValueOf("proxy-host")
				proxy_port := sect.ValueOf("proxy-port")
				fconf.proxy_addr = strings.Trim(proxy_host, " ") + ":" + strings.Trim(proxy_port, " ")
			}

			// load feed polcies
			sect_name := sect.Name()[5:]
			fconf.addr = strings.Trim(sect_name, " ")
			feed_sect, err := conf.Section(sect_name)
			if err != nil {
				log.Fatal("no section", sect_name, "in feeds.ini")
			}
			opts := feed_sect.Options()
			fconf.policy.rules = make(map[string]string)
			for k, v := range opts {
				fconf.policy.rules[k] = v
			}
			sconf.feeds[idx] = fconf
			idx += 1
		}
	}

	// feed quarks
	sections, err = conf.Find("quarks-*")
	if err == nil {
		// we have quarks? neat, let's load them
		// does not check for anything specific
		for _, sect := range sections {
			sect_name := sect.Name()[7:]
			// find the feed for this quark
			for idx, fconf := range sconf.feeds {
				if fconf.addr == sect_name {
					// yup this is the one
					sconf.feeds[idx].quarks = sect.Options()
				}
			}
		}
	}

	return &sconf
}