api: autogenerate structs from configuration

This commit is contained in:
aler9 2022-10-23 18:01:51 +02:00
parent ec4ec142b4
commit fdb1955e1d
1 changed files with 24 additions and 98 deletions

View File

@ -20,7 +20,7 @@ func interfaceIsEmpty(i interface{}) bool {
} }
func fillStruct(dest interface{}, source interface{}) { func fillStruct(dest interface{}, source interface{}) {
rvsource := reflect.ValueOf(source) rvsource := reflect.ValueOf(source).Elem()
rvdest := reflect.ValueOf(dest) rvdest := reflect.ValueOf(dest)
nf := rvsource.NumField() nf := rvsource.NumField()
for i := 0; i < nf; i++ { for i := 0; i < nf; i++ {
@ -41,64 +41,30 @@ func cloneStruct(dest interface{}, source interface{}) {
_ = json.Unmarshal(enc, dest) _ = json.Unmarshal(enc, dest)
} }
func loadConfData(ctx *gin.Context) (interface{}, error) { func generateStructWithOptionalFields(model interface{}) interface{} {
var in struct { var fields []reflect.StructField
// general
LogLevel *conf.LogLevel `json:"logLevel"`
LogDestinations *conf.LogDestinations `json:"logDestinations"`
LogFile *string `json:"logFile"`
ReadTimeout *conf.StringDuration `json:"readTimeout"`
WriteTimeout *conf.StringDuration `json:"writeTimeout"`
ReadBufferCount *int `json:"readBufferCount"`
ExternalAuthenticationURL *string `json:"externalAuthenticationURL"`
API *bool `json:"api"`
APIAddress *string `json:"apiAddress"`
Metrics *bool `json:"metrics"`
MetricsAddress *string `json:"metricsAddress"`
PPROF *bool `json:"pprof"`
PPROFAddress *string `json:"pprofAddress"`
RunOnConnect *string `json:"runOnConnect"`
RunOnConnectRestart *bool `json:"runOnConnectRestart"`
// RTSP rt := reflect.TypeOf(model)
RTSPDisable *bool `json:"rtspDisable"` nf := rt.NumField()
Protocols *conf.Protocols `json:"protocols"` for i := 0; i < nf; i++ {
Encryption *conf.Encryption `json:"encryption"` f := rt.Field(i)
RTSPAddress *string `json:"rtspAddress"` j := f.Tag.Get("json")
RTSPSAddress *string `json:"rtspsAddress"`
RTPAddress *string `json:"rtpAddress"`
RTCPAddress *string `json:"rtcpAddress"`
MulticastIPRange *string `json:"multicastIPRange"`
MulticastRTPPort *int `json:"multicastRTPPort"`
MulticastRTCPPort *int `json:"multicastRTCPPort"`
ServerKey *string `json:"serverKey"`
ServerCert *string `json:"serverCert"`
AuthMethods *conf.AuthMethods `json:"authMethods"`
// RTMP if j != "-" && j != "paths" {
RTMPDisable *bool `json:"rtmpDisable"` fields = append(fields, reflect.StructField{
RTMPAddress *string `json:"rtmpAddress"` Name: f.Name,
RTMPEncryption *conf.Encryption `json:"rtmpEncryption"` Type: reflect.PtrTo(f.Type),
RTMPSAddress *string `json:"rtmpsAddress"` Tag: f.Tag,
RTMPServerKey *string `json:"rtmpServerKey"` })
RTMPServerCert *string `json:"rtmpServerCert"`
// HLS
HLSDisable *bool `json:"hlsDisable"`
HLSAddress *string `json:"hlsAddress"`
HLSAlwaysRemux *bool `json:"hlsAlwaysRemux"`
HLSVariant *conf.HLSVariant `json:"hlsVariant"`
HLSSegmentCount *int `json:"hlsSegmentCount"`
HLSSegmentDuration *conf.StringDuration `json:"hlsSegmentDuration"`
HLSPartDuration *conf.StringDuration `json:"hlsPartDuration"`
HLSSegmentMaxSize *conf.StringSize `json:"hlsSegmentMaxSize"`
HLSAllowOrigin *string `json:"hlsAllowOrigin"`
HLSEncryption *bool `json:"hlsEncryption"`
HLSServerKey *string `json:"hlsServerKey"`
HLSServerCert *string `json:"hlsServerCert"`
HLSTrustedProxies *conf.IPsOrCIDRs `json:"hlsTrustedProxies"`
} }
err := json.NewDecoder(ctx.Request.Body).Decode(&in) }
return reflect.New(reflect.StructOf(fields)).Interface()
}
func loadConfData(ctx *gin.Context) (interface{}, error) {
in := generateStructWithOptionalFields(conf.Conf{})
err := json.NewDecoder(ctx.Request.Body).Decode(in)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -107,48 +73,8 @@ func loadConfData(ctx *gin.Context) (interface{}, error) {
} }
func loadConfPathData(ctx *gin.Context) (interface{}, error) { func loadConfPathData(ctx *gin.Context) (interface{}, error) {
var in struct { in := generateStructWithOptionalFields(conf.PathConf{})
// source err := json.NewDecoder(ctx.Request.Body).Decode(in)
Source *string `json:"source"`
SourceProtocol *conf.SourceProtocol `json:"sourceProtocol"`
SourceAnyPortEnable *bool `json:"sourceAnyPortEnable"`
SourceFingerprint *string `json:"sourceFingerprint"`
SourceOnDemand *bool `json:"sourceOnDemand"`
SourceOnDemandStartTimeout *conf.StringDuration `json:"sourceOnDemandStartTimeout"`
SourceOnDemandCloseAfter *conf.StringDuration `json:"sourceOnDemandCloseAfter"`
SourceRedirect *string `json:"sourceRedirect"`
DisablePublisherOverride *bool `json:"disablePublisherOverride"`
Fallback *string `json:"fallback"`
RPICameraCamID *int `json:"rpiCameraCamID"`
RPICameraWidth *int `json:"rpiCameraWidth"`
RPICameraHeight *int `json:"rpiCameraHeight"`
RPICameraFPS *int `json:"rpiCameraFPS"`
RPICameraIDRPeriod *int `json:"rpiCameraIDRPeriod"`
RPICameraBitrate *int `json:"rpiCameraBitrate"`
RPICameraProfile *string `json:"rpiCameraProfile"`
RPICameraLevel *string `json:"rpiCameraLevel"`
// authentication
PublishUser *conf.Credential `json:"publishUser"`
PublishPass *conf.Credential `json:"publishPass"`
PublishIPs *conf.IPsOrCIDRs `json:"publishIPs"`
ReadUser *conf.Credential `json:"readUser"`
ReadPass *conf.Credential `json:"readPass"`
ReadIPs *conf.IPsOrCIDRs `json:"readIPs"`
// external commands
RunOnInit *string `json:"runOnInit"`
RunOnInitRestart *bool `json:"runOnInitRestart"`
RunOnDemand *string `json:"runOnDemand"`
RunOnDemandRestart *bool `json:"runOnDemandRestart"`
RunOnDemandStartTimeout *conf.StringDuration `json:"runOnDemandStartTimeout"`
RunOnDemandCloseAfter *conf.StringDuration `json:"runOnDemandCloseAfter"`
RunOnReady *string `json:"runOnReady"`
RunOnReadyRestart *bool `json:"runOnReadyRestart"`
RunOnRead *string `json:"runOnRead"`
RunOnReadRestart *bool `json:"runOnReadRestart"`
}
err := json.NewDecoder(ctx.Request.Body).Decode(&in)
if err != nil { if err != nil {
return nil, err return nil, err
} }