diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 04765c0..aa3b879 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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: [] diff --git a/configs/config.go b/configs/config.go index d9acef9..6b20ae7 100644 --- a/configs/config.go +++ b/configs/config.go @@ -10,9 +10,9 @@ import ( var ( AuthorizationHeader = "Authorization" + defaultDotEnvKey = "DOTENV_FILE" Config *ConfigType origConfig ConfigType - secretVersion int64 = 0 ) type ConfigType struct { @@ -51,7 +51,7 @@ func init() { defer fp.Close() cfg := ConfigType{} - envFile := os.Getenv("DOTENV_FILE") + envFile := os.Getenv(defaultDotEnvKey) if envFile == "" { envFile = ".env" } diff --git a/internal/app/controllers/binance/controller.go b/internal/app/controllers/binance/controller.go index 20ada74..747f2b3 100644 --- a/internal/app/controllers/binance/controller.go +++ b/internal/app/controllers/binance/controller.go @@ -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) } @@ -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) } diff --git a/internal/app/controllers/line/webhook.go b/internal/app/controllers/line/webhook.go index 6d06e70..887562d 100644 --- a/internal/app/controllers/line/webhook.go +++ b/internal/app/controllers/line/webhook.go @@ -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) { diff --git a/internal/app/controllers/urls/controller.go b/internal/app/controllers/urls/controller.go index 93c54da..df1d8f3 100644 --- a/internal/app/controllers/urls/controller.go +++ b/internal/app/controllers/urls/controller.go @@ -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" @@ -72,5 +73,5 @@ func Delete(c *fiber.Ctx) error { if err != nil { return err } - return nil + return c.SendStatus(http.StatusOK) } diff --git a/internal/pkg/gstorage/gstorage.go b/internal/pkg/gstorage/gstorage.go index a7663ce..2863ba1 100644 --- a/internal/pkg/gstorage/gstorage.go +++ b/internal/pkg/gstorage/gstorage.go @@ -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)