-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
68 lines (61 loc) · 1.81 KB
/
config.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
package server
import (
"crypto/tls"
"log"
"regexp"
"strconv"
"sync"
"time"
"github.com/aukbit/pluto/common"
"github.com/aukbit/pluto/discovery"
"github.com/aukbit/pluto/server/router"
"google.golang.org/grpc"
)
// Config server configuaration options
type Config struct {
ID string
Name string
Description string
Addr string // TCP address (e.g. localhost:8000) to listen on, ":http" if empty
Format string
Mux *router.Router
TLSConfig *tls.Config // optional TLS config, used by ListenAndServeTLS
GRPCRegister GRPCRegisterServiceFunc
Discovery discovery.Discovery
ReadTimeout time.Duration
WriteTimeout time.Duration
mu sync.Mutex // ensures atomic writes; protects the following fields
Middlewares []router.Middleware // http middlewares
UnaryServerInterceptors []grpc.UnaryServerInterceptor // gRPC interceptors
StreamServerInterceptors []grpc.StreamServerInterceptor // gRPC interceptors
}
// GRPCRegisterServiceFunc grpc
type GRPCRegisterServiceFunc func(*grpc.Server)
var (
defaultAddr = ":8080"
defaultFormat = "http"
)
func newConfig() Config {
return Config{
ID: common.RandID("srv_", 6),
Name: DefaultName,
Addr: defaultAddr,
Format: defaultFormat,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
// Port converts string Addr to int Port
func (c *Config) Port() int {
// support only numeric
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Fatal(err)
}
safe := reg.ReplaceAllString(c.Addr, "")
i, err := strconv.Atoi(safe)
if err != nil {
log.Fatal(err)
}
return i
}