ActivityPub/config/configController.go
2020-11-27 05:46:17 +01:00

62 lines
1.5 KiB
Go

package config
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"git.nutfactory.org/hoernschen/ActivityPub/utils/database"
)
type SetParamBody struct {
Packetloss int `json:"packetloss,omitempty"`
UnavailableTill int64 `json:"unavailableTill,omitempty"`
Consensus bool `json:"consensus,omitempty"`
AuthentificationCheck bool `json:"authentificationCheck,omitempty"`
Signing bool `json:"signing,omitempty"`
Encryption bool `json:"encryption,omitempty"`
}
func SetParams(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
request := SetParamBody{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(fmt.Sprintf("Could not parse JSON: %s", err)); err != nil {
panic(err)
}
return
}
Packetloss = request.Packetloss
UnavailableTill = request.UnavailableTill
Consensus = request.Consensus
AuthentificationCheck = request.AuthentificationCheck
Signing = request.Signing
Encryption = request.Signing
HttpString = "https"
if !Encryption {
HttpString = "http"
}
w.WriteHeader(http.StatusOK)
}
func Reset(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
database.DB.Close()
os.Remove("sqlite.db")
if err := database.InitDB("sqlite.db"); err != nil {
log.Fatal(err)
}
SetDefaultParams()
w.WriteHeader(http.StatusOK)
}