Gorush 入門

Gorush 是一套使用 Gin framework 撰寫的訊息遞送伺服器。支援Android的FCM及蘋果的APNS服務。

特色

  • 支援使用Android的go-gcm函數的 Google Cloud Message(現為Firebase Cloud Messaging
  • 支援使用apns2庫的蘋果推送通知服務
  • 支援 YAML 配置
  • 支援使用命令列發送單個 Android 和 iOS 通知
  • 支援使用 Web API 推送通知
  • 支援使用 facebook grace 進行優雅重啟和零宕機部署
  • 支援 HTTP/2 或 HTTP/1.1 協議
  • 支援通知佇列及多個 worker
  • 使用 /api/stat/app 進行通知成功或失敗計數
  • 使用 /api/config 顯示 YAML 配置
  • 支援將應用程式統計資訊存儲到記憶體,Redis, BoltDB, BuntDB 或LevelDB
  • 支援 p12 或 pem 格式的 iOS 證書檔
  • 使用 /sys/stats 顯示時間及狀態碼計數
  • 支援 HTTP 代理到 Google 伺服器(GCM)
  • 如果伺服器回應失敗,支持重試發送通知。

gorush 運作原理

gorush
圖檔來源:https://blog.wu-boy.com/2017/06/gopher-day-in-taipei/#more-6758

  • 支援自動 TLS 功能
  • IOS 支援p12、pem或pem格式認證

取得gorush

// On linux
$ wget https://github.com/appleboy/gorush/releases/download/v1.9.0/gorush-v1.9.0-linux-amd64 -O gorush

// On OS X
$ wget https://github.com/appleboy/gorush/releases/download/v1.9.0/gorush-v1.9.0-darwin-amd64 -O gorush

// On Windows
$ wget https://github.com/appleboy/gorush/releases/download/v1.9.0/gorush-v1.9.0-windows-amd64.exe -O gorush.exe
  • Docker
docker pull appleboy/gorush
docker run --name gorush -v ${PWD}/config.yml:/config.yml -p 80:8088 appleboy/gorush

編輯 config.yml

## 伺服器設定
core:  
  enabled: true # enabale httpd server
  address: "" # ip address to bind (default: any)
  port: "8088" # ignore this port number if auto_tls is enabled (listen 443).
  worker_num: 0 # default worker number is runtime.NumCPU()
  queue_num: 0 # default queue number is 8192
  max_notification: 100
  sync: false # set true if you need get error message from fail push notification in API response.
  mode: "release"
  ssl: false
  cert_path: "cert.pem"
  key_path: "key.pem"
  http_proxy: "" # only working for FCM server
  pid:
    enabled: false
    path: "gorush.pid"
    override: true
  auto_tls:
    enabled: false # Automatically install TLS certificates from Let's Encrypt.
    folder: ".cache" # folder for storing TLS certificates
    host: "" # which domains the Let's Encrypt will attempt

grpc:
  enabled: false # enabale gRPC server
  port: 50051

api:
  push_uri: "/api/push"
  stat_go_uri: "/api/stat/go"
  stat_app_uri: "/api/stat/app"
  config_uri: "/api/config"
  sys_stat_uri: "/sys/stats"
  metric_uri: "/metrics"

android:
  enabled: true
  apikey: "YOUR_API_KEY"
  max_retry: 0 # resend fail notification, default value zero is disabled

ios:
  enabled: false
  key_path: "key.pem"
  password: "" # certificate password, default as empty string.
  production: false
  max_retry: 0 # resend fail notification, default value zero is disabled

log:
  format: "string" # string or json
  access_log: "stdout" # stdout: output to console, or define log path like "log/access_log"
  access_level: "debug"
  error_log: "stderr" # stderr: output to console, or define log path like "log/error_log"
  error_level: "error"
  hide_token: true

stat:
  engine: "memory" # support memory, redis, boltdb, buntdb or leveldb
  redis:
    addr: "localhost:6379"
    password: ""
    db: 0
  boltdb:
    path: "bolt.db"
    bucket: "gorush"
  buntdb:
    path: "bunt.db"
  leveldb:
    path: "level.db"

執行gorush server

  • 預設port為:8088
gorush -c config.yml
  • Web API
    Gorush support the following API.
GET /api/stat/go 取得 CPU、記憶體等使用量
GET /api/stat/app show notification success and failure counts.
GET /api/config 顯示YAML設定檔
POST /api/push push ios and android notifications.

Client 端

參考資料