Exemplo n.º 1
0
func initConfig() error {
	conf, err := config.ReadDefault(*configFile)
	if nil != err {
		return err
	}

	//dictionaries
	dicts := make(map[string]string)
	dictOpts, cfgerr := conf.Options("dictionary")
	if nil != cfgerr {
		return cfgerr
	}
	for _, dictOpt := range dictOpts {
		dictFile, cfgerr := conf.String("dictionary", dictOpt)
		if nil != cfgerr {
			return cfgerr
		}
		dicts[dictOpt] = dictFile
	}
	//logLevel
	loglevel, err := conf.String("server", "logLevel")
	if nil != err {
		loglevel = "UNSPECIFIED"
	}
	//listenAddr
	listenAddr, err := conf.String("server", "listenAddr")
	if nil != err {
		listenAddr = ":80"
	}
	cfgDictionaries = dicts
	cfgLogLevel = loglevel
	cfgListenAddr = listenAddr
	return nil

}
Exemplo n.º 2
0
func main() {

	flag.Parse()

	/* 1.load the config file and assign port/logfile */
	port := DEFAULT_PORT
	logfile := DEFAULT_LOG

	if _, err := os.Stat(*configFile); os.IsNotExist(err) {
		log.Fatalf("config file:%s not exists!", *configFile)
		os.Exit(1)
	}

	c, err := config.ReadDefault(*configFile)
	if nil != err {
		port = DEFAULT_PORT
		logfile = DEFAULT_LOG
	}
	port, err = c.String("service", "port")
	if nil != err {
		port = DEFAULT_PORT
	}
	logfile, err = c.String("service", "logfile")
	if nil != err {
		logfile = DEFAULT_LOG
	}

	os.MkdirAll(filepath.Dir(logfile), 0777)
	f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE, 0666)
	log.SetOutput(f)

	captcha, err := gocaptcha.CreateCaptchaFromConfigFile(*configFile)

	if nil != err {
		log.Fatalf("config load failed:%s", err.Error())
	} else {
		ccaptcha = captcha
	}

	/* 2. bind handler */
	http.HandleFunc("/showimage", ShowImageHandler)
	http.HandleFunc("/getkey", GetKeyHandler)
	http.HandleFunc("/verify", VerifyHandler)
	http.HandleFunc("/", IndexHandler)

	/* 3. run the http server */
	s := &http.Server{Addr: ":" + port}

	log.Printf("=======ready to serve=======")
	log.Fatal(s.ListenAndServe())
	f.Close()
}
Exemplo n.º 3
0
func loadConfigFromFile(configFile string) (error, string, *CaptchaConfig, *ImageConfig, *FilterConfig, *StoreConfig) {

	var retErr error

	c, err := config.ReadDefault(configFile)

	//wordDict
	wordDict, err := c.String("captcha", "word_dict")
	if nil != err {
		retErr = errors.New("loadConfigFromFile Fail,Get word_dict options failed:" + err.Error())
	}
	//captchaConfig
	captchaConfig := new(CaptchaConfig)
	var lifeTime time.Duration

	cfgLifeTime, err := c.String("captcha", "life_time")
	if nil == err {
		lifeTime, err = time.ParseDuration(cfgLifeTime)
		if nil != err {
			lifeTime = DEFAULT_LIFE_TIME
		}
	} else {
		lifeTime = DEFAULT_LIFE_TIME
	}

	captchaConfig.LifeTime = lifeTime

	//imageConfig
	imageConfig := new(ImageConfig)
	var fontFiles []string
	cfgFontFiles, err := c.StringMuti("image", "font_files")
	if nil != err {
		retErr = errors.New("loadConfigFromFile Fail,font_files options failed:" + err.Error())
	} else {
		fontFiles = cfgFontFiles
	}

	imageConfig.FontFiles = fontFiles

	var fontSize float64
	cfgFontSize, err := c.Int("image", "font_size")
	if nil != err {
		fontSize = DEFAULT_FONT_SIZE
	} else {
		fontSize = float64(cfgFontSize)
	}

	imageConfig.FontSize = fontSize

	var width int
	cfgWidth, err := c.Int("image", "width")
	if nil != err {
		width = DEFAULT_WIDTH
	} else {
		width = int(cfgWidth)
	}
	imageConfig.Width = width

	var height int
	cfgHeight, err := c.Int("image", "height")
	if nil != err {
		height = DEFAULT_HEIGHT
	} else {
		height = int(cfgHeight)
	}
	imageConfig.Height = height

	//filterConfig
	filterConfig := new(FilterConfig)
	filterConfig.Init()
	cfgOpenFilter, err := c.StringMuti("filter", "open_filter")
	if nil == err {
		filterConfig.Filters = cfgOpenFilter
	} else {
		filterConfig.Filters = []string{}
	}
	for _, section := range c.Sections() {
		if strings.HasPrefix(section, "ImageFilter") {
			filterConfigGroup := new(FilterConfigGroup)
			filterConfigGroup.Init()
			options, err := c.Options(section)
			if nil == err {
				for _, option := range options {
					v, err := c.String(section, option)
					if nil == err {
						filterConfigGroup.SetItem(option, v)
					}
				}
			}
			filterConfig.SetGroup(section, filterConfigGroup)
		}
	}

	//storeConfig
	storeConfig := new(StoreConfig)
	storeConfig.CaptchaConfig = *captchaConfig
	engine, err := c.String("store", "engine")
	if nil != err {
		retErr = errors.New("loadConfigFromFile Fail,engine options failed" + err.Error())
	} else {
		storeConfig.Engine = engine
	}
	servers, err := c.StringMuti("store", "servers")
	if nil != err {
		storeConfig.Servers = []string{}
	} else {
		storeConfig.Servers = servers
	}
	gcProbability, err := c.Int("store", "gc_probability")
	if nil != err {
		storeConfig.GcProbability = gcProbability
	} else {
		storeConfig.GcProbability = DEFAULT_GC_PROBABILITY
	}
	gcDivisor, err := c.Int("store", "gc_divisor")
	if nil != err {
		storeConfig.GcDivisor = gcDivisor
	} else {
		storeConfig.GcDivisor = DEFAULT_GC_DIVISOR
	}

	if nil != err && nil == retErr {
		retErr = err
	}

	return retErr, wordDict, captchaConfig, imageConfig, filterConfig, storeConfig
}