mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 09:37:09 +00:00
Add nil check for online storage
This commit is contained in:
parent
be564eabf9
commit
b6be41371d
2 changed files with 25 additions and 6 deletions
|
|
@ -16,14 +16,23 @@ type Client struct {
|
|||
gclient *storage.Client
|
||||
}
|
||||
|
||||
// NewInitClient returns nil of client is not initialized
|
||||
func NewInitClient() *Client {
|
||||
projectID := os.Getenv("GCP_PROJECT")
|
||||
bucketName := "game-save"
|
||||
return NewClient(projectID, bucketName)
|
||||
|
||||
client, err := NewClient(projectID, bucketName)
|
||||
if err != nil {
|
||||
log.Printf("Err: Failed to create client: %v", err)
|
||||
} else {
|
||||
log.Println("Online storage is initialized")
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// NewClient inits a new Client accessing to GCP
|
||||
func NewClient(projectID string, bucketName string) *Client {
|
||||
func NewClient(projectID string, bucketName string) (*Client, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Sets your Google Cloud Platform project ID.
|
||||
|
|
@ -31,7 +40,7 @@ func NewClient(projectID string, bucketName string) *Client {
|
|||
// Creates a client.
|
||||
gclient, err := storage.NewClient(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create client: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Creates a Bucket instance.
|
||||
|
|
@ -40,11 +49,16 @@ func NewClient(projectID string, bucketName string) *Client {
|
|||
return &Client{
|
||||
bucket: bucket,
|
||||
gclient: gclient,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Savefile save srcFile to GCP
|
||||
func (c *Client) SaveFile(name string, srcFile string) (err error) {
|
||||
// Bypass if client is nil
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
reader, err := os.Open(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -64,6 +78,11 @@ func (c *Client) SaveFile(name string, srcFile string) (err error) {
|
|||
|
||||
// Loadfile load file from GCP
|
||||
func (c *Client) LoadFile(name string) (data []byte, err error) {
|
||||
// Bypass if client is nil
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
|
||||
rc, err := c.bucket.Object(name).NewReader(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ type Handler struct {
|
|||
|
||||
// NewHandler returns a new server
|
||||
func NewHandler(overlordConn *websocket.Conn, isDebug bool, gamePath string) *Handler {
|
||||
log.Println("new OverlordClient")
|
||||
onlineStorage := storage.NewInitClient()
|
||||
|
||||
return &Handler{
|
||||
oClient: NewOverlordClient(overlordConn),
|
||||
|
|
@ -53,7 +53,7 @@ func NewHandler(overlordConn *websocket.Conn, isDebug bool, gamePath string) *Ha
|
|||
isDebug: isDebug,
|
||||
gamePath: gamePath,
|
||||
|
||||
onlineStorage: storage.NewInitClient(),
|
||||
onlineStorage: onlineStorage,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue