2022-01-15 21:45:37 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
2022-01-17 05:11:24 +00:00
|
|
|
"gorm.io/gorm/clause"
|
2022-01-15 21:45:37 +00:00
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetNodeConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// validate login
|
2022-01-17 21:42:17 +00:00
|
|
|
if !AdminLogin(r) {
|
2022-01-15 21:45:37 +00:00
|
|
|
log.Printf("SetNodeConfig - invalid admin credentials");
|
|
|
|
w.WriteHeader(http.StatusUnauthorized);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse node config
|
2022-01-17 21:42:17 +00:00
|
|
|
var config NodeConfig
|
|
|
|
if ParseRequest(r, w, &config) != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// store credentials
|
|
|
|
err := store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-01-17 05:11:24 +00:00
|
|
|
|
|
|
|
// upsert domain config
|
|
|
|
if res := tx.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "config_id"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"str_value"}),
|
|
|
|
}).Create(&store.Config{ConfigId: CONFIG_DOMAIN, StrValue: config.Domain}).Error; res != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-01-17 05:11:24 +00:00
|
|
|
|
|
|
|
// upsert public limit config
|
|
|
|
if res := tx.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "config_id"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"num_value"}),
|
|
|
|
}).Create(&store.Config{ConfigId: CONFIG_PUBLICLIMIT, NumValue: config.PublicLimit}).Error; res != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-01-17 05:11:24 +00:00
|
|
|
|
|
|
|
// upsert account storage config
|
|
|
|
if res := tx.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "config_id"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"num_value"}),
|
|
|
|
}).Create(&store.Config{ConfigId: CONFIG_STORAGE, NumValue: config.AccountStorage}).Error; res != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-01-17 05:11:24 +00:00
|
|
|
|
2022-01-15 21:45:37 +00:00
|
|
|
return nil;
|
|
|
|
})
|
|
|
|
if(err != nil) {
|
|
|
|
log.Printf("SetNodeConfig - failed to store config");
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|