Esempio n. 1
0
func init() {

	http.Handle("/apis", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		util.RenderJSON(w, models.ListAPI(), nil)
	}))

}
Esempio n. 2
0
func index() http.Handler {
	return misc.Chain(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/" {
			http.NotFound(w, r)
			return
		}
		misc.RenderHTML(w, []string{"app/index.tmpl"}, nil, nil)
	})
}
Esempio n. 3
0
func init() {

	http.Handle("/crons", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		entries := []*cron.Entry{}
		for _, c := range crons.Crons() {
			entries = append(entries, c.Entries()...)
		}
		next := []time.Time{}
		for _, entry := range entries {
			next = append(next, entry.Next)
		}
		util.RenderJSON(w, next, nil)
	}))

	http.Handle("/crons/results", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		results, _, err := models.GetCronResults()
		util.RenderJSON(w, results, err)
	}))

}
func init() {

	/**
	 * 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).]
	 */
	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("/refresh", 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)
	}))
}