Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebthun committed Oct 5, 2014
0 parents commit b20d485
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2014 Mike B

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# negronicql


Golang [Negroni](https://github.com/codegangsta/negroni) middleware for the [gocql package](https://github.com/gocql/gocql).

### Dependencies

Depends on [Gorilla Context Package](http://www.gorillatoolkit.org/pkg/context)
`

### Usage

```
import(
"github.com/mikebthun/negronicql"
"github.com/gorilla/mux"
"github.com/gorilla/context"
)
```


```
router := mux.NewRouter()
router.HandleFunc("/", MyHandler ).Methods("POST")
n := negroni.Classic()
cqldb := negronicql.NewMiddleware()
//set cluster options here if needed, defaults to localhost
//cqldb.Ips = []string{"127.0.0.1", "127.0.0.2"}
//cqldb.Consistency = gocql.Quorum
cqldb.Keyspace = "MyKeySpace"
cqldb.Connect()
//defer close here IMPORTANT
defer cqldb.Session.Close()
n.Use(cqldb)
n.UseHandler(router)
n.Run(":3000")
func MyHandler(w http.ResponseWriter, req *http.Request) {
//grab the session here
session = context.Get( req, "Session" ).(*gocql.Session)
}
```

Run your queries like normal on the gocql session:

```
session.Query( `SELECT * FROM blah` ).Exec()
```

### License

The MIT License (MIT)
67 changes: 67 additions & 0 deletions negronicql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package negronicql

import(

"github.com/gocql/gocql"
"github.com/gorilla/context"
"net/http"


)



type Middleware struct {

Cluster *gocql.ClusterConfig
Ips []string
Keyspace string
Consistency gocql.Consistency
Session *gocql.Session

}


func NewMiddleware() *Middleware {
return &Middleware{}
}

// be sure to defer session.close()
func (m *Middleware) Connect() error {

//default to localhost
if len(m.Ips) < 1 {

m.Ips = []string{"127.0.0.1"}

}

//create cluster config
m.Cluster = gocql.NewCluster( m.Ips[0] )

session, err := m.Cluster.CreateSession()

m.Session = session

if err != nil {

return err

}


return nil


}

// The middleware handler
func (m *Middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

//attach the session
context.Set( r, "Session", m.Session )

// Call the next middleware handler
next(rw, r)
}

163 changes: 163 additions & 0 deletions negronicql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package negronicql_test

import(

"github.com/mikebthun/negronicql"
"github.com/gocql/gocql"
"testing"
"fmt"
//"reflect"

)


var(
keyspace = "mikebthun_negronicql_middleware_tests"
ips = []string{"127.0.0.1"}
columnFamily = "go_db_package_test"
email = "test@test.com"
)


func TestSetup(t *testing.T) {

session := setup(t, "")

defer session.Close()

//create the keyspace if does not exist
cql := fmt.Sprintf( `
CREATE KEYSPACE %s WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
}`, keyspace )


session.Query( cql ).Exec()


cql = fmt.Sprintf( "DROP TABLE %s.%s", keyspace, columnFamily)

session.Query( cql ).Exec()

cql = fmt.Sprintf( `
CREATE TABLE %s.%s
(
email text,
first text,
last text,
PRIMARY KEY ( email )
)
`, keyspace, columnFamily )

if err := session.Query( cql ).Exec(); err != nil {

t.Errorf("%s",err)

}


}


func TestInsertWithParams(t *testing.T) {


session := setup(t, keyspace)
defer session.Close()

cql := fmt.Sprintf( `
INSERT INTO %s.%s
(email, first, last)
VALUES ( ?, ?, ? )
`, keyspace, columnFamily )


session.Query( cql , email, "Mike", "B" ).Exec()






}


func TestSelectWithParams(t *testing.T) {


session := setup(t,keyspace)
defer session.Close()


cql := fmt.Sprintf(`
SELECT email
FROM %s.%s
WHERE email = ?
LIMIT 1
`, keyspace, columnFamily )

var check_email string


if err := session.Query( cql , email ).Consistency(gocql.One).Scan(&check_email) ; err != nil {

t.Fatalf("Query failed %s", err.Error())

}


if check_email != email {

t.Errorf("Email should be %s but is %s.", email, check_email )

}

cleanup(t)

}


func cleanup(t *testing.T) {

//Setup Cassandra configuration
session := setup(t,keyspace)


cql := fmt.Sprintf( "DROP KEYSPACE %s", keyspace )

session.Query( cql ).Exec()



}

func setup(t *testing.T, k string) ( *gocql.Session ) {

//Setup Cassandra configuration

conn := negronicql.NewMiddleware()
err := conn.Connect()

if err != nil {

t.Fatalf("Setup failed: %s", err )

}

return conn.Session


}



0 comments on commit b20d485

Please sign in to comment.