http - 同时使用 HTTP 和 RPC
问题描述
我正在尝试在我的系统中实现rpc
和http
服务器。所以要同时监听服务器我已经运行了 2goroutines
这是一个代码
func main() {
// Recovering all errors during the process
defer errorHandler()
wg.Add(2)
go RPCConnect()
fmt.Println("Listening for RPC 127.0.0.1:" + config.rpcPort)
go HTTPConnect()
fmt.Println("Listening for HTTP 127.0.0.1:" + config.httpPort)
wg.Wait()
}
func RPCConnect() {
err := rpc.Register(pool)
if err != nil {
panic(err)
}
rpc.HandleHTTP()
listener, e := net.Listen("tcp", ":"+config.rpcPort)
if e != nil {
panic(e)
}
err = http.Serve(listener, nil)
if err != nil {
panic(err)
}
}
func HTTPConnect() {
var httpPool HTTPPool
r := mux.NewRouter()
r.HandleFunc("/create", httpPool.Create).Methods("POST")
r.HandleFunc("/generate", httpPool.Generate).Methods("POST")
r.HandleFunc("/list", httpPool.List).Methods("GET")
r.HandleFunc("/delete", httpPool.Delete).Methods("POST")
err := http.ListenAndServe("localhost:"+config.httpPort, r)
if err != nil {
panic(err)
}
}
我不知道这是不是最好的方法。有人可以告诉我更简单灵活的方法吗?
如果问题不相关,我提前道歉
解决方案
推荐阅读
- c - 如果双向链表是奇数,则删除中间和最后一个节点
- reactjs - 反应惰性加载器需要太长时间才能渲染
- node.js - 从节点包扩展类型
- android - 从布局中获取位图会使布局不完整
- javascript - 为什么当我在javascript中动态添加新元素时禁用滚动
- pixi.js - pixjs:显示和隐藏包含所有元素的容器
- amazon-s3 - 通过 s3fs 从 S3 读取文件时可以使用 xr.open_mfdataset 吗?
- django - 为什么这个 django formset 突然停止保存内容?
- c# - 即使包含“使用 System.Configuration”,也无法使用 ConfigurationManager.ConnectionStrings[...]
- jquery - Jquery DatePicker 设置最小和最大日期