Example #1
0
func handler(w http.ResponseWriter, r *http.Request) {

	ref := r.Referer()

	if ref == "" {
		ref = "localhost"
	}

	fmt.Printf("referrer: \"%s\"", ref)

	db := Db{ref, nil}

	err := db.sCheck()

	if err != nil {
		serveError(w, err.Error())
		return
	}

	switch r.Method {
	case "GET":
		sID := r.URL.Path[1:]
		var id int64 = 0

		if sID == "" {
			id = 0
		} else {
			convid, err := strconv.ParseInt(sID, 10, 64)
			id = convid

			if err != nil {
				serveError(w, err.Error())
				return
			}
		}

		if id == 0 {
			err = db.List()
			if err != nil {
				serveError(w, err.Error())
				return
			}
		} else {
			r, err := db.Get(id)

			if err != nil {
				serveError(w, err.Error())
				return
			}

			enc := json.NewEncoder(w)
			w.Header().Set("Content-Type", "application/json")
			enc.Encode(r)
		}

	case "POST":
		dec := json.NewDecoder(r.Body)

		r := new(Registration)

		err := dec.Decode(&r)

		if err != nil {
			serveError(w, err.Error())
			return
		}

		r.Password = Encrypt(r.Password)

		r, err = db.Create(r)

		if err != nil {
			serveError(w, err.Error())
			return
		}

		enc := json.NewEncoder(w)
		w.Header().Set("Content-Type", "application/json")
		enc.Encode(r)
	case "PUT":
		dec := json.NewDecoder(r.Body)

		r := new(Registration)

		err := dec.Decode(&r)

		if err != nil {
			serveError(w, err.Error())
			return
		}

		r.Password = Encrypt(r.Password)

		r, err = db.Update(r)

		if err != nil {
			serveError(w, err.Error())
			return
		}

		enc := json.NewEncoder(w)
		w.Header().Set("Content-Type", "application/json")
		enc.Encode(r)
	case "DELETE":
		sID := r.URL.Path[1:]
		id, err := strconv.ParseInt(sID, 10, 64)

		err = db.Delete(id)

		if err != nil {
			serveError(w, err.Error())
			return
		}
		serveOK(w, "registration deleted")

	}
}