dongzong1866 2019-03-05 13:33
浏览 82

Golang-如何创建回调通知

I am trying to implement a case where a client sends a POST request to subscribe to some service to a server.

The server has to respond with the subscription data, however after some time if there is a change in the subscription information in the server, the server has to send a notification to the client about the changes using the "nfStatusNotificationUri" in the request body ("nfStatusNotificationUri" in the JSON data below).

I do not know how to do this.

I have implemented the POST subscription part but have no idea how to implement the notification part.

Can anyone help me or give me some guidance onenter code here how to do this.

This what I have done so far:

// server // functions

func (m *NfInstanceDataAccess) Insertsub(nfinstancesub Subscriptions) error {
    err := db.C(COLLECTION).Insert(&nfinstancesub)
    return err
}

func CreateNewSubscriptionPost(w http.ResponseWriter, r *http.Request) {
    var nfinstancesub Subscriptions
    id := uuid.New()
    subscriptionID := id.String()

    if r.Header.Get("Accept") != "application/json" {
        WriteError(w, ErrNotAcceptable)
        return
    }
    if err := json.NewDecoder(r.Body).Decode(&nfinstancesub); err != nil {
        respondWithError(w, http.StatusBadRequest, "Invalid request payload")
        return
    }
    nfinstancesub.ID = bson.NewObjectId()
    nfinstancesub.SubscriptionID = subscriptionID
    if err := da.Insertsub(nfinstancesub); err != nil {
        respondWithError(w, http.StatusInternalServerError, err.Error())
        return
    }
    w.Header().Set("Response-Code", "201")
    w.Header().Set("Response-Desc", "Success")
    w.Header().Set("Cache-Control", "max-age=2592000") // 30 days
    respondWithJson(w, http.StatusCreated, nfinstancesub)
}

// Main function

func main() {
    http.HandleFunc("/nnrf-nfm/v1/subscriptions, CreateNewSubscriptionPost)

    log.Fatal(http.ListenAndServe(":8080", nil))
}

The JSON data to request subscription including the notification uri "nfStatusNotificationUri" field. Am using mongodb to store this JSON data request is sent.

{
  "nfStatusNotificationUri": "string",
  "subscriptionID": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
  "validityTime": "2019-02-11T09:45:52.015Z",
  "reqNotifEvents": [
    "NF_REGISTERED",
    "string"
  ],
  "plmnId": {
    "mcc": "string",
    "mnc": "string"
  },
  "notifCondition": {
    "monitoredAttributes": [
      "string"
    ],
    "unmonitoredAttributes": [
      "string"
    ]
  },
  "reqNfFqdn": "string"
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题