forked from jamiealquiza/polymur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
150 lines (127 loc) · 3.75 KB
/
api.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// The MIT License (MIT)
//
// Copyright (c) 2016 Jamie Alquiza
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package polymur
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net"
"strings"
"github.com/jamiealquiza/polymur/output"
"github.com/jamiealquiza/polymur/pool"
)
// Available API commands.
var (
commands = map[string]func(r Request) string{
"getdest": getdest,
"putdest": putdest,
"deldest": deldest,
}
)
// Request holds API request parameters.
type Request struct {
pool *pool.Pool
command string
param string
}
// getdest returns registered destinations from the pool.
func getdest(r Request) string {
dests := make(map[string]interface{})
// Get all registered destinations.
dests["registered"] = r.pool.Registered
// Get all active.
active := []string{}
for k, _ := range r.pool.Conns {
active = append(active, k)
}
dests["active"] = active
// Json.
response, _ := json.MarshalIndent(dests, "", " ")
return fmt.Sprintf("%s\n", response)
}
// putdest registers a destination with the pool.
func putdest(r Request) string {
if r.param == "" {
return fmt.Sprintf("Must provide destination\n")
}
dest, err := pool.ParseDestination(r.param)
if err != nil {
return fmt.Sprintln(err)
}
// TODO replace this func with an
// add destination method on the pool.
go output.DestinationWriter(r.pool, dest)
return fmt.Sprintf("Registered destination: %s\n", r.param)
}
// deldest unregisters a destination with the pool.
func deldest(r Request) string {
if r.param == "" {
return fmt.Sprintf("Must provide destination\n")
}
dest, err := pool.ParseDestination(r.param)
if err != nil {
return fmt.Sprintln(err)
}
r.pool.Unregister(dest)
return fmt.Sprintf("Unregistered destination: %s\n", r.param)
}
// Api is a simple TCP listener that
// listens for requests.
func Api(p *pool.Pool, address string) {
log.Printf("API started: %s\n", address)
server, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("Listener error: %s\n", err)
}
defer server.Close()
for {
conn, err := server.Accept()
if err != nil {
log.Printf("API error: %s\n", err)
continue
}
apiHandler(p, conn)
}
}
// apiHandler reads and handles API requests.
func apiHandler(p *pool.Pool, conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
buf, err := reader.ReadBytes('\n')
if err != nil {
log.Printf("API error: %s\n", err)
}
input := strings.Fields(string(buf[:len(buf)-1]))
request := Request{command: input[0]}
if len(input) > 1 {
request.param = input[1]
}
request.pool = p
if command, valid := commands[request.command]; valid {
response := command(request)
conn.Write([]byte(response))
} else {
m := fmt.Sprintf("Not a command: %s\n", request.command)
conn.Write([]byte(m))
}
}