Coder Social home page Coder Social logo

cfmgo's Introduction

cfmgo

A MongoDB integration package for Cloud Foundry

Overview

cfmgo is a package to assist you in connecting Go applications running on Cloud Foundry to MongoDB.

Usage

go get github.com/cloudnativego/cfmgo

appEnv, _ := cfenv,Current() //relies on github.com/cloudfoundry-community/go-cfenv
serviceName := os.Getenv("DB_NAME")
serviceURIName := os.Getenv("DB_URI")
serviceURI := cfmgo.GetServiceBinding(serviceName, serviceURIName, appEnv)
collection := cfmgo.Connect(cfmgo.NewCollectionDialer, serviceURI, "my-collection")

cfmgo/params

params is a package that extracts query parameters from a request to be used in cfmgo.Collection.Find() operations.

Example

func ListInventoryItemsHandler(collection cfmgo.Collection) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		collection.Wake()

		params := params.Extract(req.URL.Query())

		items := make([]RedactedInventoryItem, 0)

		if count, err := collection.Find(params, &items); err == nil {
			Formatter().JSON(w, http.StatusOK, wrapper.Collection(&items, count))
		} else {
			Formatter().JSON(w, http.StatusNotFound, wrapper.Error(err.Error()))
		}
	}
}

cfmgo/wrapper

wrapper is a simple helper to wrap API response data and errors in a consistent structure.

//ResponseWrapper provides a standard structure for API responses.
type ResponseWrapper struct {
	//Status indicates the result of a request as "success" or "error"
	Status string `json:"status"`
	//Data holds the payload of the response
	Data interface{} `json:"data,omitempty"`
	//Message contains the nature of an error
	Message string `json:"message,omitempty"`
	//Count contains the number of records in the result set
	Count int `json:"count,omitempty"`
}

Examples

wrapper.Error(err) yields:

{
"status": "error",
"message": "error message text"
}

wrapper.One(&someRecord) yields:

{
"status": "success",
"data": {
	"id": 1,
	"name": "fluffy"
	}
}

wrapper.Collection(&someResults, count) yields:

{
"status": "success",
"data": [
	{
	"id": 1,
	"name": "fluffy"
	},
	{
	"id": 2,
	"name": "thiggy"
	}
	],
"count": 2
}

Note: count represents the total number of matching records from the query, not the number of records returned in the result set. That number is governed by limit.

cfmgo's People

Contributors

dnem avatar autodidaddict avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.