func init() { cronOnce.Do(func() { r, _ := regexp.Compile("[^a-zA-Z0-9_\\.]") cronTable = r.ReplaceAllString(strings.ToLower(config.NewConfig().Name), "-") + "-crons" comfirmCronTableExists() }) }
func init() { dynamoOnce.Do(func() { dynamoCfg = config() if host := appcfg.NewConfig().DynamoDbLocal; host != "" { dynamoCfg.Endpoint = awssdk.String("http://" + host + ":8000") } }) }
func TestSessionTable(t *testing.T) { actual := sessionTable r, _ := regexp.Compile("[^a-zA-Z0-9_\\.]") expected := r.ReplaceAllString(strings.ToLower(config.NewConfig().Name), "-") + "-sessions" if actual != expected { t.Errorf("CronTableName was not fixed in init func. Expected %v, but got %v", expected, actual) return } }
func init() { sessionOnce.Do(func() { r, _ := regexp.Compile("[^a-zA-Z0-9_\\.]") sessionTable = r.ReplaceAllString(strings.ToLower(config.NewConfig().Name), "-") + "-sessions-2015" if found, _ := comfirmSessionTableExists(); !found { go func() { time.Sleep(10 * time.Second) SyncReInventSessions(true) }() } }) }
func main() { rand.Seed(time.Now().UnixNano()) cfg := config.NewConfig() logs.Debug.Print("[config] " + cfg.String()) http.Handle("/", index()) http.HandleFunc("/alive", alive) http.HandleFunc("/version", version) http.Handle("/assets/", assets(cfg)) logs.Info.Printf("[service] listening on port %v", cfg.Port) logs.Fatal.Print(http.ListenAndServe(":"+fmt.Sprint(cfg.Port), nil)) }
func TestIndex(t *testing.T) { ts := httptest.NewServer(http.Handler(index())) defer ts.Close() res, err := http.Get(ts.URL) if ok := misc.IsSuccessHTTPRequest(t, res, err); !ok { return } actual, _ := misc.ParseHTTPResponse(res) expected := config.NewConfig().Name if !strings.Contains(actual, expected) { t.Errorf("Invalid response. Expected %v, but got %v", expected, actual) return } }
func TestAssets(t *testing.T) { cfg := config.NewConfig() ts := httptest.NewServer(http.Handler(assets(cfg))) defer ts.Close() res, err := http.Get(ts.URL + "/assets/css/main.css") if ok := misc.IsSuccessHTTPRequest(t, res, err); !ok { return } actual, _ := misc.ParseHTTPResponse(res) expected := "monospace" if !strings.Contains(actual, expected) { t.Errorf("Invalid response. Expected %v, but got %v", expected, actual) return } }
func config() *aws.Config { log := aws.LogLevel(aws.LogOff) cfg := app.NewConfig() if cfg.AwsLog { log = aws.LogLevel(aws.LogDebug) } return &aws.Config{ Credentials: credentials.NewChainCredentials( []credentials.Provider{ &credentials.EnvProvider{}, &ec2rolecreds.EC2RoleProvider{ExpiryWindow: cfg.AwsRoleExpiry * time.Minute}, }), Region: aws.String(os.Getenv("AWS_REGION")), LogLevel: log, } }
func init() { cfg = config.NewConfig() // Access limitations if cfg.LimitRatePerMin > 0 { if cfg.LimitBursts > 0 { th = throttled.Interval( throttled.PerMin(cfg.LimitRatePerMin), cfg.LimitBursts, cfg.LimitVaryBy, cfg.LimitKeyCache) } else { th = throttled.RateLimit( throttled.PerMin(cfg.LimitRatePerMin), cfg.LimitVaryBy, store.NewMemStore(cfg.LimitKeyCache)) } } }
func init() { cfg := config.NewConfig() stage := cfg.Stage if !misc.ZeroOrNil(stage) { stage = "/" + stage } /** * sessions * @param string output [The formatting style for response body (html | json).] * @param string q [Space seperated words to use in filtering the response data (for example, best practice).] * @param int type * @param int track * @param int level * @param int date */ http.Handle("/reinvent/sessions", util.Chain(func(w http.ResponseWriter, r *http.Request) { id, found := util.RequestGetParam(r, "id") if found { session, err := models.GetSession(id) if util.IsInvalid(w, err, "@aws.DynamoRecords") { return } util.RenderJSON(w, session, nil) return } output, found := util.RequestGetParam(r, "output") if found && (strings.ToLower(output) == "html") { token, _, _ := checkTwitterSession(w, r) util.RenderHTML(w, []string{"reinvent/index.tmpl"}, struct{ Token models.OAuthAuthorizedToken }{token}, nil) return } cron, err1 := models.GetCronResult("SyncReInventSessions") if util.IsInvalid(w, err1, "@aws.DynamoRecord") { return } // get sessions sessions, _, err2 := models.GetSessions() if util.IsInvalid(w, err2, "@aws.DynamoRecords") { return } // filter if q, found := util.RequestGetParam(r, "q"); found { splitted := strings.FieldsFunc(q, func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) }) words := make([]string, len(splitted)) for i, val := range splitted { words[i] = strings.ToUpper(val) } filtered := models.Sessions{} for _, session := range sessions { if session.Contains(words) { filtered = append(filtered, session) } } sessions = filtered } util.RenderJSON(w, struct { Count int `json:"count"` Sessions []models.Session `json:"sessions"` Sync time.Time `json:"sync"` }{ Count: len(sessions), Sessions: sessions, Sync: cron.LastEndDate, }, nil) })) http.Handle("/reinvent-sessions", util.Chain(func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, stage+"/reinvent/sessions", http.StatusFound) })) http.Handle("/reinvent/sync-sessions", util.Chain(func(w http.ResponseWriter, r *http.Request) { sessions, err := models.SyncReInventSessions(true) util.RenderJSON(w, sessions, err) })) }
func init() { sessions = map[string]*oauth1a.UserConfig{} cfg := config.NewConfig() twitter = &oauth1a.Service{ RequestURL: "https://api.twitter.com/oauth/request_token", AuthorizeURL: "https://api.twitter.com/oauth/authorize", AccessURL: "https://api.twitter.com/oauth/access_token", ClientConfig: &oauth1a.ClientConfig{ ConsumerKey: cfg.TwitterKey, ConsumerSecret: cfg.TwitterSecret, CallbackURL: cfg.TwitterCallback, }, Signer: new(oauth1a.HmacSha1Signer), } /** * Twitter OAuth */ http.Handle("/twitter/signin", util.Chain(func(w http.ResponseWriter, r *http.Request) { http.SetCookie(w, util.SetCookie(twitterSessionKey, "", -1)) _, sessionID, ok := checkTwitterSession(w, r) if !ok { logs.Error.Print("Could not generate new sessionID.") http.Error(w, "Problem generating new session", http.StatusInternalServerError) return } session := &oauth1a.UserConfig{} if err := session.GetRequestToken(twitter, new(http.Client)); err != nil { logs.Error.Printf("Could not get request token: %v", err) http.Error(w, fmt.Sprintf("Problem getting the request token: %v", err), http.StatusInternalServerError) return } url, err := session.GetAuthorizeURL(twitter) if err != nil { logs.Error.Printf("Could not get authorization URL: %v", err) http.Error(w, "Problem getting the authorization URL", http.StatusInternalServerError) return } sessions[sessionID] = session http.Redirect(w, r, url, http.StatusFound) })) http.Handle("/twitter/callback", util.Chain(func(w http.ResponseWriter, r *http.Request) { _, found := util.RequestGetParam(r, "denied") if found { http.Redirect(w, r, "/reinvent/sessions?output=html", http.StatusFound) return } authorizedToken, sessionID, ok := checkTwitterSession(w, r) if !ok { logs.Error.Print("Could not generate new sessionID.") http.Error(w, "Problem generating new session", http.StatusInternalServerError) return } if authorizedToken.ID != "" { http.Redirect(w, r, "/", http.StatusFound) return } session, ok := sessions[sessionID] if !ok { logs.Error.Print("Could not find user config in sesions storage.") http.Error(w, "Invalid session", http.StatusBadRequest) return } token, verifier, err := session.ParseAuthorize(r, twitter) if err != nil { logs.Error.Printf("Could not parse authorization: %v", err) http.Error(w, "Problem parsing authorization", http.StatusInternalServerError) return } if err = session.GetAccessToken(token, verifier, twitter, new(http.Client)); err != nil { logs.Error.Printf("Error getting access token: %v", err) http.Error(w, "Problem getting an access token", http.StatusInternalServerError) return } delete(sessions, sessionID) authorized := models.OAuthAuthorizedToken{ ID: session.AccessValues.Get("user_id"), ScreenName: session.AccessValues.Get("screen_name"), AccessTokenKey: session.AccessTokenKey, AccessTokenSecret: session.AccessTokenSecret, CognitoPoolID: strings.Replace(cfg.CognitoPoolID, ":", "*", -1), CognitoRoleArn: strings.Replace(cfg.CognitoRoleArn, ":", "*", -1), } bytes, _ := json.Marshal(authorized) http.SetCookie(w, util.SetCookie(twitterSessionKey, string(bytes), 60*60*24)) http.SetCookie(w, util.SetCookie(twitterTemporaryKey, "", -1)) http.Redirect(w, r, "/reinvent/sessions?output=html", http.StatusFound) })) }