@@ -59,6 +59,83 @@ func configHandler(configPath string) http.HandlerFunc {
5959 }
6060}
6161
62+ // updateConfigHandler updates the config.yaml file
63+ func updateConfigHandler (configPath string ) http.HandlerFunc {
64+ return func (w http.ResponseWriter , r * http.Request ) {
65+ // Only allow POST/PUT requests
66+ if r .Method != http .MethodPost && r .Method != http .MethodPut {
67+ http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
68+ return
69+ }
70+
71+ // Read the request body
72+ var configData map [string ]interface {}
73+ if err := json .NewDecoder (r .Body ).Decode (& configData ); err != nil {
74+ log .Printf ("Error decoding request body: %v" , err )
75+ http .Error (w , fmt .Sprintf ("Invalid request body: %v" , err ), http .StatusBadRequest )
76+ return
77+ }
78+
79+ // Convert to YAML
80+ yamlData , err := yaml .Marshal (configData )
81+ if err != nil {
82+ log .Printf ("Error marshaling config to YAML: %v" , err )
83+ http .Error (w , fmt .Sprintf ("Failed to convert config to YAML: %v" , err ), http .StatusInternalServerError )
84+ return
85+ }
86+
87+ // Write to file
88+ if err := os .WriteFile (configPath , yamlData , 0644 ); err != nil {
89+ log .Printf ("Error writing config file: %v" , err )
90+ http .Error (w , fmt .Sprintf ("Failed to write config file: %v" , err ), http .StatusInternalServerError )
91+ return
92+ }
93+
94+ log .Printf ("Configuration updated successfully" )
95+ w .Header ().Set ("Content-Type" , "application/json" )
96+ w .WriteHeader (http .StatusOK )
97+ json .NewEncoder (w ).Encode (map [string ]string {"status" : "success" , "message" : "Configuration updated successfully" })
98+ }
99+ }
100+
101+ // toolsDBHandler reads and serves the tools_db.json file
102+ func toolsDBHandler (configDir string ) http.HandlerFunc {
103+ return func (w http.ResponseWriter , r * http.Request ) {
104+ // Only allow GET requests
105+ if r .Method != http .MethodGet {
106+ http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
107+ return
108+ }
109+
110+ // Construct the tools_db.json path
111+ toolsDBPath := filepath .Join (configDir , "tools_db.json" )
112+
113+ // Read the tools database file
114+ data , err := os .ReadFile (toolsDBPath )
115+ if err != nil {
116+ log .Printf ("Error reading tools_db.json: %v" , err )
117+ http .Error (w , fmt .Sprintf ("Failed to read tools database: %v" , err ), http .StatusInternalServerError )
118+ return
119+ }
120+
121+ // Parse JSON to validate it
122+ var tools interface {}
123+ if err := json .Unmarshal (data , & tools ); err != nil {
124+ log .Printf ("Error parsing tools_db.json: %v" , err )
125+ http .Error (w , fmt .Sprintf ("Failed to parse tools database: %v" , err ), http .StatusInternalServerError )
126+ return
127+ }
128+
129+ // Send response
130+ w .Header ().Set ("Content-Type" , "application/json" )
131+ if err := json .NewEncoder (w ).Encode (tools ); err != nil {
132+ log .Printf ("Error encoding tools to JSON: %v" , err )
133+ http .Error (w , fmt .Sprintf ("Failed to encode tools: %v" , err ), http .StatusInternalServerError )
134+ return
135+ }
136+ }
137+ }
138+
62139// newReverseProxy creates a reverse proxy to targetBase and strips the given prefix from the incoming path
63140func newReverseProxy (targetBase , stripPrefix string , forwardAuth bool ) (* httputil.ReverseProxy , error ) {
64141 targetURL , err := url .Parse (targetBase )
@@ -213,6 +290,15 @@ func main() {
213290 mux .HandleFunc ("/api/router/config/all" , configHandler (absConfigPath ))
214291 log .Printf ("Config API endpoint registered: /api/router/config/all" )
215292
293+ // Config update endpoint - update the config.yaml file
294+ mux .HandleFunc ("/api/router/config/update" , updateConfigHandler (absConfigPath ))
295+ log .Printf ("Config update API endpoint registered: /api/router/config/update" )
296+
297+ // Tools DB endpoint - serve the tools_db.json
298+ configDir := filepath .Dir (absConfigPath )
299+ mux .HandleFunc ("/api/tools-db" , toolsDBHandler (configDir ))
300+ log .Printf ("Tools DB API endpoint registered: /api/tools-db" )
301+
216302 // Router API proxy (forward Authorization) - MUST be registered before Grafana
217303 var routerAPIProxy * httputil.ReverseProxy
218304 if * routerAPI != "" {
0 commit comments