Files
dobotai-platform/internal/adminweb/server.go
T
2026-08-13 12:45:08 +08:00

83 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package adminweb
import (
"context"
"crypto/subtle"
"fmt"
"html/template"
"net/http"
"strings"
"time"
)
type Snapshot struct {
SystemState, ClientIDMasked string
SecretConfigured bool
Components []Component
}
type Component struct{ Name, State, Detail string }
type Verification struct {
OK bool
Code string
CheckedAt time.Time
}
type Service interface {
Snapshot(context.Context) (Snapshot, error)
SaveCredentials(context.Context, string, string) error
VerifyCredentials(context.Context, string, string) Verification
}
type server struct {
service Service
token string
mux *http.ServeMux
page *template.Template
}
func New(service Service, token string) http.Handler {
page := template.Must(template.New("settings").Parse(`<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>DobotAI 设置</title><style>body{font-family:sans-serif;background:#f5f8ff;color:#17324d;max-width:960px;margin:40px auto;padding:0 20px}.card{background:white;border:1px solid #dbe7f4;border-radius:14px;padding:20px;margin:14px 0}.ok{color:#087f5b}input{padding:10px;width:95%;margin:6px 0}button{padding:10px 18px;background:#1677ff;color:white;border:0;border-radius:8px}</style></head><body><h1>DobotAI 配置中心</h1><div class="card"><b>系统状态:</b>{{.SystemState}}<br><b>Client ID:</b>{{.ClientIDMasked}}<br><b>Client Secret:</b>{{if .SecretConfigured}}已配置(不可查看){{else}}未配置{{end}}</div><div class="card"><h2>配置钉钉应用</h2><form method="post" action="/admin/setup/credentials"><label>Client ID / AppKey</label><input name="client_id" required><label>Client Secret / AppSecret</label><input type="password" name="client_secret" required autocomplete="new-password"><button>测试并保存</button></form></div>{{range .Components}}<div class="card"><b>{{.Name}}</b>:{{.State}}<br>{{.Detail}}</div>{{end}}</body></html>`))
s := &server{service: service, token: strings.TrimSpace(token), mux: http.NewServeMux(), page: page}
s.mux.HandleFunc("GET /admin/settings", s.settings)
s.mux.HandleFunc("GET /admin/setup", s.settings)
s.mux.HandleFunc("POST /admin/setup/credentials", s.saveCredentials)
return s
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if !s.authorized(r) {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
s.mux.ServeHTTP(w, r)
}
func (s *server) authorized(r *http.Request) bool {
v := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
return s.token != "" && len(v) == len(s.token) && subtle.ConstantTimeCompare([]byte(v), []byte(s.token)) == 1
}
func (s *server) settings(w http.ResponseWriter, r *http.Request) {
snap, err := s.service.Snapshot(r.Context())
if err != nil {
http.Error(w, "settings unavailable", 500)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_ = s.page.Execute(w, snap)
}
func (s *server) saveCredentials(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, 16<<10)
if err := r.ParseForm(); err != nil {
http.Error(w, "invalid form", 400)
return
}
id, secret := strings.TrimSpace(r.FormValue("client_id")), strings.TrimSpace(r.FormValue("client_secret"))
result := s.service.VerifyCredentials(r.Context(), id, secret)
if !result.OK {
http.Error(w, fmt.Sprintf("credential verification failed: %s", result.Code), 422)
return
}
if err := s.service.SaveCredentials(r.Context(), id, secret); err != nil {
http.Error(w, "credential save failed", 500)
return
}
http.Redirect(w, r, "/admin/settings", http.StatusSeeOther)
}