Ejemplo n.º 1
0
/*
PUT: http://localhost:8080/chat/{ad}/{profile}
{
	status: "OK"
    "data":"PUTed"
}
*/
func Chat(w http.ResponseWriter, r *http.Request) {
	var ad, profile string
	s := strings.Split(r.URL.Path, "/")
	if len(s) >= 4 {
		ad, profile = s[2], s[3]
	} else {
		w.Write(json.Message3("ERROR", nil, "Wrong URL"))
		return
	}
	session, err := mgo.Dial(conf.Mongodb)
	if err != nil {
		log.Fatal("Unable to connect to DB ", err)
	}
	defer session.Close()

	session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
	db := session.DB("sa")

	err = db.C("ad").Update(bson.M{"_id": bson.ObjectIdHex(ad)}, bson.M{"$addToSet": bson.M{"chat": profile}})
	if err != nil {
		w.Write(json.Message("ERROR", "Could not PUT"))
		log.Printf("err = %s\n", err)
	} else {
		w.Write(json.Message("OK", "PUTed"))
	}
}
Ejemplo n.º 2
0
/*
GET: http://localhost:8080/ad/5322ee3d2f6ee98d1df6831c&eggtype=baby
{
	status: "OK",
	result: {
	    profile: 123412341134123,
	    title: "test",
	    category: 323,
	    description: "dasfasdfas asdfadsf adsfadfadsfadsf qwerqwerqwer adfasdfdf",
	    price: 1241234123,
	    currency: "qwerqwer",
	    report: 0,
	    date: "2014-02-03T18:09:43.309+08:00"
	    image1: = "0001_040db0bc2fc49ab41fd81294c7d195c7d1de358b_ACA0AC_100_160"
	    image2: = "0001_040db0bc2fc49ab41fd81294c7d195c7d1de358b_ACA0AC_100_160"
	    image3: = ""
	}
}
*/
func GetAd(w http.ResponseWriter, r *http.Request, id string) {
	result, err := getAdById(id)
	if err != nil {
		w.Write(json.Message("ERROR", "Ad not found"))
	} else {
		w.Write(json.Message("OK", result))
	}
}
Ejemplo n.º 3
0
/*
DELETE: http://localhost:8080/ad/{id}/{token}
{
	status: "OK"
    "data":"Deleted"
}
*/
func DeleteAd(w http.ResponseWriter, r *http.Request) {
	var ad, token string
	s := strings.Split(r.URL.Path, "/")
	if len(s) >= 4 {
		ad, token = s[2], s[3]
	} else {
		w.Write(json.Message3("ERROR", nil, "Wrong URL"))
		return
	}

	// create a global App var to hold your app id and secret.
	var globalApp = fb.New("1454662381420609", "9a9d5fd67edc5245f366a1a0fb9bb9bf")

	// facebook asks for a valid redirect uri when parsing signed request.
	// it's a new enforced policy starting in late 2013.
	// it can be omitted in a mobile app server.
	//globalApp.RedirectUri = ""

	// here comes a client with a facebook signed request string in query string.
	// creates a new session with signed request.
	//session, _ := globalApp.SessionFromSignedRequest(signedRequest)

	// or, you just get a valid access token in other way.
	// creates a session directly.
	session := globalApp.Session(token)

	// use session to send api request with your access token.
	//res, _ := session.Get("/me/feed", nil)

	// validate access token. err is nil if token is valid.
	err := session.Validate()

	if err == nil {
		session, err := mgo.Dial(conf.Mongodb)
		if err != nil {
			log.Fatal("Unable to connect to DB ", err)
		}
		defer session.Close()
		session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
		db := session.DB("sa")

		err = db.C("ad").Remove(bson.M{"_id": bson.ObjectIdHex(ad)})
		if err != nil {
			w.Write(json.Message("ERROR", "Could not DELETE"))
			log.Printf("err = %s\n", err)
		} else {
			w.Write(json.Message("OK", "Deleted"))
		}
	} else {
		w.Write(json.Message("ERROR", "Not valid session"))
	}
}
Ejemplo n.º 4
0
//http://localhost:9090/egg/0001_8787bec619ff019fd17fe02599a384d580bf6779_9BA4AA_400_300?type=baby
func Get(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", conf.Mime)
	w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", conf.CacheMaxAge))
	size := r.FormValue("size")
	eid := html.EscapeString(r.URL.Path[5:]) //cutting "/egg/"
	if size != "" {
		d, err := GetEggBySize(size, eid)
		if err != nil {
			w.Write(json.Message("ERROR", "Unable to find by size"))
			return
		}
		if size == "baby" {
			eid = d.Baby
		} else if size == "infant" {
			eid = d.Infant
		} else if size == "newborn" {
			eid = d.Newborn
		} else {
			eid = d.Egg
		}
	}
	log.Println("GET: eid = " + eid)
	path := parsePath(eid)
	log.Println("GET: path = " + path)
	http.ServeFile(w, r, path)
}
Ejemplo n.º 5
0
/*
GET: http://localhost:8080/myads/profile // TODO: make it dynamic
{
	status: "OK"
	data: [
	    0:{
	        title: "test"
	        price: 6468
	        currency: "SGD"
	        image: "0001_72a53f664db6f415e9e862c607d9c0ba177c20af_655B4C_100_75"
	      }
	    ...
    ]
}
*/
func Myads(w http.ResponseWriter, r *http.Request) {
	var profile string
	lim, sort := conf.ResultLimit, "-_id" // TODO: do not hardcode
	params := strings.Split(r.URL.Path, "/")
	if len(params) >= 2 {
		profile = params[2] //TODO: it should work even Path is not enough
	} else {
		w.Write(json.Message3("ERROR", nil, "Wrong URL"))
		return
	}

	p, err := strconv.Atoi(profile)
	var prof bson.M
	if err != nil {
		w.Write(json.Message3("ERROR", nil, "Please, provide profile id"))
		return
	} else {
		prof = bson.M{"profile": p}
	}

	session, err := mgo.Dial(conf.Mongodb)
	if err != nil {
		log.Fatal("Unable to connect to DB ", err)
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
	db := session.DB("sa")
	var data interface{}
	var adList []AdList
	var ad AdList

	iter := db.C("ad").Find(prof).Limit(lim).Sort(sort).Iter()
	for iter.Next(&ad) {
		ad.Image = ad.Image1.Baby // TODO: make it dynamic
		imgName := strings.Split(ad.Image, "_")
		ad.Width = imgName[3]
		ad.Height = imgName[4]
		ad.Image = conf.IkuraUrl + ad.Image
		adList = append(adList, ad)
	}
	data = adList
	if err != nil {
		w.Write(json.Message("ERROR", "Ads not found"))
	} else {
		if ad.Image != "" {
			w.Write(json.Message3("OK", data, "Ads found"))
		} else {
			w.Write(json.Message3("ERROR", nil, "Ads not found"))
		}
	}
	log.Printf("err = %s\n", err)
}
Ejemplo n.º 6
0
/*
POST: http://localhost:8080/search
{
	status: "OK",
	result: {
	    language: "english",
	    ok: 1,
	    queryDebugString: "hsjsjd||||||",
	    results: [
	        {
            	obj: {
                	_id: "53202376058424b87c9d9368",
                	image1: {
                	    baby: "0001_68415c85528ccf9e763eb48d9dc0fca8a540f701_655B4C_400_300",
                	    egg: "0001_c0448ef44bf7fd00476fadf11805d94fe94a5820_655B4C_816_612",
                	    infant: "0001_9b0e36f28be91dae81a02863fadce2bc2f196312_655B4C_200_150",
                	    newborn: "0001_72a53f664db6f415e9e862c607d9c0ba177c20af_655B4C_100_75"
                    },
            	    price: 6468
                },
            	score: 1.1
            },
	        ...
        ],
        stats: {
        	n: 2,
        	nfound: 2,
        	nscanned: 3,
        	nscannedObjects: 0,
        	timeMicros: 69
        }
	}
}
*/
func Search(w http.ResponseWriter, r *http.Request) {
	q := r.FormValue("q")
	fmt.Printf("q = %s\n", q)
	limit, err := strconv.Atoi(r.FormValue("limit"))
	if err != nil {
		limit = conf.ResultLimit
	}
	fmt.Printf("limit = %s\n", limit)
	session, err := mgo.Dial(conf.Mongodb)
	if err != nil {
		log.Fatal("Unable to connect to DB ", err)
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
	db := session.DB("sa")
	var result interface{}
	//db.ad.runCommand("text", { search: "hsjsjd", limit: 20, project: { "price" : 1, "image1": 1 }})
	if q != "" {
		sql := bson.D{
			{"text", "ad"},
			{"search", q},
			{"limit", limit},
			{"project",
				bson.D{
					{"price", 1},
					{"image1", 1},
				},
			},
		}
		err = db.Run(sql, &result)
	}
	if err != nil {
		w.Write(json.Message("ERROR", "Ads not found"))
	} else {
		w.Write(json.Message("OK", result))
	}

	log.Printf("err = %s\n", err)
}
Ejemplo n.º 7
0
/*
POST: http://localhost:8080/ad/
      profile=123412341134123&category=323&price=1241234123&title=test&description=adfasdfdf&currency=qwerqwer&\
      newborn1=0001_66f8b0fd119d9189a020cbe7ca604f9c3ee18499_CD262C_400_711
*/
func PostAd(w http.ResponseWriter, r *http.Request) {
	// TODO: refactor it!
	profile, err := strconv.ParseUint(r.FormValue("profile"), 10, 64)
	if err != nil {
		w.Write(json.Message("ERROR", "Profile is missing"))
		return
	}
	log.Println(r.FormValue("category"))
	category, err := strconv.ParseUint(r.FormValue("category"), 10, 64)
	if err != nil {
		w.Write(json.Message("ERROR", "Category is missing"))
		return
	}
	price, err := strconv.ParseUint(r.FormValue("price"), 10, 64)
	if err != nil {
		w.Write(json.Message("ERROR", "Price is missing"))
		return
	}
	title := r.FormValue("title")
	if title == "" {
		w.Write(json.Message("ERROR", "Title is missing"))
		return
	}
	description := r.FormValue("description")
	if description == "" {
		w.Write(json.Message("ERROR", "Description is missing"))
		return
	}
	currency := r.FormValue("currency")
	if currency == "" {
		w.Write(json.Message("ERROR", "Currency is missing"))
		return
	}

	ad := Ad{
		Profile:     profile,
		Title:       title,
		Category:    category,
		Description: description,
		Price:       price,
		Currency:    currency,
		Report:      0,
		Date:        time.Now(),
	}
	newborn1 := r.FormValue("newborn1") // Newborn image1
	if newborn1 == "" {
		w.Write(json.Message("ERROR", "At least one image should be uploaded"))
	} else {
		image1, err := api.GetEggBySize("newborn", newborn1)
		if err == nil {
			ad.Image1 = image1
		}
	}
	newborn2 := r.FormValue("newborn2") // Newborn image2
	if newborn2 != "" {
		image2, err := api.GetEggBySize("newborn", newborn2)
		if err == nil {
			ad.Image2 = image2
		}
	}
	newborn3 := r.FormValue("newborn3") // Newborn image3
	if newborn3 != "" {
		image3, err := api.GetEggBySize("newborn", newborn3)
		if err == nil {
			ad.Image3 = image3
		}
	}
	err = ad.saveAd()
	if err != nil {
		w.Write(json.Message("ERROR", "Could not save your ad, please, try again later"))
		return
	}
	w.Write(json.Message("OK", "Saved!"))
}
Ejemplo n.º 8
0
/*
GET: http://localhost:8080/listinig/category/limit/sort
{
	status: "OK"
	data: [
	    0:{
	        title: "test"
	        price: 6468
	        currency: "SGD"
	        image: "0001_72a53f664db6f415e9e862c607d9c0ba177c20af_655B4C_100_75"
	      }
	    ...
    ]
}
*/
func Listing(w http.ResponseWriter, r *http.Request) {
	var category, limit, sort string
	sort = "-_id"
	s := strings.Split(r.URL.Path, "/")
	if len(s) >= 4 {
		category, limit, sort = s[2], s[3], s[4] //TODO: it should work even Path is not enough
	} else {
		w.Write(json.Message3("ERROR", nil, "Wrong URL"))
		return
	}
	c, err := strconv.Atoi(category)
	var cat bson.M
	if err != nil || c == 0 {
		cat = nil // if not number, then consider All categories
	} else {
		cat = bson.M{"category": c}
	}

	lim, err := strconv.Atoi(limit)
	if err != nil {
		lim = conf.ResultLimit
	}

	if sort != "date" {
		if sort != "price" {
			w.Write(json.Message3("ERROR", nil, "Wrong URL"))
			return
		}
	} else {
		sort = "-_id"
	}

	session, err := mgo.Dial(conf.Mongodb)
	if err != nil {
		log.Fatal("Unable to connect to DB ", err)
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
	db := session.DB("sa")
	var data interface{}
	var adList []AdList
	var ad AdList

	iter := db.C("ad").Find(cat).Limit(lim).Sort(sort).Iter()
	for iter.Next(&ad) {
		ad.Image = ad.Image1.Baby // TODO: make it dynamic
		imgName := strings.Split(ad.Image, "_")
		ad.Width = imgName[3]
		ad.Height = imgName[4]
		ad.Image = conf.IkuraUrl + ad.Image
		adList = append(adList, ad)
	}
	data = adList
	if err != nil {
		w.Write(json.Message("ERROR", "Ads not found"))
	} else {
		if ad.Image != "" {
			w.Write(json.Message3("OK", data, "Ads found"))
		} else {
			w.Write(json.Message3("ERROR", nil, "Ads not found"))
		}
	}
	log.Printf("err = %s\n", err)
}
Ejemplo n.º 9
0
/*
 *{
 *	status: "ok"
 * 	result: { newborn: "0001_040db0bc2fc49ab41fd81294c7d195c7d1de358b_ACA0AC_100_160" }
 *}
 */
func Put(w http.ResponseWriter, r *http.Request) {
	if r.Method != "PUT" {
		w.Write(json.Message("ERROR", "Not supported Method"))
		return
	}

	log.Println(r)

	reader, err := r.MultipartReader()
	if err != nil {
		w.Write(json.Message("ERROR", "Client should support multipart/form-data"))
		return
	}

	buf := bytes.NewBufferString("")
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}
		if part.FileName() == "" { // if empy skip this iteration
			continue
		}
		_, err = io.Copy(buf, part)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	defer r.Body.Close()
	img, _, err := image.Decode(buf)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to decode your image"))
		return
	}
	t0 := time.Now()

	imgBaby := resize.Resize(conf.BabyWidth, 0, img, resize.NearestNeighbor)
	imgInfant := resize.Resize(conf.InfantWidth, 0, imgBaby, resize.NearestNeighbor)
	imgNewborn := resize.Resize(conf.NewbornWidth, 0, imgInfant, resize.NearestNeighbor)
	imgSperm := resize.Resize(conf.Sperm, conf.Sperm, imgNewborn, resize.NearestNeighbor)

	red, green, blue, _ := imgSperm.At(0, 0).RGBA()
	color := fmt.Sprintf("%X%X%X", red>>8, green>>8, blue>>8) // removing 1 byte 9A16->9A

	fileOrig, err := imgToFile(img, color)
	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image"))
	}
	fileBaby, err := imgToFile(imgBaby, color)
	fileInfant, err := imgToFile(imgInfant, color)
	fileNewborn, err := imgToFile(imgNewborn, color)

	result := json.Result{
		Newborn: fileNewborn,
	}

	egg := json.Egg{
		Egg:     fileOrig,
		Baby:    fileBaby,
		Infant:  fileInfant,
		Newborn: fileNewborn,
	}
	err = egg.SaveMeta()

	if err != nil {
		w.Write(json.Message("ERROR", "Unable to save your image"))
	} else {
		w.Write(json.Message("OK", &result))
	}

	t1 := time.Now()
	log.Printf("The call took %v to run.\n", t1.Sub(t0))
}