happyDomain/services/interfaces.go

114 lines
3.6 KiB
Go
Raw Permalink Normal View History

2023-12-24 10:18:08 +00:00
// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2024 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
2020-05-09 11:14:29 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
2020-05-09 11:14:29 +00:00
//
2023-12-24 10:18:08 +00:00
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2020-05-09 11:14:29 +00:00
//
2023-12-24 10:18:08 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
2020-05-09 11:14:29 +00:00
//
2023-12-24 10:18:08 +00:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-05-09 11:14:29 +00:00
package svcs
2020-06-08 23:25:15 +00:00
import (
"encoding/json"
2020-10-10 16:44:17 +00:00
"fmt"
2020-06-08 23:25:15 +00:00
2023-09-07 09:37:18 +00:00
"git.happydns.org/happyDomain/model"
2020-06-08 23:25:15 +00:00
)
2020-05-09 11:14:29 +00:00
2020-07-06 09:07:37 +00:00
type ServiceRestrictions struct {
// Alone restricts the service to be the only one for a given subdomain.
Alone bool `json:"alone,omitempty"`
// ExclusiveRR restricts the service to be present along with others given types.
ExclusiveRR []string `json:"exclusive,omitempty"`
// GLUE allows a service to be present under Leaf, as GLUE record.
GLUE bool `json:"glue,omitempty"`
// Leaf restricts the creation of subdomains under this kind of service (blocks NearAlone).
Leaf bool `json:"leaf,omitempty"`
// NearAlone allows a service to be present along with Alone restricted services (eg. services that will create sub-subdomain from their given subdomain).
NearAlone bool `json:"nearAlone,omitempty"`
// NeedTypes restricts the service to sources that are compatibles with ALL the given types.
NeedTypes []uint16 `json:"needTypes,omitempty"`
2020-07-06 09:07:37 +00:00
// RootOnly restricts the service to be present at the root of the domain only.
RootOnly bool `json:"rootOnly,omitempty"`
// Single restricts the service to be present only once per subdomain.
Single bool `json:"single,omitempty"`
}
2020-05-09 11:14:29 +00:00
type ServiceInfos struct {
2020-07-06 09:07:37 +00:00
Name string `json:"name"`
2020-10-12 12:16:14 +00:00
Type string `json:"_svctype"`
2020-10-12 14:34:36 +00:00
Icon string `json:"_svcicon,omitempty"`
2020-07-06 09:07:37 +00:00
Description string `json:"description"`
2020-10-10 16:44:17 +00:00
Family string `json:"family"`
2020-07-06 09:07:37 +00:00
Categories []string `json:"categories"`
RecordTypes []uint16 `json:"record_types"`
2020-07-06 09:07:37 +00:00
Tabs bool `json:"tabs,omitempty"`
Restrictions ServiceRestrictions `json:"restrictions,omitempty"`
2020-05-09 11:14:29 +00:00
}
2020-06-08 23:25:15 +00:00
type serviceCombined struct {
Service happydns.Service
}
2020-10-10 16:44:17 +00:00
type ServiceNotFoundError struct {
name string
}
func (err ServiceNotFoundError) Error() string {
return fmt.Sprintf("Unable to find corresponding service for `%s`.", err.name)
}
2020-07-28 15:34:55 +00:00
// UnmarshalServiceJSON implements the UnmarshalJSON function for the
// encoding/json module.
2020-06-08 23:25:15 +00:00
func UnmarshalServiceJSON(svc *happydns.ServiceCombined, b []byte) (err error) {
2020-07-28 15:34:55 +00:00
var svcType happydns.ServiceMeta
2020-06-08 23:25:15 +00:00
err = json.Unmarshal(b, &svcType)
if err != nil {
return
}
var tsvc happydns.Service
tsvc, err = FindService(svcType.Type)
2020-10-10 16:44:17 +00:00
if err != nil {
return
}
2020-06-08 23:25:15 +00:00
mySvc := &serviceCombined{
tsvc,
}
err = json.Unmarshal(b, mySvc)
svc.Service = tsvc
2020-07-28 15:34:55 +00:00
svc.ServiceMeta = svcType
2020-06-08 23:25:15 +00:00
return
}
func init() {
2020-07-28 15:34:55 +00:00
// Register the UnmarshalServiceJSON variable thats points to the
// Service's UnmarshalJSON implementation that can't be made in model
// module due to cyclic dependancy.
2020-06-08 23:25:15 +00:00
happydns.UnmarshalServiceJSON = UnmarshalServiceJSON
}