summaryrefslogtreecommitdiff
path: root/internal/server/servers.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-04-12 22:52:49 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2023-09-25 09:43:37 +0200
commita23c3e61c5d89ef67973891b5b3a176c06e1b174 (patch)
treef1eed03b047f8affd3d5123fa5c9e868ac7d8bec /internal/server/servers.go
parentee95eb45708e1fa766a63866d26d05d13f23e8c9 (diff)
Refactor: Split internal server into multiple packages
- Pass contexts - Have separate packages for e.g. custom, institute and secure - internet servers, profiles.... - Return types from the public ./types package with a Public() method
Diffstat (limited to 'internal/server/servers.go')
-rw-r--r--internal/server/servers.go121
1 files changed, 0 insertions, 121 deletions
diff --git a/internal/server/servers.go b/internal/server/servers.go
deleted file mode 100644
index 60c993d..0000000
--- a/internal/server/servers.go
+++ /dev/null
@@ -1,121 +0,0 @@
-package server
-
-import (
- discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
- "github.com/go-errors/errors"
-)
-
-// TODO: Have a dedicated type for custom servers
-type Servers struct {
- // A custom server is just an institute access server under the hood
- CustomServers InstituteAccessServers `json:"custom_servers"`
- InstituteServers InstituteAccessServers `json:"institute_servers"`
- SecureInternetHomeServer SecureInternetHomeServer `json:"secure_internet_home"`
- IsType Type `json:"is_secure_internet"`
-}
-
-// HasSecureInternet returns whether or not we have a secure internet server added
-func (ss *Servers) HasSecureInternet() bool {
- return len(ss.SecureInternetHomeServer.BaseMap) > 0
-}
-
-func (ss *Servers) AddSecureInternet(
- secureOrg *discotypes.Organization,
- secureServer *discotypes.Server,
-) (Server, error) {
- // If we have specified an organization ID
- // We also need to get an authorization template
- err := ss.SecureInternetHomeServer.init(secureOrg, secureServer)
- if err != nil {
- return nil, err
- }
-
- ss.IsType = SecureInternetServerType
- return &ss.SecureInternetHomeServer, nil
-}
-
-func (ss *Servers) GetCurrentServer() (Server, error) {
- // TODO(jwijenbergh): Almost certainly the return type should be pointer (*Server)
- if ss.IsType == SecureInternetServerType {
- if !ss.HasSecureLocation() {
- return nil, errors.Errorf("ss.IsType = %v; ss.HasSecureLocation() = false", ss.IsType)
- }
- return &ss.SecureInternetHomeServer, nil
- }
-
- srvs := &ss.InstituteServers
-
- if ss.IsType == CustomServerType {
- srvs = &ss.CustomServers
- }
- if srvs.Map == nil {
- return nil, errors.Errorf("srvs.Map is nil")
- }
-
- srv, ok := srvs.Map[srvs.CurrentURL]
- if !ok || srv == nil {
- return nil, errors.Errorf("server not found")
- }
- return srv, nil
-}
-
-func (ss *Servers) addInstituteAndCustom(
- discoServer *discotypes.Server,
- isCustom bool,
-) (Server, error) {
- URL := discoServer.BaseURL
- srvs := &ss.InstituteServers
- srvType := InstituteAccessServerType
-
- if isCustom {
- srvs = &ss.CustomServers
- srvType = CustomServerType
- }
-
- if srvs.Map == nil {
- srvs.Map = make(map[string]*InstituteAccessServer)
- }
-
- srv, ok := srvs.Map[URL]
-
- // initialize the server if it doesn't exist yet
- if !ok {
- srv = &InstituteAccessServer{}
- }
-
- if err := srv.init(URL, discoServer.DisplayName, discoServer.Type, discoServer.SupportContact); err != nil {
- return nil, err
- }
- srvs.Map[URL] = srv
- ss.IsType = srvType
- return srv, nil
-}
-
-func (ss *Servers) AddInstituteAccessServer(
- instituteServer *discotypes.Server,
-) (Server, error) {
- return ss.addInstituteAndCustom(instituteServer, false)
-}
-
-func (ss *Servers) AddCustomServer(
- customServer *discotypes.Server,
-) (Server, error) {
- return ss.addInstituteAndCustom(customServer, true)
-}
-
-func (ss *Servers) GetSecureLocation() string {
- return ss.SecureInternetHomeServer.CurrentLocation
-}
-
-func (ss *Servers) SetSecureLocation(
- chosenLocationServer *discotypes.Server,
-) error {
- // Make sure to add the current location
-
- if _, err := ss.SecureInternetHomeServer.addLocation(chosenLocationServer); err != nil {
- return err
- }
-
- ss.SecureInternetHomeServer.CurrentLocation = chosenLocationServer.CountryCode
- return nil
-}