예제 #1
0
파일: api.go 프로젝트: AeroNotix/freepoint
func UpdateMetadata(self *ss.AppServer, w http.ResponseWriter, req *http.Request) error {
	job, err := ss.NewAsyncMetadataUpdate(req)
	if err != nil {
		ss.SendJSON(w, false)
		return err
	}
	err = self.UpdateEntry(job)
	if err != nil {
		ss.SendJSON(w, false)
		return err
	}
	ss.SendJSON(w, true)
	return nil
}
예제 #2
0
파일: api.go 프로젝트: AeroNotix/freepoint
// This is a handler which checks the POST for the data required to
// make a change to the database.
//
// This function will have one of two possible side effects which are
// writing a JSON response to the caller.
func ChangeTable(self *ss.AppServer, w http.ResponseWriter, req *http.Request) error {

	job, err := ss.NewAsyncJob(req)
	if err != nil {
		ss.SendJSON(w, false)
		return err
	}

	err = self.WriteEntry(job)
	if err != nil {
		ss.SendJSON(w, false)
		return err
	}

	ss.SendJSON(w, true)
	return nil
}
예제 #3
0
파일: api.go 프로젝트: AeroNotix/freepoint
// This is a URL attached to ^login/?$ because then we can
// programmatically log in the user and also check if the user is
// login-able. Eventually userLogin will create a session row in the
// database and send the sessionid key back to the requester.
func UserLogin(self *ss.AppServer, w http.ResponseWriter, req *http.Request) error {
	errorhandler := func(w http.ResponseWriter, req *http.Request, err error) {}
	successhandler := func(w http.ResponseWriter, req *http.Request) {}
	userdata := new(ss.User)
	switch req.Header.Get("Content-type") {
	case "application/x-www-form-urlencoded":
		userdata.Password = req.FormValue("password")
		userdata.Username = req.FormValue("username")
		errorhandler = func(w http.ResponseWriter, req *http.Request, err error) {
			http.Redirect(w, req, "/loginfail/", http.StatusSeeOther)
		}
		successhandler = func(w http.ResponseWriter, req *http.Request) {
			http.Redirect(w, req, "/user/", http.StatusSeeOther)
		}
	case "application/json":
		// We retrieve the JSON string and encode it into the proper JSON
		// request struct.
		json_dec := json.NewDecoder(req.Body)
		err := json_dec.Decode(&userdata)
		if err != nil {
			logfile.Println(err)
			return err
		}
		errorhandler = func(w http.ResponseWriter, req *http.Request, err error) {
			ss.SendJSONError(w, err)
		}
		successhandler = func(w http.ResponseWriter, req *http.Request) {
			ss.SendJSON(w, true)
		}
	}

	if ok, err := Login(userdata); !ok {
		errorhandler(w, req, err)
		return err
	}

	session, err := ss.CreateMySQLSession()
	if err != nil {
		fmt.Println("Error creating cookie: " + err.Error())
	} else {
		session.Add("user", userdata.Username)
		http.SetCookie(w, &http.Cookie{
			Name:    "session",
			Value:   string(session.Key),
			Domain:  req.Host,
			Path:    "/",
			Expires: time.Now().Add(24 * time.Hour),
		})
	}
	successhandler(w, req)
	return nil
}
예제 #4
0
파일: api.go 프로젝트: AeroNotix/freepoint
// deleteRows handles the deletion of a group of rows from a table.
func DeleteRows(self *ss.AppServer, w http.ResponseWriter, req *http.Request) error {

	job, err := ss.NewAsyncDelete(req)
	if err != nil {
		ss.SendJSONError(w, err)
		return err
	}
	err = self.DeleteEntries(job)
	if err != nil {
		ss.SendJSONError(w, err)
	}

	ss.SendJSON(w, true)
	return nil
}
예제 #5
0
파일: api.go 프로젝트: AeroNotix/freepoint
// InsertData server as the back-end for handling data insertion into
// existing tables.  The request holds all the relevent data encoded
// into rows.
func InsertData(self *ss.AppServer, w http.ResponseWriter, req *http.Request) error {

	job, err := ss.NewAsyncInsert(req)
	if err != nil {
		ss.SendJSONError(w, err)
		return err
	}

	err = self.InsertEntry(job)
	if err != nil {
		ss.SendJSONError(w, err)
		return err
	}

	ss.SendJSON(w, true)
	return nil
}
예제 #6
0
파일: api.go 프로젝트: AeroNotix/freepoint
// Simple server which is used to return parameters for particular databases.
func DatabaseParameters(self *ss.AppServer, w http.ResponseWriter, req *http.Request) error {

	// We add the content type so the browser can accept it properly if
	// we get requested via a browser.
	w.Header().Set("Content Type", "application/json")

	// We retrieve the JSON string and encode it
	// into the proper JSON request struct.
	json_dec := json.NewDecoder(req.Body)
	dbreq := new(ss.DatabaseRequest)
	err := json_dec.Decode(&dbreq)
	if err != nil {
		ss.SendJSON(w, false)
		logfile.Println(err)
		return err
	}

	// Get the metadata associated with the table.
	metadata, err := ss.GetMetadata(dbreq)
	if err != nil {
		val, ok := err.(ss.AppError)
		if !ok {
			ss.SendJSONError(w, err)
			return err
		}
		if val.Code() != 3 {
			ss.SendJSONError(w, err)
			return err
		}
	}

	// Marshal our metadata into a struct and encode.
	jsonMap := ss.NewJSONMessage(metadata)

	rows, err := ss.GetRows(dbreq)
	if err != nil {
		return err
	}
	headings, err := ss.GetHeadings(dbreq)
	if err != nil {
		logfile.Println(err)
		return err
	}
	// Add the column names to the jsonMap
	jsonMap.Headings = headings

	// Iterate through the rows, create a [][]byte to hold a row.
	// Then we encode the row by asserting it to []byte and appending
	// it to the [][]byte row. We then AddRow that row.
	for _, row := range rows {
		var newrow = [][]byte{}
		for _, item := range row {
			data, ok := item.([]byte)
			if ok {
				newrow = append(newrow, data)
			}
		}
		jsonMap.AddRow(newrow)
	}

	// Send the JSON to the client.
	err = json.NewEncoder(w).Encode(jsonMap)
	return err
}