2022-01-15 21:45:37 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-01-16 07:25:43 +00:00
|
|
|
"errors"
|
2022-01-15 21:45:37 +00:00
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
2022-02-08 20:24:42 +00:00
|
|
|
func SetNodeStatus(w http.ResponseWriter, r *http.Request) {
|
2022-01-15 21:45:37 +00:00
|
|
|
|
2022-01-16 07:25:43 +00:00
|
|
|
var config store.Config
|
2022-07-22 18:01:29 +00:00
|
|
|
err := store.DB.Where("config_id = ?", CNFConfigured).First(&config).Error
|
2022-01-16 07:25:43 +00:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config.BoolValue {
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-06 22:18:45 +00:00
|
|
|
token := r.FormValue("token")
|
|
|
|
if token == "" {
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-07-22 18:01:29 +00:00
|
|
|
if res := tx.Create(&store.Config{ConfigID: CNFToken, StrValue: token}).Error; res != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-07-22 18:01:29 +00:00
|
|
|
if res := tx.Create(&store.Config{ConfigID: CNFConfigured, BoolValue: true}).Error; res != nil {
|
2022-01-15 21:45:37 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
})
|
|
|
|
if(err != nil) {
|
2022-01-16 07:25:43 +00:00
|
|
|
LogMsg("SetNodeCalim - failed to store credentials");
|
2022-01-15 21:45:37 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|