示例#1
0
func gamepage(w http.ResponseWriter, r *http.Request) {
    cookie, _ := r.Cookie("Spanzhash")
    if(hashExists(cookie.Name))
	data, err := ioutil.ReadFile("./layouts/gamepage.html")
    if err==nil {
    	w.Header().Add("Content-Type","text/html")
    	w.Write(data)
    } else {
    	w.WriteHeader(404)
    	w.Write([]byte("404 Page not found - "+http.StatusText(404)))
    }
}
示例#2
0
func HandlePing(c web.C, w http.Responsewriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	ping := &Ping{}
	err := decoder.Decode(ping)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write("Malformed Body")
		return
	}
	ping.Timestamp = time.Now()
	ping.NextPing = time.Now().Add(15*time.Minute)
	// Begin validation of different fields.
	if ping.Timestamp
}
示例#3
0
func PriceAdd(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*") // cors
	var price Price
	// parse request body
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	bodyString := string(body)
	fmt.Printf("\nData to be parsed and added:\n\n%s\n\n", bodyString)

	if strings.Contains(bodyString, "\\") {
		bodyString = strings.Replace(bodyString, "\\", "", -1)
		bodyString = strings.TrimRight(bodyString, "\"")
		bodyString = strings.TrimLeft(bodyString, "\"")
		fmt.Printf("Removed excape chars, should be parseable json now: \n%s", bodyString)
	}

	if err != nil {
		fmt.Fprintf(w, "Error reading request body")
	}
	if err := json.Unmarshal([]byte(bodyString), &price); err != nil {
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		w.WriteHeader(422) // unprocessable entity
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
	}
	fmt.Printf("\nAdding to database...\n")
	fmt.Printf("\nNumber of stocks: %d", len(price.StockId))
	// add price object to database
	if AddPriceData(price) {
		fmt.Fprintf(w, "Data added successfully! :)")
		fmt.Printf("\nData added successfully")
	} else {
		fmt.Fprintf(w, "Failed to add data :(")
		fmt.Printf("Failed to add data")
	}
}
示例#4
0
func EventController(w http.ResponseWriter, req *http.Request) {

  // Implementing the events server. Code for listening to the events will come here.
  // Events are going to be view / buy events, with 'weightToAdd'. Add this to the Ratings matrix.

  log.Println(req)

  example_event:= new(models.Event)
  decoder := json.NewDecoder(req.Body)
  decoder.Decode(&example_event)

  fmt.Println("Event: ")
  fmt.Println(example_event)
  // Update values in ALS.Ratings_matrix
  UserID := example_event.UserID
  ProductID := example_event.ProductID
  Event := example_event.Event

  // Calculate weight to add.
  if Event == "View Product" {
    weightToAdd = 0.1
  }
  if Event == "Add Product to Cart" {
    weightToAdd = 0.5
  }
  if Event == "Delete from Cart" {
    weightToAdd = 0.2
  }

  // Get which user this userID belongs to.
  i := 0
  if NumUsers==0 {
    UserDB := make([]string, 1)
    UserDB[0] = UserID
    NumUsers = 1
    ALS.AddUser()
  }
  for i=0; i<NumUsers; i++ {
    if UserDB[i] = UserID {
      break
    }
  }
  if(i == NumUsers){
    // User does not exist. Add to UserDB.
    UserDB = append(UserDB, UserID)
    NumUsers = NumUsers+1
    ALS.AddUser()
  }
  userNumber := i

  i = 0
  if NumItems==0 {
    ProductDB := make([]string, 1)
    ProductDB[0] = ProductID
    NumItems = 1
    ALS.AddItem()
  }
  for i=0; i<NumItems; i++ {
    if ProductDB[i] = ProductID {
      break
    }
  }
  if(i == NumItems){
    // User does not exist. Add to UserDB.
    ProductDB = append(ProductDB, ProductID)
    NumItems = NumItems+1
    ALS.AddItem()
  }
  productNumber := i

  // Now add this to the Ratings matrix.
  ALS.Ratings_Matrix.Set(userNumber, productNumber, weightToAdd)
  w.Header().Set("Content-Type", "application/json")
  w.WriteHeader(http.StatusOk)
}