コード例 #1
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func SignUpHandler(w http.ResponseWriter, r *http.Request) {

	user := User{}
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(400)
		return
	}
	userp := &user
	err = json.Unmarshal(body, userp)

	if !((len(userp.Email) > 0) && (len(userp.Password) > 4)) {
		w.WriteHeader(400)
		return
	}

	count, err := mongo.Find(user, bson.M{"email": userp.Email}).Count()
	if count > 0 {
		w.WriteHeader(400)
		return
	}

	err = userp.CreateUser()
	if err != nil {
		w.WriteHeader(400)
	}
	err = mongo.Find(user, bson.M{"email": userp.Email}).One(userp)
	json.NewEncoder(w).Encode(userp.Serialize())

}
コード例 #2
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func FetchURLHandler(w http.ResponseWriter, r *http.Request) {

	vars := mux.Vars(r)
	token := r.FormValue("token")
	applicationId := vars["applicationId"]
	urlId := vars["urlId"]
	isAuthenticated, err := IsAuthenticated(token, applicationId)

	if isAuthenticated != true {
		w.WriteHeader(403)
		return
	}
	url := Url{}
	application := Application{}
	var urls []Url
	err = mongo.Find(application, bson.M{"_id": bson.ObjectIdHex(applicationId)}).One(&application)

	err = mongo.Find(url, bson.M{"application_id": bson.ObjectIdHex(applicationId),
		"_id": bson.ObjectIdHex(urlId)}).All(&urls)

	if err != nil {
		w.WriteHeader(400)
		return
	}
	go AsyncUrlCall(urls, application, applicationId, false)

}
コード例 #3
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func ApplicationListHandler(w http.ResponseWriter, r *http.Request) {

	var applications []Application

	user := users.User{}
	application := Application{}

	token := r.FormValue("token")
	err := mongo.Find(user, bson.M{"token": token}).One(&user)

	if err != nil {
		w.WriteHeader(403)
		return
	}
	err = mongo.Find(application, bson.M{"ownerid": user.Id}).All(&applications)

	if err != nil {
		w.WriteHeader(403)
		return
	}
	serializedApplications := make([]map[string]interface{}, len(applications))

	for i, element := range applications {
		serializedApplications[i] = element.Serialize()
	}
	json.NewEncoder(w).Encode(serializedApplications)
}
コード例 #4
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func UrlDetailHandler(w http.ResponseWriter, r *http.Request) {

	url_record := UrlRecord{}
	var records []UrlRecord
	url := Url{}
	vars := mux.Vars(r)
	urlId := vars["urlId"]
	applicationId := vars["applicationId"]
	token := r.FormValue("token")

	isAuthenticated, err := IsAuthenticated(token, applicationId)
	if isAuthenticated != true {
		w.WriteHeader(403)
		return
	}
	err = mongo.Find(url_record, bson.M{"url_id": bson.ObjectIdHex(urlId)}).All(&records)
	err = mongo.Find(url, bson.M{"_id": bson.ObjectIdHex(urlId)}).One(&url)
	if err != nil {
		w.WriteHeader(404)
		return
	}
	serializedUrlRecords := make([]map[string]interface{}, len(records))

	for i, element := range records {
		serializedUrlRecords[i] = element.Serialize()
	}
	serializedUrl, err := url.Serialize()
	bundle := map[string]interface{}{"records": serializedUrlRecords,
		"url": serializedUrl}
	json.NewEncoder(w).Encode(bundle)
}
コード例 #5
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func LoginHandler(w http.ResponseWriter, r *http.Request) {

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(400)
		return
	}
	user := User{}
	requestuser := User{}
	err = json.Unmarshal(body, &requestuser)
	err = mongo.Find(user, bson.M{"email": requestuser.Email}).One(&user)

	if err != nil {
		w.WriteHeader(403)
		return
	}
	err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(requestuser.Password))

	if err != nil {
		w.WriteHeader(403)
		return
	}
	userp := &user
	json.NewEncoder(w).Encode(userp.Serialize())

}
コード例 #6
0
ファイル: models.go プロジェクト: Hipo/gotcha
func (u *Url) Serialize() (map[string]interface{}, error) {
	record := UrlRecord{}
	records := []UrlRecord{}
	err := mongo.Find(record, bson.M{"url_id": bson.ObjectId(u.Id)}).Sort("-date_created").Limit(2).All(&records)
	if err != nil {
		return nil, err
	}
	bundle := make(map[string]interface{})
	bundle["Id"] = u.Id
	bundle["Url"] = u.Url
	bundle["Title"] = u.Title
	bundle["WaitTime"] = u.WaitTime
	bundle["TryCount"] = u.TryCount
	bundle["ApplicationId"] = u.ApplicationId
	bundle["Headers"] = u.Headers
	if len(records) >= 1 {
		record1 := records[0]
		bundle["Time"] = record1.DateCreated
		bundle["Last"] = record1.Time
		bundle["Status"] = record1.StatusCode
	}

	if len(records) >= 2 {

		record1, record2 := records[0], records[1]
		bundle["Previous"] = record2.Time
		bundle["Faster"] = record1.Time < record2.Time
	}

	return bundle, nil
}
コード例 #7
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func IsAuthenticated(authToken string, applicationId string) (bool, error) {
	user := users.User{}
	application := Application{}
	err := mongo.Find(user, bson.M{"token": authToken}).One(&user)
	if err != nil {
		return false, err
	}
	err = mongo.Find(application, bson.M{"_id": bson.ObjectIdHex(applicationId)}).One(&application)
	if err != nil {
		return false, err
	}
	if application.OwnerId != user.Id {
		return false, nil
	}
	return true, nil
}
コード例 #8
0
ファイル: models.go プロジェクト: Hipo/gotcha
func (a Application) UrlCount() string {
	url := Url{}
	count, err := mongo.Find(url, bson.M{"application_id": a.Id}).Count()
	if err != nil {
		fmt.Println(err)

	}
	return fmt.Sprintf("%v", count)
}
コード例 #9
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func UrlDetailTemplateHandler(w http.ResponseWriter, r *http.Request) {
	t := template.New("url_detail.tmpl")
	t = template.Must(t.ParseGlob("templates/*.tmpl"))
	url := Url{}
	vars := mux.Vars(r)

	urlId := vars["urlId"]
	urlp := &url
	err := mongo.Find(url, bson.M{"_id": bson.ObjectIdHex(urlId)}).One(urlp)
	if err != nil {
		w.WriteHeader(404)
		return

	}
	t.Execute(w, urlp)
}
コード例 #10
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func UrlListTemplateHandler(w http.ResponseWriter, r *http.Request) {
	t := template.New("urls.tmpl")
	t = template.Must(t.ParseGlob("templates/*.tmpl"))
	application := Application{}
	vars := mux.Vars(r)

	applicationId := vars["applicationId"]
	applicationp := &application
	err := mongo.Find(application, bson.M{"_id": bson.ObjectIdHex(applicationId)}).One(applicationp)
	if err != nil {
		w.WriteHeader(404)
		return

	}
	t.Execute(w, applicationp)
}
コード例 #11
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func UrlEditHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")

	vars := mux.Vars(r)
	urlId := vars["urlId"]
	var url Url
	err := mongo.Find(url, bson.M{"_id": bson.ObjectIdHex(urlId)}).One(&url)
	if err != nil {
		w.WriteHeader(400)
		return
	}
	applicationId := vars["applicationId"]
	token := r.FormValue("token")
	isAuthenticated, err := IsAuthenticated(token, applicationId)
	if isAuthenticated != true {
		w.WriteHeader(403)
		return
	}
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(400)
		return
	}
	urlp := &Url{WaitTime: 100,
		TryCount: 10}

	err = json.Unmarshal(body, urlp)
	if err != nil {
		fmt.Println(err)
	}

	urlp.ApplicationId = bson.ObjectIdHex(applicationId)
	urlQuery := bson.M{"_id": url.Id}
	updateData := urlp.Deserialize()

	err = urlp.UpdateUrl(urlQuery, updateData)
	if err != nil {
		w.WriteHeader(400)
		return
	}
}
コード例 #12
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func ApplicationAddHandler(w http.ResponseWriter, r *http.Request) {
	application := Application{}
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(err)
	}

	applicationp := &application
	err = json.Unmarshal(body, applicationp)
	if err != nil {
		panic(err)
	}
	user := users.User{}
	token := r.FormValue("token")
	err = mongo.Find(user, bson.M{"token": token}).One(&user)

	applicationp.Id = bson.NewObjectId()
	application.OwnerId = user.Id
	applicationp.CreateApplication()
	w.WriteHeader(200)
	return
}
コード例 #13
0
ファイル: handlers.go プロジェクト: Hipo/gotcha
func UrlListHandler(w http.ResponseWriter, r *http.Request) {

	url := Url{}
	var urls []Url
	vars := mux.Vars(r)
	applicationId := vars["applicationId"]
	token := r.FormValue("token")

	isAuthenticated, err := IsAuthenticated(token, applicationId)
	if isAuthenticated != true {
		w.WriteHeader(403)
		return
	}

	err = mongo.Find(url, bson.M{"application_id": bson.ObjectIdHex(applicationId)}).All(&urls)
	if err != nil {
		w.WriteHeader(404)
		return
	}
	serializedUrls := make([]map[string]interface{}, len(urls))
	for i, element := range urls {
		serializedUrls[i], err = element.Serialize()
	}
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(400)
		return
	}

	err = json.NewEncoder(w).Encode(serializedUrls)
	if err != nil {
		fmt.Println(err)
		w.WriteHeader(400)
		return
	}

}