Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
fix: loading GCLOUD_CREDENTIALS
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtim committed Apr 26, 2022
1 parent f0cd860 commit b0d06b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ release-helm:
script:
- helm plugin install https://github.com/chartmuseum/helm-push.git
- "sed -i \"s/version:.*/version: ${CI_COMMIT_TAG}/\" helm/Chart.yaml"
- "sed -i \"s/appVersion:.*/appVersion: \"${CI_COMMIT_TAG}\"/\" helm/Chart.yaml"
- helm package helm
- helm cm-push ./cjtim-backend-go-${CI_COMMIT_TAG}.tgz ${CI_PROJECT_NAME}
needs: []
Expand Down
4 changes: 2 additions & 2 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

var (
AuthorizationHeader = "Authorization"
defaultDotEnvKey = "DOTENV_FILE"
Config *ConfigType
origConfig ConfigType
secretVersion int64 = 0
)

type ConfigType struct {
Expand Down Expand Up @@ -51,7 +51,7 @@ func init() {
defer fp.Close()

cfg := ConfigType{}
envFile := os.Getenv("DOTENV_FILE")
envFile := os.Getenv(defaultDotEnvKey)
if envFile == "" {
envFile = ".env"
}
Expand Down
15 changes: 13 additions & 2 deletions internal/app/controllers/binance/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ func UpdatePrice(c *fiber.Ctx) error {
return err
}
user := middlewares.GetUser(c)
repository.BinanceRepo.FindOneAndReplace(context.TODO(), bson.M{"lineUid": user.UserID}, &data)
err = repository.BinanceRepo.FindOneAndReplace(context.TODO(), bson.M{"lineUid": user.UserID}, &data)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.SendStatus(200)
}

Expand All @@ -77,7 +82,13 @@ func Cronjob(c *fiber.Ctx) error {
return err
}

line_notify.TriggerLineNotify(&data)
_, errorUsers := line_notify.TriggerLineNotify(&data)

if len(errorUsers) > 0 {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"error": "cannot send notify for some users.",
})
}

return c.SendStatus(200)
}
37 changes: 26 additions & 11 deletions internal/app/controllers/line/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,44 @@ func Webhook(c *fiber.Ctx) error {
}
err = line.Reply(event.ReplyToken, []interface{}{line.WeatherFlexMessage(weatherData)})
if err != nil {
zap.L().Error("Line webhook error", zap.String("event", "location"), zap.Error(err))
zap.L().Error("Line webhook error - reply", zap.String("event", "location"), zap.Error(err))
}
return err
case "file":
message := event.Message.(*linebot.FileMessage)
_, err = files.Client.AddFromLine(message.ID, event.Source.UserID)
if err != nil {
zap.L().Error("Line webhook error", zap.String("event", "file"), zap.Error(err))
}
return err
return addFile(message.ID, event.Source.UserID, event.ReplyToken)

case "image":
message := event.Message.(*linebot.ImageMessage)
_, err = files.Client.AddFromLine(message.ID, event.Source.UserID)
if err != nil {
zap.L().Error("Line webhook error", zap.String("event", "image"), zap.Error(err))
}
return err
return addFile(message.ID, event.Source.UserID, event.ReplyToken)
}
}

return c.SendStatus(fiber.StatusOK)
}

func addFile(msgId, userId, replyToken string) error {
newfile, err := files.Client.AddFromLine(msgId, userId)
if err != nil {
zap.L().Error("Line webhook error",
zap.String("event", "file"),
zap.String("msgId", msgId),
zap.String("userId", userId),
zap.Error(err),
)
}
msgs := []interface{}{
map[string]interface{}{
"type": "text",
"text": newfile.URL.ShortURL,
}}
err = line.Reply(replyToken, msgs)
if err != nil {
zap.L().Error("error reply msg", zap.Error(err))
}
return err
}

// EventMessageType - Check event message type
func EventMessageType(e *linebot.Event) linebot.MessageType {
switch e.Message.(type) {
Expand Down
3 changes: 2 additions & 1 deletion internal/app/controllers/urls/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package urls

import (
"context"
"net/http"

"github.com/cjtim/cjtim-backend-go/internal/app/middlewares"
"github.com/cjtim/cjtim-backend-go/internal/app/repository"
Expand Down Expand Up @@ -72,5 +73,5 @@ func Delete(c *fiber.Ctx) error {
if err != nil {
return err
}
return nil
return c.SendStatus(http.StatusOK)
}
2 changes: 1 addition & 1 deletion internal/pkg/gstorage/gstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type c struct {
func GetClient() (*c, error) {
var client, err = storage.NewClient(
context.TODO(),
option.WithCredentialsJSON([]byte(configs.Config.GCLOUD_CREDENTIAL)),
option.WithCredentialsFile(configs.Config.GCLOUD_CREDENTIAL),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
Expand Down

0 comments on commit b0d06b4

Please sign in to comment.