Skip to content

Commit

Permalink
enable custom endpoint for fcmv1
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Apr 5, 2024
1 parent 0767f51 commit 9527836
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type SectionFCMv1 struct {
Enabled bool
ProjectID string
TokenSource oauth2.TokenSource
Endpoint string
}

// DefaultLoadConfig loads default /etc/gunfish.toml
Expand Down
19 changes: 9 additions & 10 deletions fcmv1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *Client) NewRequest(p Payload) (*http.Request, error) {
}

// NewClient establishes a http connection with fcm v1
func NewClient(tokenSource oauth2.TokenSource, projectID string, endpoint *url.URL, timeout time.Duration) (*Client, error) {
func NewClient(tokenSource oauth2.TokenSource, projectID string, endpoint string, timeout time.Duration) (*Client, error) {
client := &http.Client{
Timeout: timeout,
}
Expand All @@ -94,16 +94,15 @@ func NewClient(tokenSource oauth2.TokenSource, projectID string, endpoint *url.U
tokenSource: tokenSource,
}

if endpoint != nil {
c.endpoint = endpoint
} else {
ep, err := url.Parse(DefaultFCMEndpoint)
if err != nil {
return nil, err
}
ep.Path = path.Join(ep.Path, projectID, "messages:send")
c.endpoint = ep
if endpoint == "" {
endpoint = DefaultFCMEndpoint
}
ep, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
ep.Path = path.Join(ep.Path, projectID, "messages:send")
c.endpoint = ep

return c, nil
}
2 changes: 1 addition & 1 deletion supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func StartSupervisor(conf *config.Config) (Supervisor, error) {
return Supervisor{}, errors.New("FCM legacy is not supported")
}
if conf.FCMv1.Enabled {
fcv1, err = fcmv1.NewClient(conf.FCMv1.TokenSource, conf.FCMv1.ProjectID, nil, fcmv1.ClientTimeout)
fcv1, err = fcmv1.NewClient(conf.FCMv1.TokenSource, conf.FCMv1.ProjectID, conf.FCMv1.Endpoint, fcmv1.ClientTimeout)
if err != nil {
LogWithFields(logrus.Fields{
"type": "supervisor",
Expand Down
6 changes: 6 additions & 0 deletions test/gunfish_test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ cert_file = "{{ env `PROJECT_ROOT` `.` }}/test/server.crt"
key_file = "{{ env `PROJECT_ROOT` `.` }}/test/server.key"
request_per_sec = 2000
sender_num = 50

[fcm_v1]
# google_application_credentials = "{{ env `PROJECT_ROOT` `.` }}/credentials.json"
enabled = true
endpoint = "http://localhost:8888/v1/projects"
projectid = "test"

0 comments on commit 9527836

Please sign in to comment.