mirror of
https://github.com/luscis/openlan.git
synced 2025-10-08 18:10:22 +08:00
clone from danieldin95
This commit is contained in:
42
pkg/api/link.go
Executable file
42
pkg/api/link.go
Executable file
@@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/luscis/openlan/pkg/cache"
|
||||
"github.com/luscis/openlan/pkg/libol"
|
||||
"github.com/luscis/openlan/pkg/models"
|
||||
"github.com/luscis/openlan/pkg/schema"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
Switcher Switcher
|
||||
}
|
||||
|
||||
func (h Link) Router(router *mux.Router) {
|
||||
router.HandleFunc("/api/link", h.List).Methods("GET")
|
||||
router.HandleFunc("/api/link/{id}", h.Get).Methods("GET")
|
||||
}
|
||||
|
||||
func (h Link) List(w http.ResponseWriter, r *http.Request) {
|
||||
links := make([]schema.Link, 0, 1024)
|
||||
for l := range cache.Link.List() {
|
||||
if l == nil {
|
||||
break
|
||||
}
|
||||
links = append(links, models.NewLinkSchema(l))
|
||||
}
|
||||
ResponseJson(w, links)
|
||||
}
|
||||
|
||||
func (h Link) Get(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
libol.Info("GetPoint %s", vars["id"])
|
||||
|
||||
link := cache.Link.Get(vars["id"])
|
||||
if link != nil {
|
||||
ResponseJson(w, models.NewLinkSchema(link))
|
||||
} else {
|
||||
http.Error(w, vars["id"], http.StatusNotFound)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user