| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package api
- import (
- "encoding/json"
- "net/http"
- "strings"
- "debtik/internal/reverseproxy"
- )
- func (s *Server) handleGetProxyConfig(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- cfg := s.proxyMgr.GetConfig()
- writeJSON(w, http.StatusOK, cfg)
- }
- func (s *Server) handleUpdateProxyConfig(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- var req reverseproxy.Config
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request payload")
- return
- }
- if err := s.proxyMgr.UpdateConfig(req); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{
- "success": true,
- "message": "Reverse proxy configuration updated",
- })
- }
- func (s *Server) handleListProxyRules(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- rules := s.proxyMgr.ListRules()
- writeJSON(w, http.StatusOK, rules)
- }
- func (s *Server) handleGetProxyRule(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- id := r.PathValue("id")
- rule, err := s.proxyMgr.GetRule(id)
- if err != nil {
- writeError(w, http.StatusNotFound, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, rule)
- }
- func (s *Server) handleSaveProxyRule(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- var rule reverseproxy.Rule
- if err := json.NewDecoder(r.Body).Decode(&rule); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request payload")
- return
- }
- id := r.PathValue("id")
- if id != "" {
- rule.ID = id
- }
- saved, err := s.proxyMgr.SaveRule(&rule)
- if err != nil {
- writeError(w, http.StatusBadRequest, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, saved)
- }
- func (s *Server) handleDeleteProxyRule(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- id := r.PathValue("id")
- if err := s.proxyMgr.DeleteRule(id); err != nil {
- writeError(w, http.StatusNotFound, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{
- "success": true,
- "message": "Rule deleted successfully",
- })
- }
- func (s *Server) handleSetProxyRuleState(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- id := r.PathValue("id")
- var req struct {
- Enabled bool `json:"enabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request payload")
- return
- }
- if err := s.proxyMgr.SetRuleState(id, req.Enabled); err != nil {
- writeError(w, http.StatusBadRequest, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{
- "success": true,
- "enabled": req.Enabled,
- })
- }
- func (s *Server) handleGetProxyStatus(w http.ResponseWriter, r *http.Request) {
- if s.proxyMgr == nil {
- writeError(w, http.StatusServiceUnavailable, "reverse proxy manager not configured")
- return
- }
- cfg := s.proxyMgr.GetConfig()
- var totalRequests uint64
- var activeCerts int
- certSet := make(map[string]bool)
- for _, rule := range cfg.Rules {
- totalRequests += rule.Stats.Requests
- if rule.Enabled && rule.Certificate != "" {
- certSet[strings.ToLower(rule.Certificate)] = true
- }
- }
- activeCerts = len(certSet)
- writeJSON(w, http.StatusOK, map[string]any{
- "enabled": cfg.Enabled,
- "http_port": cfg.HTTPPort,
- "https_port": cfg.HTTPSPort,
- "rules_count": len(cfg.Rules),
- "total_requests": totalRequests,
- "active_certs": activeCerts,
- "default_cert": cfg.DefaultCert,
- })
- }
|