-
Notifications
You must be signed in to change notification settings - Fork 2
/
registry.go
81 lines (63 loc) · 1.3 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package shimgo
import (
"fmt"
"sync"
)
var serverCache *servers
type servers struct {
backends map[Format]*shimServer
mu sync.RWMutex
}
func init() {
pyserver := newServer(pythonServer)
rbserver := newServer(rubyServer)
serverCache = &servers{
backends: map[Format]*shimServer{
RST: pyserver,
ASCIIDOC: pyserver,
ASCIIDOCTOR: rbserver,
},
}
}
func (s *servers) cleanup() {
s.mu.Lock()
defer s.mu.Unlock()
for _, server := range s.backends {
server.stop()
}
}
func (s *servers) reset() {
s.mu.Lock()
defer s.mu.Unlock()
for _, server := range s.backends {
wasRunning := server.isRunning()
server.reset()
if wasRunning {
server.start()
}
}
}
func (s *servers) hasSupport(f Format) bool {
s.mu.RLock()
defer s.mu.RUnlock()
server, ok := s.backends[f]
if !ok {
return false
}
if err := server.supportsConversion(f); err != nil {
return false
}
return true
}
func (s *servers) getServer(f Format) (*shimServer, error) {
s.mu.RLock()
defer s.mu.RUnlock()
server, ok := s.backends[f]
if !ok {
return nil, fmt.Errorf("server for '%s' is not registered", f)
}
if err := server.supportsConversion(f); err != nil {
return nil, fmt.Errorf("registered server for '%s' does not support conversion [%s]", f, err.Error())
}
return server, nil
}