예제 #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
파일: main.go 프로젝트: ory-am/workshop-dbg
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
파일: main.go 프로젝트: ory-am/workshop-dbg
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
파일: main.go 프로젝트: ory-am/workshop-dbg
// 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
파일: main.go 프로젝트: ory-am/workshop-dbg
// 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
파일: main.go 프로젝트: ory-am/workshop-dbg
// 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)
	}
}