add first Server struct

implement loading of server configs
implement hasPermission on server struct
This commit is contained in:
Jakob Schrettenbrunner 2017-07-06 20:51:09 +02:00
parent 5c8eb31ba0
commit ec937603fd

View File

@ -1,12 +1,85 @@
package control package control
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// Server is a single instance of a Service managed by the panel // Server is a single instance of a Service managed by the panel
type Server struct { type Server struct {
Service *Service // UUID is the unique identifier of the server
UUID string `json:"uuid"`
// Service is the service the server is an instance of
Service *Service `json:"-"`
// ServiceName is the name of the service. It is mainly used to allow storing the service
// in the config
ServiceName string `json:"serviceName"`
Keys map[string][]string `json:"keys"`
}
var servers map[string]*Server
// LoadServerConfigurations loads the configured servers from a specified path
func LoadServerConfigurations(path string) error {
serverFiles, err := ioutil.ReadDir(path)
if err != nil {
return err
}
servers = make(map[string]*Server)
for _, file := range serverFiles {
if !file.IsDir() {
server, err := loadServerConfiguration(path + file.Name())
if err != nil {
return err
}
servers[server.UUID] = server
}
}
return nil
}
func loadServerConfiguration(path string) (*Server, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
server := NewServer()
if err := json.Unmarshal(file, server); err != nil {
return nil, err
}
fmt.Println(server)
return server, nil
}
// GetServer returns the server identified by the provided uuid
func GetServer(uuid string) *Server {
return servers[uuid]
}
// NewServer creates a new Server
func NewServer() *Server {
return new(Server)
} }
// HasPermission checks wether a provided token has a specific permission // HasPermission checks wether a provided token has a specific permission
func (s *Server) HasPermission(token string, permission string) bool { func (s *Server) HasPermission(token string, permission string) bool {
// TODO: properly implement this for key, perms := range s.Keys {
return true if key == token {
for _, perm := range perms {
if perm == permission || perm == "s:*" {
return true
}
}
return false
}
}
return false
} }