Пример #1
0
// ListContacts takes a contact list and outputs it.
func ListContacts(contacts Contacts) func(rw http.ResponseWriter, r *http.Request) {
	return func(rw http.ResponseWriter, r *http.Request) {

		// Write contact list to output
		pkg.WriteIndentJSON(rw, contacts)
	}
}
Пример #2
0
func Allocate(rw http.ResponseWriter, r *http.Request) {
	n, err := strconv.Atoi(r.URL.Query().Get("n"))
	if err != nil {
		n = 0
	}
	t, err := strconv.Atoi(r.URL.Query().Get("t"))
	if err != nil {
		t = 5
	}
	m := make([][]byte, n+1)

	for i := 0; i < n; i++ {
		z := make([]byte, n+1)
		_, _ = rand.Read(z)
		m[i] = z
	}

	time.Sleep(time.Second * time.Duration(t))

	pkg.WriteIndentJSON(rw, struct {
		Result string `json:"result"`
		N      int    `json:"n"`
	}{
		Result: "Processed!",
		N:      n,
	})
}
Пример #3
0
// UpdateContact will update a contact on the list
func UpdateContact(contacts Contacts) func(rw http.ResponseWriter, r *http.Request) {
	return func(rw http.ResponseWriter, r *http.Request) {
		// Fetch the ID of the contact that is going to be updated
		contactToBeUpdated := mux.Vars(r)["id"]

		// Check if the contact exists
		if _, found := contacts[contactToBeUpdated]; !found {
			http.Error(rw, "I don't know any contact by that ID.", http.StatusNotFound)
			return
		}

		// We parse the request's information into newContactData.
		newContactData, err := ReadContactData(rw, r)

		// Abort handling the request if an error occurs.
		if err != nil {
			return
		}

		// Update the data in the contact list.
		delete(contacts, contactToBeUpdated)
		contacts[newContactData.ID] = newContactData

		// Set the new data
		pkg.WriteIndentJSON(rw, newContactData)
	}
}
Пример #4
0
func ComputePis(rw http.ResponseWriter, r *http.Request) {
	n, err := strconv.Atoi(r.URL.Query().Get("n"))
	if err != nil {
		n = 0
	}

	pkg.WriteIndentJSON(rw, struct {
		Pi string `json:"pi"`
		N  int    `json:"n"`
	}{
		Pi: strconv.FormatFloat(pis(int64(n)), 'E', -1, 64),
		N:  n,
	})
}
Пример #5
0
// ListContacts takes a contact list and outputs it.
func ListContacts(store ContactStorer) func(rw http.ResponseWriter, r *http.Request) {
	return func(rw http.ResponseWriter, r *http.Request) {

		// Write contact list to output
		contacts, err := store.FetchContacts()
		if err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}

		pkg.WriteIndentJSON(rw, contacts)

	}
}
Пример #6
0
func (c *Fosite) WriteAuthorizeError(rw http.ResponseWriter, ar AuthorizeRequester, err error) {
	rfcerr := ErrorToRFC6749Error(err)

	if !ar.IsRedirectURIValid() {
		pkg.WriteIndentJSON(rw, rfcerr)
		return
	}

	redirectURI := ar.GetRedirectURI()
	query := redirectURI.Query()
	query.Add("error", rfcerr.Name)
	query.Add("error_description", rfcerr.Description)
	redirectURI.RawQuery = query.Encode()

	rw.Header().Add("Location", redirectURI.String())
	rw.WriteHeader(http.StatusFound)
}
Пример #7
0
// AddContact will add a contact to the list
func AddContact(contacts Contacts) func(rw http.ResponseWriter, r *http.Request) {
	return func(rw http.ResponseWriter, r *http.Request) {

		// We parse the request's information into contactToBeAdded
		contactToBeAdded, err := ReadContactData(rw, r)

		// Abort handling the request if an error occurs.
		if err != nil {
			return
		}

		// Save newContact to the list of contacts.
		contacts[contactToBeAdded.ID] = contactToBeAdded

		// Output our newly created contact
		pkg.WriteIndentJSON(rw, contactToBeAdded)
	}
}
Пример #8
0
// UpdateContact will update a contact on the list
func UpdateContact(store ContactStorer) func(rw http.ResponseWriter, r *http.Request) {
	return func(rw http.ResponseWriter, r *http.Request) {
		// We parse the request's information into newContactData.
		newContactData, err := ReadContactData(rw, r)

		// Abort handling the request if an error occurs.
		if err != nil {
			http.Error(rw, err.Error(), http.StatusBadRequest)
			return
		}

		// Update the data in the contact list.
		if err := store.UpdateContact(&newContactData); err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}

		// Set the new data
		pkg.WriteIndentJSON(rw, newContactData)
	}
}
Пример #9
0
// AddContact will add a contact to the list
func AddContact(contacts ContactStorer) func(rw http.ResponseWriter, r *http.Request) {
	return func(rw http.ResponseWriter, r *http.Request) {

		// We parse the request's information into contactToBeAdded
		contactToBeAdded, err := ReadContactData(rw, r)

		// Abort handling the request if an error occurs.
		if err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}

		// Save newContact to the list of contacts.
		if err = contacts.CreateContact(&contactToBeAdded); err != nil {
			http.Error(rw, err.Error(), http.StatusInternalServerError)
			return
		}

		// Output our newly created contact
		pkg.WriteIndentJSON(rw, contactToBeAdded)
	}
}