feat(api): server configuration

master
Xeronith 2023-06-23 14:52:07 +03:30
rodzic 429f77fe2f
commit e96ceab3f0
18 zmienionych plików z 795 dodań i 445 usunięć

Wyświetl plik

@ -0,0 +1,22 @@
package commands
import . "github.com/reiver/greatape/components/contracts"
func GetServerConfiguration(x IDispatcher) (IGetServerConfigurationResult, error) {
environment := ""
if x.IsDevelopmentEnvironment() {
environment = "development"
} else if x.IsTestEnvironment() {
environment = "test"
} else if x.IsStagingEnvironment() {
environment = "staging"
} else if x.IsProductionEnvironment() {
environment = "production"
}
return x.NewGetServerConfigurationResult(
"GreatApe",
environment,
x.FQDN(),
), nil
}

Wyświetl plik

@ -45,6 +45,16 @@ func TestEchoApi(test *testing.T) {
} }
} }
func TestGetServerConfigurationApi(test *testing.T) {
input := &GetServerConfigurationRequest{}
if output, err := api.GetServerConfiguration(input); err != nil {
test.Fatal(err)
} else if output == nil {
test.Fail()
}
}
func TestCheckUsernameAvailabilityApi(test *testing.T) { func TestCheckUsernameAvailabilityApi(test *testing.T) {
input := &CheckUsernameAvailabilityRequest{ input := &CheckUsernameAvailabilityRequest{
Username: "username", Username: "username",

Wyświetl plik

@ -8,6 +8,7 @@ func (factory *operationFactory) Operations() []IOperation {
return []IOperation{ return []IOperation{
SystemCallOperation(), SystemCallOperation(),
EchoOperation(), EchoOperation(),
GetServerConfigurationOperation(),
CheckUsernameAvailabilityOperation(), CheckUsernameAvailabilityOperation(),
SignupOperation(), SignupOperation(),
ResendVerificationCodeOperation(), ResendVerificationCodeOperation(),

Wyświetl plik

@ -0,0 +1,39 @@
package operations
import (
. "github.com/reiver/greatape/components/api/protobuf"
. "github.com/reiver/greatape/components/api/services"
. "github.com/reiver/greatape/components/contracts"
. "github.com/xeronith/diamante/contracts/operation"
. "github.com/xeronith/diamante/contracts/service"
. "github.com/xeronith/diamante/contracts/system"
. "github.com/xeronith/diamante/operation"
)
type getServerConfigurationOperation struct {
Operation
run func(IContext, *GetServerConfigurationRequest) (*GetServerConfigurationResult, error)
}
func GetServerConfigurationOperation() IOperation {
return &getServerConfigurationOperation{
run: GetServerConfigurationService,
}
}
func (operation *getServerConfigurationOperation) Id() (ID, ID) {
return GET_SERVER_CONFIGURATION_REQUEST, GET_SERVER_CONFIGURATION_RESULT
}
func (operation *getServerConfigurationOperation) InputContainer() Pointer {
return new(GetServerConfigurationRequest)
}
func (operation *getServerConfigurationOperation) OutputContainer() Pointer {
return new(GetServerConfigurationResult)
}
func (operation *getServerConfigurationOperation) Execute(context IContext, payload Pointer) (Pointer, error) {
return operation.run(context, payload.(*GetServerConfigurationRequest))
}

Wyświetl plik

@ -25,6 +25,17 @@ message EchoResult {
Document document = 0x00000001; Document document = 0x00000001;
} }
// API: GetServerConfiguration
//-----------------------------------------------------------
message GetServerConfigurationRequest {
}
message GetServerConfigurationResult {
string product = 0x00000001;
string environment = 0x00000002;
string fqdn = 0x00000003;
}
// API: CheckUsernameAvailability // API: CheckUsernameAvailability
//----------------------------------------------------------- //-----------------------------------------------------------
message CheckUsernameAvailabilityRequest { message CheckUsernameAvailabilityRequest {

Wyświetl plik

@ -0,0 +1,25 @@
package services
import (
. "github.com/reiver/greatape/components/api/protobuf"
. "github.com/reiver/greatape/components/contracts"
. "github.com/reiver/greatape/components/core"
. "github.com/xeronith/diamante/contracts/service"
)
func GetServerConfigurationService(context IContext, input *GetServerConfigurationRequest) (result *GetServerConfigurationResult, err error) {
source := "get_server_configuration"
/* //////// */ Conductor.LogRemoteCall(context, INIT, source, input, result, err)
defer func() { Conductor.LogRemoteCall(context, DONE, source, input, result, err) }()
commandResult, err := Conductor.GetServerConfiguration(context.Identity())
if err != nil {
return nil, err
}
result = context.ResultContainer().(*GetServerConfigurationResult)
result.Product = commandResult.Product()
result.Environment = commandResult.Environment()
result.Fqdn = commandResult.Fqdn()
return result, nil
}

Wyświetl plik

@ -8,6 +8,7 @@ type IApi interface {
//API Methods //API Methods
SystemCall(*SystemCallRequest) (*SystemCallResult, error) SystemCall(*SystemCallRequest) (*SystemCallResult, error)
Echo(*EchoRequest) (*EchoResult, error) Echo(*EchoRequest) (*EchoResult, error)
GetServerConfiguration(*GetServerConfigurationRequest) (*GetServerConfigurationResult, error)
CheckUsernameAvailability(*CheckUsernameAvailabilityRequest) (*CheckUsernameAvailabilityResult, error) CheckUsernameAvailability(*CheckUsernameAvailabilityRequest) (*CheckUsernameAvailabilityResult, error)
Signup(*SignupRequest) (*SignupResult, error) Signup(*SignupRequest) (*SignupResult, error)
ResendVerificationCode(*ResendVerificationCodeRequest) (*ResendVerificationCodeResult, error) ResendVerificationCode(*ResendVerificationCodeRequest) (*ResendVerificationCodeResult, error)

Wyświetl plik

@ -12,6 +12,10 @@ const (
ECHO_REQUEST = 0x0541BD72 ECHO_REQUEST = 0x0541BD72
ECHO_RESULT = 0xAB2FF7D4 ECHO_RESULT = 0xAB2FF7D4
//GetServerConfigurationOperation
GET_SERVER_CONFIGURATION_REQUEST = 0xC89C311F
GET_SERVER_CONFIGURATION_RESULT = 0xA1A50921
//CheckUsernameAvailabilityOperation //CheckUsernameAvailabilityOperation
CHECK_USERNAME_AVAILABILITY_REQUEST = 0xA9501A55 CHECK_USERNAME_AVAILABILITY_REQUEST = 0xA9501A55
CHECK_USERNAME_AVAILABILITY_RESULT = 0x067190FF CHECK_USERNAME_AVAILABILITY_RESULT = 0x067190FF
@ -101,6 +105,8 @@ var OPCODES = Opcodes{
0x00000000: "N/A", 0x00000000: "N/A",
0x0541BD72: "ECHO", 0x0541BD72: "ECHO",
0xAB2FF7D4: "Echo", 0xAB2FF7D4: "Echo",
0xC89C311F: "GET_SERVER_CONFIGURATION",
0xA1A50921: "GetServerConfiguration",
0xA9501A55: "CHECK_USERNAME_AVAILABILITY", 0xA9501A55: "CHECK_USERNAME_AVAILABILITY",
0x067190FF: "CheckUsernameAvailability", 0x067190FF: "CheckUsernameAvailability",
0x48DB23BF: "SIGNUP", 0x48DB23BF: "SIGNUP",

Wyświetl plik

@ -56,6 +56,7 @@ type (
Filter(predicate SpiFilterPredicate) ISpiCollection Filter(predicate SpiFilterPredicate) ISpiCollection
Map(predicate SpiMapPredicate) ISpiCollection Map(predicate SpiMapPredicate) ISpiCollection
Echo(document IDocument, editor Identity) (IEchoResult, error) Echo(document IDocument, editor Identity) (IEchoResult, error)
GetServerConfiguration(editor Identity) (IGetServerConfigurationResult, error)
CheckUsernameAvailability(username string, editor Identity) (ICheckUsernameAvailabilityResult, error) CheckUsernameAvailability(username string, editor Identity) (ICheckUsernameAvailabilityResult, error)
Signup(username string, email string, password string, editor Identity) (ISignupResult, error) Signup(username string, email string, password string, editor Identity) (ISignupResult, error)
ResendVerificationCode(email string, editor Identity) (IResendVerificationCodeResult, error) ResendVerificationCode(email string, editor Identity) (IResendVerificationCodeResult, error)
@ -83,6 +84,12 @@ type (
Document() IDocument Document() IDocument
} }
IGetServerConfigurationResult interface {
Product() string
Environment() string
Fqdn() string
}
ICheckUsernameAvailabilityResult interface { ICheckUsernameAvailabilityResult interface {
IsAvailable() bool IsAvailable() bool
} }

Wyświetl plik

@ -258,6 +258,7 @@ type (
RemoveSpi(id int64, editor Identity) (ISpi, error) RemoveSpi(id int64, editor Identity) (ISpi, error)
RemoveSpiAtomic(transaction ITransaction, id int64, editor Identity) (ISpi, error) RemoveSpiAtomic(transaction ITransaction, id int64, editor Identity) (ISpi, error)
Echo(document IDocument, editor Identity) (IEchoResult, error) Echo(document IDocument, editor Identity) (IEchoResult, error)
GetServerConfiguration(editor Identity) (IGetServerConfigurationResult, error)
CheckUsernameAvailability(username string, editor Identity) (ICheckUsernameAvailabilityResult, error) CheckUsernameAvailability(username string, editor Identity) (ICheckUsernameAvailabilityResult, error)
Signup(username string, email string, password string, editor Identity) (ISignupResult, error) Signup(username string, email string, password string, editor Identity) (ISignupResult, error)
ResendVerificationCode(email string, editor Identity) (IResendVerificationCodeResult, error) ResendVerificationCode(email string, editor Identity) (IResendVerificationCodeResult, error)
@ -298,6 +299,7 @@ type (
NewActivityPubFollower(id int64, handle string, inbox string, subject string, activity string, accepted bool) (IActivityPubFollower, error) NewActivityPubFollower(id int64, handle string, inbox string, subject string, activity string, accepted bool) (IActivityPubFollower, error)
NewSpi() (ISpi, error) NewSpi() (ISpi, error)
NewEchoResult(document IDocument, ignored interface{}) IEchoResult NewEchoResult(document IDocument, ignored interface{}) IEchoResult
NewGetServerConfigurationResult(product string, environment string, fqdn string, ignored interface{}) IGetServerConfigurationResult
NewCheckUsernameAvailabilityResult(isAvailable bool, ignored interface{}) ICheckUsernameAvailabilityResult NewCheckUsernameAvailabilityResult(isAvailable bool, ignored interface{}) ICheckUsernameAvailabilityResult
NewSignupResult(token string, code string, ignored interface{}) ISignupResult NewSignupResult(token string, code string, ignored interface{}) ISignupResult
NewResendVerificationCodeResult(code string, ignored interface{}) IResendVerificationCodeResult NewResendVerificationCodeResult(code string, ignored interface{}) IResendVerificationCodeResult

Wyświetl plik

@ -1033,6 +1033,7 @@ type IDispatcher interface {
// the transaction if used in an x.Atomic context. This method is synchronous. // the transaction if used in an x.Atomic context. This method is synchronous.
RemoveSpi(id int64) ISpi RemoveSpi(id int64) ISpi
Echo(document IDocument) (IEchoResult, error) Echo(document IDocument) (IEchoResult, error)
GetServerConfiguration() (IGetServerConfigurationResult, error)
CheckUsernameAvailability(username string) (ICheckUsernameAvailabilityResult, error) CheckUsernameAvailability(username string) (ICheckUsernameAvailabilityResult, error)
Signup(username string, email string, password string) (ISignupResult, error) Signup(username string, email string, password string) (ISignupResult, error)
ResendVerificationCode(email string) (IResendVerificationCodeResult, error) ResendVerificationCode(email string) (IResendVerificationCodeResult, error)
@ -1125,6 +1126,8 @@ type IDispatcher interface {
NewSpis() ISpiCollection NewSpis() ISpiCollection
// NewEchoResult creates a new result container for 'Echo' system action. // NewEchoResult creates a new result container for 'Echo' system action.
NewEchoResult(document IDocument) IEchoResult NewEchoResult(document IDocument) IEchoResult
// NewGetServerConfigurationResult creates a new result container for 'Get Server Configuration' system action.
NewGetServerConfigurationResult(product string, environment string, fqdn string) IGetServerConfigurationResult
// NewCheckUsernameAvailabilityResult creates a new result container for 'Check Username Availability' system action. // NewCheckUsernameAvailabilityResult creates a new result container for 'Check Username Availability' system action.
NewCheckUsernameAvailabilityResult(isAvailable bool) ICheckUsernameAvailabilityResult NewCheckUsernameAvailabilityResult(isAvailable bool) ICheckUsernameAvailabilityResult
// NewSignupResult creates a new result container for 'Signup' system action. // NewSignupResult creates a new result container for 'Signup' system action.

Wyświetl plik

@ -25,6 +25,16 @@ func (api *api) Echo(request *EchoRequest) (*EchoResult, error) {
} }
} }
func (api *api) GetServerConfiguration(request *GetServerConfigurationRequest) (*GetServerConfigurationResult, error) {
result, err := api.call(GET_SERVER_CONFIGURATION_REQUEST, request)
if err != nil {
return nil, err
} else {
return result.(*GetServerConfigurationResult), nil
}
}
func (api *api) CheckUsernameAvailability(request *CheckUsernameAvailabilityRequest) (*CheckUsernameAvailabilityResult, error) { func (api *api) CheckUsernameAvailability(request *CheckUsernameAvailabilityRequest) (*CheckUsernameAvailabilityResult, error) {
result, err := api.call(CHECK_USERNAME_AVAILABILITY_REQUEST, request) result, err := api.call(CHECK_USERNAME_AVAILABILITY_REQUEST, request)
@ -238,6 +248,7 @@ func (api *api) GetInbox(request *GetInboxRequest) (*GetInboxResult, error) {
func init() { func init() {
API_RESULT[SYSTEM_CALL_RESULT] = SystemCallResult{} API_RESULT[SYSTEM_CALL_RESULT] = SystemCallResult{}
API_RESULT[ECHO_RESULT] = EchoResult{} API_RESULT[ECHO_RESULT] = EchoResult{}
API_RESULT[GET_SERVER_CONFIGURATION_RESULT] = GetServerConfigurationResult{}
API_RESULT[CHECK_USERNAME_AVAILABILITY_RESULT] = CheckUsernameAvailabilityResult{} API_RESULT[CHECK_USERNAME_AVAILABILITY_RESULT] = CheckUsernameAvailabilityResult{}
API_RESULT[SIGNUP_RESULT] = SignupResult{} API_RESULT[SIGNUP_RESULT] = SignupResult{}
API_RESULT[RESEND_VERIFICATION_CODE_RESULT] = ResendVerificationCodeResult{} API_RESULT[RESEND_VERIFICATION_CODE_RESULT] = ResendVerificationCodeResult{}

Wyświetl plik

@ -1098,6 +1098,10 @@ func (conductor *conductor) Echo(document IDocument, editor Identity) (IEchoResu
return conductor.spiManager.Echo(document, editor) return conductor.spiManager.Echo(document, editor)
} }
func (conductor *conductor) GetServerConfiguration(editor Identity) (IGetServerConfigurationResult, error) {
return conductor.spiManager.GetServerConfiguration(editor)
}
func (conductor *conductor) CheckUsernameAvailability(username string, editor Identity) (ICheckUsernameAvailabilityResult, error) { func (conductor *conductor) CheckUsernameAvailability(username string, editor Identity) (ICheckUsernameAvailabilityResult, error) {
return conductor.spiManager.CheckUsernameAvailability(username, editor) return conductor.spiManager.CheckUsernameAvailability(username, editor)
} }
@ -1254,6 +1258,10 @@ func (conductor *conductor) NewEchoResult(document IDocument, _ interface{}) IEc
return NewEchoResult(document, nil) return NewEchoResult(document, nil)
} }
func (conductor *conductor) NewGetServerConfigurationResult(product string, environment string, fqdn string, _ interface{}) IGetServerConfigurationResult {
return NewGetServerConfigurationResult(product, environment, fqdn, nil)
}
func (conductor *conductor) NewCheckUsernameAvailabilityResult(isAvailable bool, _ interface{}) ICheckUsernameAvailabilityResult { func (conductor *conductor) NewCheckUsernameAvailabilityResult(isAvailable bool, _ interface{}) ICheckUsernameAvailabilityResult {
return NewCheckUsernameAvailabilityResult(isAvailable, nil) return NewCheckUsernameAvailabilityResult(isAvailable, nil)
} }

Wyświetl plik

@ -244,6 +244,10 @@ func (dispatcher *dispatcher) Echo(document IDocument) (IEchoResult, error) {
return dispatcher.conductor.SpiManager().Echo(document, dispatcher.identity) return dispatcher.conductor.SpiManager().Echo(document, dispatcher.identity)
} }
func (dispatcher *dispatcher) GetServerConfiguration() (IGetServerConfigurationResult, error) {
return dispatcher.conductor.SpiManager().GetServerConfiguration(dispatcher.identity)
}
func (dispatcher *dispatcher) CheckUsernameAvailability(username string) (ICheckUsernameAvailabilityResult, error) { func (dispatcher *dispatcher) CheckUsernameAvailability(username string) (ICheckUsernameAvailabilityResult, error) {
return dispatcher.conductor.SpiManager().CheckUsernameAvailability(username, dispatcher.identity) return dispatcher.conductor.SpiManager().CheckUsernameAvailability(username, dispatcher.identity)
} }

Wyświetl plik

@ -228,6 +228,53 @@ func (manager *spiManager) Echo(document IDocument, editor Identity) (result IEc
} }
} }
//region IGetServerConfigurationResult Implementation
type getServerConfigurationResult struct {
product string
environment string
fqdn string
}
func NewGetServerConfigurationResult(product string, environment string, fqdn string, _ interface{}) IGetServerConfigurationResult {
return &getServerConfigurationResult{
product: product,
environment: environment,
fqdn: fqdn,
}
}
func (result getServerConfigurationResult) Product() string {
return result.product
}
func (result getServerConfigurationResult) Environment() string {
return result.environment
}
func (result getServerConfigurationResult) Fqdn() string {
return result.fqdn
}
//endregion
func (manager *spiManager) GetServerConfiguration(editor Identity) (result IGetServerConfigurationResult, err error) {
defer func() {
if reason := recover(); reason != nil {
err = manager.Error(reason)
}
}()
editor.Lock(GET_SERVER_CONFIGURATION_REQUEST)
defer editor.Unlock(GET_SERVER_CONFIGURATION_REQUEST)
if result, err = commands.GetServerConfiguration(NewDispatcher(Conductor, editor)); err != nil {
return nil, err
} else {
return result, nil
}
}
//region ICheckUsernameAvailabilityResult Implementation //region ICheckUsernameAvailabilityResult Implementation
type checkUsernameAvailabilityResult struct { type checkUsernameAvailabilityResult struct {

Wyświetl plik

@ -160,6 +160,17 @@ func TestSpiManager_Echo(test *testing.T) {
_ = result _ = result
} }
func TestSpiManager_GetServerConfiguration(test *testing.T) {
manager := Conductor.SpiManager()
result, err := manager.GetServerConfiguration(nil)
if err != nil {
test.Fatal(err)
}
_ = result
}
func TestSpiManager_CheckUsernameAvailability(test *testing.T) { func TestSpiManager_CheckUsernameAvailability(test *testing.T) {
manager := Conductor.SpiManager() manager := Conductor.SpiManager()

Wyświetl plik

@ -8,6 +8,10 @@ func (dispatcher *dispatcher) NewEchoResult(document IDocument) IEchoResult {
return NewEchoResult(document, nil) return NewEchoResult(document, nil)
} }
func (dispatcher *dispatcher) NewGetServerConfigurationResult(product string, environment string, fqdn string) IGetServerConfigurationResult {
return NewGetServerConfigurationResult(product, environment, fqdn, nil)
}
func (dispatcher *dispatcher) NewCheckUsernameAvailabilityResult(isAvailable bool) ICheckUsernameAvailabilityResult { func (dispatcher *dispatcher) NewCheckUsernameAvailabilityResult(isAvailable bool) ICheckUsernameAvailabilityResult {
return NewCheckUsernameAvailabilityResult(isAvailable, nil) return NewCheckUsernameAvailabilityResult(isAvailable, nil)
} }