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() }
func GetCaptcha(path string) (*gocaptcha.Captcha, error) { // var configFile = flag.String("c", path, "the config file") return gocaptcha.CreateCaptchaFromConfigFile(path) }