mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-07-23 12:51:40 +02:00
Implementation of resolvers for new GraphQL schema
This commit is contained in:
64
config/config.go
Normal file
64
config/config.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var lock sync.RWMutex
|
||||
var config map[string]interface{}
|
||||
|
||||
const configFilePath string = "./var/ui.config.json"
|
||||
|
||||
func init() {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
bytes, err := os.ReadFile(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(bytes, &config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Call this function to change the current configuration.
|
||||
// `value` must be valid JSON. This This function is thread-safe.
|
||||
func UpdateConfig(key, value string, ctx context.Context) error {
|
||||
var v interface{}
|
||||
if err := json.Unmarshal([]byte(value), &v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
config[key] = v
|
||||
bytes, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(configFilePath, bytes, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// http.HandlerFunc compatible function that serves the current configuration as JSON
|
||||
func ServeConfig(rw http.ResponseWriter, r *http.Request) {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
|
||||
rw.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(rw).Encode(config); err != nil {
|
||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user