Skip to content

Commit

Permalink
init cassandra #72
Browse files Browse the repository at this point in the history
Signed-off-by: v.login <v.login@s7.ru>
  • Loading branch information
v.login committed Nov 21, 2019
1 parent 0959725 commit da4a19f
Show file tree
Hide file tree
Showing 62 changed files with 17,241 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ test: ## Run all test
run: ## Run this project in docker-compose
@docker-compose \
-f docker-compose.yaml \
-f ops/docker-compose/database/cassandra.yaml \
-f ops/docker-compose/gateway/traefik.yaml \
-f ops/docker-compose/application/shortlink.yaml \
-f ops/docker-compose/tooling/opentracing.yaml \
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ docker-compose \
+ LevelDB
+ Badger
+ SQLite
+ Сassandra

### Cloud-Native

Expand All @@ -79,14 +80,15 @@ docker-compose \

| Name | Default | Description |
|---------------------|-------------------------------------------------------|----------------------------------------------------------|
| STORE_TYPE | ram | Select: postgres, mongo, redis, dgraph, sqlite, leveldb, badger, ram |
| STORE_TYPE | ram | Select: postgres, mongo, redis, dgraph, sqlite, leveldb, badger, ram, cassandra |
| STORE_MONGODB_URI | mongodb://localhost:27017 | MongoDB URI |
| STORE_BADGER_PATH | /tmp/links.badger | Badger path to file |
| STORE_DGRAPH_URI | localhost:9080 | DGRAPH link |
| STORE_LEVELDB_PATH | /tmp/links.db | LevelDB path to file |
| STORE_POSTGRES_URI | postgres://postgres:postgres@localhost:5432/shortlink | Postgres URI |
| STORE_REDIS_URI | localhost:6379 | Redis URI |
| STORE_SQLITE_PATH | /tmp/links.sqlite | SQLite URI |
| STORE_CASSANDRA_URI | localhost | Cassandra URI |
| LOG_LEVEL | 3 | Log level. Select 0-4 (Fatal->Debug) |
| LOG_TIME_FORMAT | 2006-01-02T15:04:05.999999999Z07:00 | Log time format (golang time format) |
| TRACER_SERVICE_NAME | ShortLink | Service Name |
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/go-chi/render v1.0.1
github.com/go-redis/redis v6.15.6+incompatible
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gocql/gocql v0.0.0-20191106222750-ae2f7fc85f32
github.com/golang/protobuf v1.3.2
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -111,6 +113,8 @@ github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZp
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gocql/gocql v0.0.0-20191106222750-ae2f7fc85f32 h1:rCAnpA1hmiR2r7/fCAwj8Sr7fYrXW/1Ba5og11BcpRk=
github.com/gocql/gocql v0.0.0-20191106222750-ae2f7fc85f32/go.mod h1:DL0ekTmBSTdlNF25Orwt/JMzqIq3EJ4MVa/J/uK64OY=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand All @@ -125,6 +129,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down Expand Up @@ -158,6 +163,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.8.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.12.1 h1:zCy2xE9ablevUOrUZc3Dl72Dt+ya2FNAvC2yLYMHzi4=
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -546,6 +553,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
Expand Down
102 changes: 102 additions & 0 deletions internal/store/cassandra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package store

import (
"github.com/batazor/shortlink/pkg/link"
"github.com/gocql/gocql"
"github.com/spf13/viper"
)

// CassandraConfig ...
type CassandraConfig struct { // nolint unused
URI string
}

// CassandraLinkList implementation of store interface
type CassandraLinkList struct { // nolint unused
client *gocql.Session
config CassandraConfig
}

// Init ...
func (c *CassandraLinkList) Init() error {
var err error

// Set configuration
c.setConfig()

// Connect to CassandraDB
cluster := gocql.NewCluster(c.config.URI)
cluster.Keyspace = "shortlink"

c.client, err = cluster.CreateSession()
if err != nil {
panic(err)
}

return nil
}

// Close ...
func (c *CassandraLinkList) Close() error {
c.client.Close()
return nil
}

// Get ...
func (c *CassandraLinkList) Get(id string) (*link.Link, error) {
panic("implement me")

return nil, nil
}

// Add ...
func (c *CassandraLinkList) Add(data link.Link) (*link.Link, error) {
hash := data.CreateHash([]byte(data.Url), []byte("secret"))
data.Hash = hash[:7]

if err := c.client.Query(`INSERT INTO links (url hash describe) VALUES (?, ?, ?)`, data.Url, data.Hash, data.Describe).Exec(); err != nil {
return nil, err
}

return &data, nil
}

// List ...
func (c *CassandraLinkList) List() ([]*link.Link, error) {
iter := c.client.Query(`SELECT url, hash, describe FROM links`).Iter()

// Here's an array in which you can store the decoded documents
var response []*link.Link
var link link.Link

for iter.Scan(&link) {
response = append(response, &link)
}

if err := iter.Close(); err != nil {
return nil, err
}

return response, nil
}

// Update ...
func (c *CassandraLinkList) Update(data link.Link) (*link.Link, error) {
return nil, nil
}

// Delete ...
func (c *CassandraLinkList) Delete(id string) error {
panic("implement me")

return nil
}

// setConfig - set configuration
func (c *CassandraLinkList) setConfig() {
viper.AutomaticEnv()
viper.SetDefault("STORE_CASSANDRA_URI", "localhost")
c.config = CassandraConfig{
URI: viper.GetString("STORE_CASSANDRA_URI"),
}
}
14 changes: 14 additions & 0 deletions ops/docker-compose/database/cassandra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '2.4'

services:

cassandra:
image: cassandra:3.11
restart: on-failure
container_name: cassandra
ports:
- 7000:7000
- 9042:9042
environment:
- CASSANDRA_BROADCAST_ADDRESS=host.docker.internal
- CASSANDRA_SEEDS=host.docker.internal
5 changes: 5 additions & 0 deletions vendor/github.com/gocql/gocql/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/gocql/gocql/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions vendor/github.com/gocql/gocql/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit da4a19f

Please sign in to comment.