Skip to content

Commit 9d637db

Browse files
authored
Merge pull request #16 from Acture/feat/mipush-env-handling
feat(mipush): handle missing key by environment mode & update README run instructions
2 parents 66eea3a + ac62081 commit 9d637db

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This is a notification microservice.
44

55
## Features
6-
- support apns and mipush notifications
6+
- support apns and mipush notifications
77
- REST API to manage notification and user device tokens
88

99
## Usage
@@ -20,12 +20,22 @@ go build -o notification.exe
2020
./notification.exe
2121
```
2222

23+
### Run
24+
Before running, export `MODE` and `BASE_PATH`:
25+
26+
```bash
27+
export BASE_PATH=$PWD
28+
# Available modes: production / dev / test / perf
29+
export MODE=dev
30+
go run main.go
31+
```
32+
2333
### Test
2434
Please export `MODE=test` and `BASE_PATH=$PWD`
2535
to avoid relative path errors in unit tests.
2636

27-
Device tokens must be set to test push notifications,
28-
export `${service}_DEVICE_TOKEN` for each push service,
37+
Device tokens must be set to test push notifications,
38+
export `${service}_DEVICE_TOKEN` for each push service,
2939
e.g. `APNS_DEVICE_TOKEN=1234567`
3040

3141
### API Docs
@@ -51,7 +61,7 @@ Please visit http://localhost:8000/docs after running app
5161

5262
Feel free to dive in! [Open an issue](https://github.com/OpenTreeHole/notification/issues/new) or [Submit PRs](https://github.com/OpenTreeHole/notification/compare).
5363

54-
We are now in rapid development, any contribution would be of great help.
64+
We are now in rapid development, any contribution would be of great help.
5565
For the developing roadmap, please visit [this issue](https://github.com/OpenTreeHole/notification/issues/1).
5666

5767
### Contributors

push/mipush/init.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,53 @@ var client = http.Client{Timeout: timeout}
1717
var authorization string
1818

1919
func init() {
20+
2021
log.Debug().Msg("init mipush")
21-
authorization = "key=" + getMipushKey()
22+
key, _ := getMipushKey()
23+
authorization = "key=" + key
2224
}
2325

24-
func getMipushKey() string {
26+
func getMipushKey() (string, error) {
2527
data, err := os.ReadFile(config.Config.MipushKeyPath)
28+
2629
if err != nil {
2730
pwd, _ := os.Getwd()
28-
log.Fatal().Str("pwd", pwd).Err(err).Str("scope", "init mipush").Msg("failed to read mipush key")
31+
32+
switch config.Config.Mode {
33+
case "production":
34+
// 在生产环境中严格要求 key 存在
35+
log.Fatal().
36+
Str("pwd", pwd).
37+
Err(err).
38+
Str("scope", "init mipush").
39+
Msg("failed to read mipush key in production")
40+
// 不会执行到这里
41+
return "", err
42+
43+
case "dev", "test":
44+
// 在开发或测试环境中只警告,不终止
45+
log.Warn().
46+
Str("pwd", pwd).
47+
Err(err).
48+
Str("scope", "init mipush").
49+
Msg("failed to read mipush key, using empty key in non-production mode")
50+
return "", nil
51+
52+
case "perf":
53+
// 压测环境用 mock key
54+
log.Info().
55+
Str("scope", "init mipush").
56+
Msg("using mock mipush key for perf mode")
57+
return "mock-mipush-key", nil
58+
59+
default:
60+
// 未知模式时明确报错退出
61+
log.Fatal().
62+
Str("scope", "init mipush").
63+
Str("mode", config.Config.Mode).
64+
Msg("unknown mode while reading mipush key")
65+
return "", err
66+
}
2967
}
30-
return string(data)
68+
return string(data), nil
3169
}

0 commit comments

Comments
 (0)