Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d726cac
commit 1f06d1b
Showing
10 changed files
with
254 additions
and
38 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
.vscode | ||
.idea | ||
|
||
*/**/node_modules | ||
*/**/node_modules | ||
gcs-credentials.json |
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,52 @@ | ||
package lib | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/karlseguin/ccache" | ||
) | ||
|
||
type Cache interface { | ||
Get(key string) (bool, []byte) | ||
Set(key string, value []byte, TTL time.Duration) | ||
GetSet(key string, set func() ([]byte, error), TTL time.Duration) ([]byte, error) | ||
} | ||
|
||
func NewCache() Cache { | ||
return &cache{ | ||
client: ccache.New(ccache.Configure()), | ||
} | ||
} | ||
|
||
type cache struct { | ||
client *ccache.Cache | ||
} | ||
|
||
func (c *cache) Get(key string) (bool, []byte) { | ||
item := c.client.Get(key) | ||
if item != nil { | ||
return true, item.Value().([]byte) | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (c *cache) Set(key string, value []byte, TTL time.Duration) { | ||
c.client.Set(key, value, TTL) | ||
} | ||
|
||
func (c *cache) GetSet(key string, getValue func() ([]byte, error), TTL time.Duration) ([]byte, error) { | ||
hit, value := c.Get(key) | ||
if hit { | ||
return value, nil | ||
} | ||
|
||
value, err := getValue() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.Set(key, value, TTL) | ||
|
||
return value, nil | ||
} |
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,86 @@ | ||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
|
||
"cloud.google.com/go/storage" | ||
) | ||
|
||
// Sets the name for the new bucket. | ||
const bucketName = "gocity" | ||
|
||
type Storage interface { | ||
Get(projectName string) (bool, []byte, error) | ||
Save(projectName string, content []byte) error | ||
Delete(projectName string) error | ||
} | ||
|
||
type GCS struct { | ||
ctx context.Context | ||
client *storage.Client | ||
} | ||
|
||
func NewGCS(ctx context.Context) (Storage, error) { | ||
client, err := storage.NewClient(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &GCS{ | ||
ctx: ctx, | ||
client: client, | ||
}, nil | ||
} | ||
|
||
func getObjectName(name string) string { | ||
return fmt.Sprintf("%s.json", name) | ||
} | ||
|
||
func (g *GCS) Get(projectName string) (bool, []byte, error) { | ||
object := g.client.Bucket(bucketName).Object(getObjectName(projectName)) | ||
if object == nil { | ||
return false, nil, nil | ||
} | ||
|
||
reader, err := object.NewReader(g.ctx) | ||
if err != nil { | ||
if err == storage.ErrObjectNotExist { | ||
log.Print("file not exists...") | ||
return false, nil, nil | ||
} | ||
|
||
return false, nil, err | ||
} | ||
defer reader.Close() | ||
|
||
data, err := ioutil.ReadAll(reader) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
|
||
return true, data, nil | ||
} | ||
|
||
func (g *GCS) Save(projectName string, content []byte) error { | ||
client, err := storage.NewClient(g.ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buffer := bytes.NewBuffer(content) | ||
wc := client.Bucket(bucketName).Object(getObjectName(projectName)).NewWriter(g.ctx) | ||
if _, err = io.Copy(wc, buffer); err != nil { | ||
return err | ||
} | ||
|
||
return wc.Close() | ||
} | ||
|
||
func (g *GCS) Delete(projectName string) error { | ||
return nil | ||
} |
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
Oops, something went wrong.