-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gondola.go
53 lines (45 loc) · 1.27 KB
/
gondola.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
package gondola
import (
"fmt"
"io"
"log/slog"
)
// Runner is an interface that defines the Run method.
type Runner interface {
Run() error
}
// NewGondola returns a new Gondola.
func NewGondola(r io.Reader) (*Gondola, error) {
cfg := &Config{}
c, err := cfg.Load(r)
if err != nil {
return nil, &ConfigLoadError{Err: err}
}
s, err := newServer(c)
if err != nil {
return nil, &ProxyServerError{Err: err}
}
return &Gondola{
config: c,
server: s,
}, nil
}
// TODO: Need to dynamically load a configuration file. For now, we will limit the implementation to just loading the file at startup.
// Run starts the proxy server.
func (g *Gondola) Run() error {
logger := NewLogger(g.config.LogLevel)
slog.SetDefault(logger.Logger)
// TODO: do health check for upstreams.
if g.config.Proxy.IsEnableTLS() {
slog.Info(fmt.Sprintf("Running server on port %s with TLS...", g.config.Proxy.Port))
if err := g.server.ListenAndServeTLS(g.config.Proxy.TLSCertPath, g.config.Proxy.TLSKeyPath); err != nil {
slog.Error("Error running server with TLS: " + err.Error())
}
} else {
slog.Info("Running server on port " + g.config.Proxy.Port + "...")
if err := g.server.ListenAndServe(); err != nil {
slog.Error("Error running server: " + err.Error())
}
}
return nil
}