-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: v.login <v.login@s7.ru>
- Loading branch information
v.login
committed
Nov 21, 2019
1 parent
0959725
commit da4a19f
Showing
62 changed files
with
17,241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.