You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
995 B
47 lines
995 B
2 months ago
|
package dbHelper
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type ApiServerHelper struct {
|
||
|
mux *http.ServeMux
|
||
|
route map[string]string
|
||
|
port uint
|
||
|
}
|
||
|
|
||
|
func NewApiServer(port uint, routes map[string]string) *ApiServerHelper {
|
||
|
return &ApiServerHelper{
|
||
|
mux: http.NewServeMux(),
|
||
|
route: routes,
|
||
|
port: port,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (the *ApiServerHelper) Initial() {
|
||
|
the.mux = http.NewServeMux()
|
||
|
// 创建 HTTP 服务器
|
||
|
ser := http.Server{
|
||
|
Handler: the.mux,
|
||
|
Addr: fmt.Sprintf(":%d", the.port),
|
||
|
}
|
||
|
log.Printf("apiServer监听端口 %d", the.port)
|
||
|
go log.Fatal(ser.ListenAndServe())
|
||
|
}
|
||
|
|
||
|
func (the *ApiServerHelper) routeRegister() {
|
||
|
the.mux.HandleFunc("GET /nodeList", func(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
s := `{"a":1}`
|
||
|
fmt.Fprintf(w, s)
|
||
|
})
|
||
|
|
||
|
the.mux.HandleFunc("GET /namespaceList", func(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
s := `{"b":1}`
|
||
|
fmt.Fprintf(w, s)
|
||
|
})
|
||
|
}
|