-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
46 lines (38 loc) · 962 Bytes
/
service.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
package bridge
import (
"net/http"
"strconv"
)
type Service struct {
Config *Config
}
func NewService(configPath string) (*Service, error) {
config, err := newConfigFromFile(configPath)
if err != nil {
return nil, err
}
return &Service{Config: config}, nil
}
func (s *Service) StartServer() error {
for _, b := range s.Config.Bridges {
pattern := s.Config.Server.PathPrefix + "/" + b.Name
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
input, err := b.Source.GetInput(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
output, err := b.Converter.Convert(input)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = b.Target.SendOutput(output); err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
w.WriteHeader(http.StatusOK)
})
}
return http.ListenAndServe(":"+strconv.Itoa(s.Config.Server.Port), nil)
}