コード例 #1
0
ファイル: mini.go プロジェクト: HasKewei/topsecret
//the function which handle the post method
//post the json data from broswer to the server and sql databases
func POST(w http.ResponseWriter, r *http.Request) {
	//time when do request
	RequestStart := time.Now()

	//get the raw bytes of input data
	bytes, errs := ioutil.ReadAll(r.Body)
	if errs != nil {
		errString := fmt.Sprintf("buffer overflow %s", errs)
		response(w, errString, "", http.StatusBadRequest)
		return
	}

	//store the raw bytes to a temporary struct and log the Json invalid format
	var point Click
	errs = json.Unmarshal(bytes, &point)
	if errs != nil {
		errString := fmt.Sprintf("invalid Json format: %s", errs)
		response(w, errString, "", http.StatusBadRequest)
		structured.Error("", "", errString, 0, nil)
		return
	}

	//validate the input and log error input message
	if point.AdvertiserID == 0 || point.SiteID == 0 {
		errString := "your advertiserID or site ID may equals to 0"
		response(w, errString, "", http.StatusBadRequest)
		structured.Error("", "", errString, 0, nil)
		return
	}

	//generate a ramdom id for the post data and also get the ip address
	id := Id(point.AdvertiserID)
	ip := r.RemoteAddr

	//store the data from the struct to the sql databases and log the error or latency time
	QueryStart := time.Now()

	_, errs = db.Exec("INSERT INTO clicks(id, advertiser_id, site_id, ip, ios_ifa, google_aid, windows_aid, date_time) VALUES(?, ?, ?, ?, ?, ?, ?,?)",
		id, point.AdvertiserID, point.SiteID, ip, point.IosIfa, point.GoogleAid, point.WindowsAid, RequestStart)

	if errs != nil {
		fmt.Println(errs) //show to the sever protecter inside of users
		errString := "sorry, there is an error"
		response(w, errString, "", http.StatusInternalServerError)
		errString = fmt.Sprintf("database connection error : %s", errs)
		structured.Error("", "", errString, 0, nil)
		return
	}

	//sucess and log the request latency
	response(w, "", id, http.StatusOK)
	structured.Info(point.ID, point.Type, "Post successful!", point.SiteID,
		structured.ExtraFields{structured.RequestLatency: time.Since(RequestStart),
			structured.QueryLatency: time.Since(QueryStart)})
}
コード例 #2
0
ファイル: mini.go プロジェクト: HasKewei/topsecret
//function that handles the GET method
//retrieve the json data form from database/server and output to the browser
func GET(writer http.ResponseWriter, reader *http.Request) {

	// time the method
	var starttime = time.Now()

	///get the id from the hashmap
	id := mux.Vars(reader)["id"]

	//select data from sql databases according to the id
	row := db.QueryRow("SELECT id, advertiser_id, site_id, ip, ios_ifa, google_aid, windows_aid, date_time FROM clicks WHERE id=?", id)

	//store the data from sql database in a temp struct
	var c Click

	err := row.Scan(&c.ID, &c.AdvertiserID, &c.SiteID, &c.IP, &c.IosIfa, &c.GoogleAid, &c.WindowsAid, &c.Date_time)

	//check for errors in scan  (404 and 500)
	if err == sql.ErrNoRows {

		// fmt.Println(err)

		writer.WriteHeader(http.StatusNotFound)

		structured.Error(c.ID, c.Type, "Error 404: no rows found", c.SiteID, nil)

		return
	} else if err != nil {

		writer.WriteHeader(http.StatusInternalServerError)

		structured.Error(c.ID, c.Type, "Error 500: Internal server error", c.SiteID, nil)

		return
	}

	// log the event
	structured.Info(c.ID, c.Type, "GET successful", c.SiteID,
		structured.ExtraFields{structured.RequestLatency: time.Since(starttime)})

}