-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
51 lines (47 loc) · 1013 Bytes
/
server.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
package server
import (
"context"
"errors"
"github.com/gorilla/mux"
"log"
"net/http"
)
type Config struct {
Port string
JWTSecret string
DatabaseUrl string
}
type Server interface {
Config() *Config
}
type Broker struct {
config *Config
router *mux.Router
}
func (b *Broker) Config() *Config {
return b.config
}
func NewServer(ctx context.Context, config *Config) (*Broker, error) {
if config.Port == "" {
return nil, errors.New("Port is required")
}
if config.JWTSecret == "" {
return nil, errors.New("Secret is required")
}
if config.DatabaseUrl == "" {
return nil, errors.New("Database is required")
}
broker := &Broker{
config: config,
router: mux.NewRouter(),
}
return broker, nil
}
func (b *Broker) Start(binder func(s Server, r *mux.Router)) {
b.router = mux.NewRouter()
binder(b, b.router)
log.Println("Starting server on port", b.Config().Port)
if err := http.ListenAndServe(b.config.Port, b.router); err != nil {
log.Fatal("Listen And Serve", err)
}
}