func init() { // ORDER MATTERs // configuration initConfig() // logging l := must(logrus.ParseLevel(config.Log.Level)).(logrus.Level) logrus.SetLevel(l) logrus.SetOutput(os.Stdout) // logging hooks graylogHookLevelThreshold := must(logrus.ParseLevel(config.Log.Graylog.Level)).(logrus.Level) graylogHook := must( grayloghook.New( config.Log.Graylog.Address, config.Log.Graylog.Facility, map[string]interface{}{ "go_version": goVersion, "build_time": buildTime, "git_commit": gitCommit, }, graylogHookLevelThreshold, ), ).(logrus.Hook) logrus.AddHook(graylogHook) // connection hub connsHub = list.New() // storage s := mysql.New(config.DB.DataSourceName) s.SetMaxOpenConns(config.DB.MaxOpenConns) s.SetMaxIdleConns(config.DB.MaxIdleConns) store = s // cache memoryCache = memory.New(config.Cache.NumCachedIncomes) total := must(store.GetLatestTotalReward()).(models.TotalReward) memoryCache.IncrementTotalReward(total.CreatedAt, total.Total) updateCache() // coin client initCoinClient(config.Coin.Type) // cronjob initCronjob(config.Coin.Type, config.CronjobSpec.CreateWithdrawal, config.CronjobSpec.ProcessWithdrawal) // mailer mailer = mandrill.New(config.Mandrill.Key, config.Mandrill.FromEmail, config.Mandrill.FromName) // geetest geetest = gt.New(config.Geetest.CaptchaID, config.Geetest.PrivateKey, false, time.Second*10, time.Second*10, 2048) // geo geo = must(geoip2.Open(config.Geo.Database)).(*geoip2.Reader) }
func main() { g := geetest.New(captchaID, privateKey, false, 5*time.Second, 5*time.Second, 8) fs := http.FileServer(http.Dir("static")) http.Handle("/", fs) http.HandleFunc("/geetest/validate", func(writer http.ResponseWriter, req *http.Request) { challenge := req.PostFormValue("geetest_challenge") validate := req.PostFormValue("geetest_validate") seccode := req.PostFormValue("geetest_seccode") result := struct { Status string `json:"status"` Info string `json:"info"` }{} ok, err := g.Validate(challenge, validate, seccode) if err != nil { writer.WriteHeader(http.StatusInternalServerError) return } if ok { result.Status = "success" result.Info = "登陆成功" } else { result.Status = "fail" result.Info = "登陆失败" } resultBytes, _ := json.Marshal(result) writer.Write(resultBytes) }) http.HandleFunc("/geetest/register", func(writer http.ResponseWriter, req *http.Request) { challenge, err := g.Register() if err != nil { writer.WriteHeader(http.StatusInternalServerError) return } result, _ := json.Marshal(map[string]interface{}{ "gt": g.CaptchaID(), "challenge": challenge, "success": 1, }) writer.Write(result) }) log.Println("listen and serve on 8080...") http.ListenAndServe("127.0.0.1:8080", nil) }