コード例 #1
0
ファイル: api.go プロジェクト: cjqian/princeton_museum_api
func infoHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")

	fmt.Println(r.URL.Path)
	path := r.URL.Path[1:]
	request := urlParser.ParseURL(path)
	fmt.Println(request)
	tableName := request.TableName
	tableParameters := request.Parameters
	specialParameters := request.SpecialParameters

	var resp interface{}
	fmt.Println(tableName)
	fmt.Println(tableParameters)
	fmt.Println(specialParameters)

	if tableName == "" {
		records := sqlParser.GetTableNames()
		numResults := len(records)
		resp = outputFormatter.MakeApiWrapper(request, records, numResults, specialParameters)

	} else if len(tableParameters) <= 0 {
		records := sqlParser.GetColumnNames(tableName)
		numResults := len(records)
		resp = outputFormatter.MakeApiWrapper(request, records, numResults, specialParameters)

	} else {
		records := make(map[string][]string, 0)
		rowWrappers := make(map[string]interface{}, 0)
		for _, columnName := range tableParameters {
			records[columnName] = sqlParser.GetColumnValues(tableName, columnName)

			curNumResults := len(records[columnName])
			rowWrappers[columnName] = outputFormatter.MakeInfoWrapper(curNumResults, records[columnName])
		}
		numResults := len(records)
		resp = outputFormatter.MakeApiWrapper(request, rowWrappers, numResults, specialParameters)

	}

	fmt.Println(resp)

	enc := json.NewEncoder(w)
	enc.Encode(resp)
}
コード例 #2
0
ファイル: api.go プロジェクト: cjqian/princeton_museum_api
//handles all calls to the API
func apiHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")

	//url of type "/table?parameterA=valueA&parameterB=valueB/id
	path := r.URL.Path[1:]
	//fmt.Println(path)
	if r.URL.RawQuery != "" {
		path += "?" + r.URL.RawQuery
	}

	request := urlParser.ParseURL(path)
	//fmt.Println(request)

	//note: tableName could also refer to a view
	tableName := request.TableName
	tableParameters := request.Parameters
	specialParameters := request.SpecialParameters

	var records []map[string]interface{}

	//GETS the request
	if tableName != "" {
		fmt.Println("Getting request")
		records, _ = sqlParser.Get(tableName, tableParameters, specialParameters)
		/*if err != nil {*/
		/*errString = err.Error()*/
		/*}*/
	} else {
		records = nil
	}

	fmt.Println("Getting numbers, lim String")
	numResults := sqlParser.GetNumRows(tableName, sqlParser.GetWhereString(tableParameters), sqlParser.GetLimString(specialParameters))

	fmt.Println("Making wrapper")
	resp := outputFormatter.MakeApiWrapper(request, records, numResults, specialParameters)
	enc := json.NewEncoder(w)
	enc.Encode(resp)
}