peermap/api: admin -> api

This commit is contained in:
rkonfj
2025-03-20 20:52:20 +08:00
parent 3aab28a3fb
commit e9596124a3
5 changed files with 18 additions and 18 deletions
+2 -2
View File
@@ -14,7 +14,7 @@ import (
"syscall"
"github.com/sigcn/pg/peermap"
"github.com/sigcn/pg/peermap/admin"
"github.com/sigcn/pg/peermap/api"
"github.com/sigcn/pg/peermap/config"
)
@@ -102,7 +102,7 @@ func run(commandConfig config.Config, configPath string) error {
cfg, _ := config.ReadConfig(configPath)
cfg.Overwrite(commandConfig)
admin.Version = Version
api.Version = Version
srv, err := peermap.New(cfg)
if err != nil {
return err
@@ -1,4 +1,4 @@
package admin
package api
import (
"context"
@@ -12,7 +12,7 @@ import (
"time"
"github.com/sigcn/pg/langs"
"github.com/sigcn/pg/peermap/admin/types"
"github.com/sigcn/pg/peermap/api/types"
"github.com/sigcn/pg/peermap/auth"
"github.com/sigcn/pg/peermap/config"
"github.com/sigcn/pg/peermap/oidc"
@@ -25,7 +25,7 @@ var (
type contextKey string
type AdministratorV1 struct {
type ApiV1 struct {
Config config.Config
Auth *auth.Authenticator
PeerStore types.PeerStore
@@ -35,15 +35,15 @@ type AdministratorV1 struct {
initOnce sync.Once
}
func (a *AdministratorV1) init() {
func (a *ApiV1) init() {
a.initOnce.Do(func() {
a.mux.HandleFunc("GET /pg/apis/v1/admin/peers", a.handleQueryPeers)
a.mux.HandleFunc("GET /pg/apis/v1/admin/psns.json", a.handleDownloadSecret)
a.mux.HandleFunc("GET /pg/apis/v1/admin/server_info", a.handleQueryServerInfo)
a.mux.HandleFunc("GET /pg/api/v1/peers", a.handleQueryPeers)
a.mux.HandleFunc("GET /pg/api/v1/psns.json", a.handleDownloadSecret)
a.mux.HandleFunc("GET /pg/api/v1/server_info", a.handleQueryServerInfo)
})
}
func (a *AdministratorV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (a *ApiV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.init()
token := r.Header.Get("X-Token")
secret, err := a.Auth.ParseSecret(token)
@@ -65,7 +65,7 @@ func (a *AdministratorV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.mux.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), contextKey("secret"), secret)))
}
func (a *AdministratorV1) handleQueryPeers(w http.ResponseWriter, r *http.Request) {
func (a *ApiV1) handleQueryPeers(w http.ResponseWriter, r *http.Request) {
peers, err := a.PeerStore.Peers(r.Context().Value(contextKey("secret")).(auth.JSONSecret).Network)
if err != nil {
langs.Err(err).MarshalTo(w)
@@ -74,7 +74,7 @@ func (a *AdministratorV1) handleQueryPeers(w http.ResponseWriter, r *http.Reques
langs.Data[any]{Data: peers}.MarshalTo(w)
}
func (a *AdministratorV1) handleDownloadSecret(w http.ResponseWriter, r *http.Request) {
func (a *ApiV1) handleDownloadSecret(w http.ResponseWriter, r *http.Request) {
secret := r.Context().Value(contextKey("secret")).(auth.JSONSecret)
secretJSON, err := a.Grant(secret.Network, "")
if err != nil {
@@ -89,7 +89,7 @@ func (a *AdministratorV1) handleDownloadSecret(w http.ResponseWriter, r *http.Re
slog.Info("Generate a secret", "network", secret.Network)
}
func (a *AdministratorV1) handleQueryServerInfo(w http.ResponseWriter, r *http.Request) {
func (a *ApiV1) handleQueryServerInfo(w http.ResponseWriter, r *http.Request) {
info, err := readBuildInfo()
if err != nil {
langs.Err(err).MarshalTo(w)
+2 -2
View File
@@ -19,7 +19,7 @@ import (
"github.com/gorilla/websocket"
"github.com/sigcn/pg/disco"
"github.com/sigcn/pg/langs"
"github.com/sigcn/pg/peermap/admin"
"github.com/sigcn/pg/peermap/api"
"github.com/sigcn/pg/peermap/auth"
"github.com/sigcn/pg/peermap/config"
"github.com/sigcn/pg/peermap/exporter"
@@ -788,7 +788,7 @@ func New(cfg config.Config) (*PeerMap, error) {
mux := http.NewServeMux()
pm.httpServer = &http.Server{Handler: mux, Addr: cfg.Listen}
mux.Handle("/pg/apis/v1/admin/", &admin.AdministratorV1{Config: cfg, Auth: pm.authenticator, Grant: pm.Grant, PeerStore: &pm})
mux.Handle("/pg/api/v1/", &api.ApiV1{Config: cfg, Auth: pm.authenticator, Grant: pm.Grant, PeerStore: &pm})
mux.HandleFunc("/", ui.HandleStaticFiles)
mux.HandleFunc("GET /pg", pm.HandlePeerPacketConnect)
mux.HandleFunc("GET /pg/networks", pm.HandleQueryNetworks)
+3 -3
View File
@@ -7,7 +7,7 @@ const peers = ref([])
const serverInfo = ref()
const loadPeers = async () => {
let r = await http.get('/pg/apis/v1/admin/peers', { session: session.value })
let r = await http.get('/pg/api/v1/peers', { session: session.value })
if (r.code != 0) {
alert(r.msg)
return
@@ -21,7 +21,7 @@ const loadPeers = async () => {
}
const loadServerInfo = async () => {
let r = await http.get('/pg/apis/v1/admin/server_info', { session: session.value })
let r = await http.get('/pg/api/v1/server_info', { session: session.value })
if (r.code != 0) {
alert(r.msg)
return
@@ -36,7 +36,7 @@ const signout = () => {
}
const downloadSecret = async () => {
let r = await http.download('/pg/apis/v1/admin/psns.json', { session: session.value })
let r = await http.download('/pg/api/v1/psns.json', { session: session.value })
if (r.code != 0) {
alert(r.msg)
}