Files
openlan/pkg/api/server.go
Daniel Ding caef90cc04
Some checks failed
Coverage CI / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Ubuntu CI / build (push) Has been cancelled
fea: switch: do dnat.
2025-10-22 14:03:19 +08:00

49 lines
1.2 KiB
Go
Executable File

package api
import (
"net/http"
"github.com/gorilla/mux"
)
type Server struct {
cs SwitchApi
}
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.cs.Server()
data := &struct {
UpTime int64 `json:"uptime"`
Total int `json:"total"`
Statistic map[string]int64 `json:"statistic"`
Connection []interface{} `json:"connection"`
}{
UpTime: l.cs.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)
}