Sample Application

    +
    Discover how to program interactions with the Couchbase Server via the data, Query, and search services — using the Travel Sample Application with the built-in Travel Sample data Bucket. This version also demonstrated the Developer Preview of the upcoming Collections feature.

    Travel Sample Application uses the Travel Sample data Bucket, which ships with Couchbase Server. For Couchbase Server 6.5, make sure that you have at least one node each of data; query; index; and search. For a development box, mixing more than one of these on a single node (given enough memory resources) is perfectly acceptable. If you have yet to install Couchbase Server in your development environment, start here.

    Then load up the Travel Sample Bucket, using either the Web interface or the command line. You will also need to create a Search Index — Query indexes are taken care of by the Sample Bucket.

    Preparation

    As well as the Go SDK 2.0 and Couchbase Server, set up as described above, you will need git to fetch the travel sample application code:

    git clone https://github.com/couchbaselabs/try-cb-golang.git

    Change directory into your cloned repository, and check out the 6.5 branch (this will most probably be enabled as the default branch).

    cd try-cb-golang
    git checkout 6.5

    Running the Travel Sample Application

    Next, edit the cbConnStr field in main.go to the one for your containerised Couchbase Server (or localhost, 127.0.0.1, if appropriate), and any other local changes — such as password. From here onwards, we’ll assume the defaults.

    And run with

    go run main.go

    After the build, with your Web browser of choice, head to port 8080 of the local machine — http://localhost:8080.

    Using the Sample App

    Give yourself a username and password and click Register.

    Now try out a few queries, and see Search in action for the hotel finder feature..

    Sample App Backend

    The backend code shows Couchbase Java SDK in action with Query and Search, but also how to plug together all of the elements and build an application with Couchbase Server and the Java SDK.

    Here’s the Search code, where AirportSearch checks to see whether the search term is a three or four letter FAA or ICAO abbreviation, and if not searches for it as an airport name:

    func AirportSearch(w http.ResponseWriter, req *http.Request) {
    	var respData jsonAirportSearchResp
    
    	searchKey := req.FormValue("search")
    
    	var queryStr string
    	if len(searchKey) == 3 {
    		queryStr = fmt.Sprintf("SELECT airportname FROM `travel-sample` WHERE faa='%s'", strings.ToUpper(searchKey))
    	} else if len(searchKey) == 4 && searchKey == strings.ToUpper(searchKey) {
    		queryStr = fmt.Sprintf("SELECT airportname FROM `travel-sample` WHERE icao ='%s'", searchKey)
    	} else {
    		queryStr = fmt.Sprintf("SELECT airportname FROM `travel-sample` WHERE airportname like '%s%%'", searchKey)
    	}
    
    	respData.Context.Add(queryStr)
    	rows, err := globalCluster.Query(queryStr, nil)
    	if err != nil {
    		writeJsonFailure(w, 500, err)
    		return
    	}
    
    	respData.Data = []jsonAirport{}
    	var airport jsonAirport
    	for rows.Next(&airport) {
    		respData.Data = append(respData.Data, airport)
    		airport = jsonAirport{}
    	}
    
    	encodeRespOrFail(w, respData)
    }

    The main.go file also contains the functions for handling users, registration, and N1QL queries — which you may find instructive for putting together your first Couchbase applications in Go.

    Data Model

    See the Travel App Data Model reference page for more information about the sample data set used.