-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
47 lines (39 loc) · 886 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
package stallion
import (
"fmt"
"net"
"github.com/official-stallion/stallion/internal"
)
type Server interface {
// Handle method generates a new worker for clients.
Handle(conn net.Conn)
}
// NewServer creates a new broker server on given port.
func NewServer(port string, metrics int, auth ...string) error {
// get authentication options
var (
user string
pass string
)
// setting the authentication options
if len(auth) > 1 && auth[0] != "" && auth[1] != "" {
user = auth[0]
pass = auth[1]
} else {
user = " "
pass = " "
}
// creating a new server
serve := internal.NewServer(metrics, user, pass)
// listen over a port
listener, err := net.Listen("tcp", port)
if err != nil {
return fmt.Errorf("failed to start server: %v", err)
}
// handling our clients
for {
if conn, er := listener.Accept(); er == nil {
serve.Handle(conn)
}
}
}