聊聊storagetapper的server


本文摘自网络,作者,侵删。

本文主要研究一下storagetapper的server

server

storagetapper/server/server.go

var server *http.Server
var mutex = sync.Mutex{}

func init() {
    http.HandleFunc("/health", healthCheck)
    http.HandleFunc("/schema", schemaCmd)
    http.HandleFunc("/cluster", clusterInfoCmd)
    http.HandleFunc("/table", tableCmd)
    http.HandleFunc("/config", configCmd)
    http.HandleFunc("/", indexCmd)
}

//StartHTTPServer starts listening and serving traffic on configured port and sets up http routes.
func StartHTTPServer(port int) {
    state.EmitRegisteredTablesCount()
    mutex.Lock()

    server = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: nil}
    log.Debugf("HTTP server is listening on %v port", port)

    mutex.Unlock()

    err := server.ListenAndServe()
    if err != nil && err != http.ErrServerClosed {
        log.E(err)
    }
}

//Shutdown gracefully stops the server
func Shutdown() {
    mutex.Lock()
    defer mutex.Unlock()
    if server == nil {
        return
    }
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()
    log.E(server.Shutdown(ctx))
    server = nil
}
storagetapper的server提供了StartHTTPServer、Shutdown方法;其init方法注册了/health/schema/cluster/table/config/这几个url

healthCheck

storagetapper/server/server.go

//healthCheck handles call to the health check endpoint
func healthCheck(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Content-Type", "text/plain")
    w.WriteHeader(http.StatusOK)
    if _, err := w.Write([]byte("OK")); err != nil {
        log.Errorf("Health check failed: %s\n", err)
    }
}
healthCheck返回200,文本内容为OK

小结

storagetapper的server提供了StartHTTPServer、Shutdown方法;其init方法注册了/health/schema/cluster/table/config/这几个url。

doc

  • storagetapper

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊storagetapper的server

相关阅读 >>

Go bool

手撸Golang 基本数据结构与算法 堆

Golang 创建型设计模式 建造者模式

Golang iota从几开始

聊聊dapr的fswatcher

手撸Golang 基本数据结构与算法 队列

Golang 可以写游戏吗?

vscode 安装Golang插件

Golang如何防止意外崩溃

Go link

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...