mirror of
https://github.com/luscis/openlan.git
synced 2025-10-12 20:10:08 +08:00
clone from danieldin95
This commit is contained in:
47
pkg/api/server.go
Executable file
47
pkg/api/server.go
Executable file
@@ -0,0 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Switcher Switcher
|
||||
}
|
||||
|
||||
func (l Server) Router(router *mux.Router) {
|
||||
router.HandleFunc("/api/server", l.List).Methods("GET")
|
||||
router.HandleFunc("/api/server/{id}", l.List).Methods("GET")
|
||||
}
|
||||
|
||||
func (l Server) List(w http.ResponseWriter, r *http.Request) {
|
||||
server := l.Switcher.Server()
|
||||
data := &struct {
|
||||
UpTime int64 `json:"uptime"`
|
||||
Total int `json:"total"`
|
||||
Statistic map[string]int64 `json:"statistic"`
|
||||
Connection []interface{} `json:"connection"`
|
||||
}{
|
||||
UpTime: l.Switcher.UpTime(),
|
||||
Statistic: server.Statistics(),
|
||||
Connection: make([]interface{}, 0, 1024),
|
||||
Total: server.TotalClient(),
|
||||
}
|
||||
for u := range server.ListClient() {
|
||||
if u == nil {
|
||||
break
|
||||
}
|
||||
data.Connection = append(data.Connection, &struct {
|
||||
UpTime int64 `json:"uptime"`
|
||||
LocalAddr string `json:"localAddr"`
|
||||
RemoteAddr string `json:"remoteAddr"`
|
||||
Statistic map[string]int64 `json:"statistic"`
|
||||
}{
|
||||
UpTime: u.UpTime(),
|
||||
LocalAddr: u.LocalAddr(),
|
||||
RemoteAddr: u.RemoteAddr(),
|
||||
Statistic: u.Statistics(),
|
||||
})
|
||||
}
|
||||
ResponseJson(w, data)
|
||||
}
|
Reference in New Issue
Block a user