-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (59 loc) · 1.38 KB
/
main.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 main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"github.com/tmlbl/echox/config"
"github.com/tmlbl/echox/server"
"github.com/tmlbl/echox/shell"
)
func main() {
cfg := config.New()
// If the arguments are config files, load them
for _, arg := range os.Args[1:] {
if _, err := os.Stat(arg); err == nil {
fmt.Println("Loading config from", arg)
data, err := ioutil.ReadFile(arg)
if err != nil {
log.Fatalln(err)
}
c, err := config.Parse(string(data))
if err != nil {
log.Fatalln(err)
}
cfg.Merge(c)
}
}
// If we got no arguments, attempt to read config from stdin
if len(os.Args) == 1 {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalln(err)
}
c, err := config.Parse(string(data))
cfg.Merge(c)
}
// Show include files / make sure they exist
for _, src := range cfg.Sources {
if _, err := os.Stat(src); err != nil {
fmt.Printf("ERROR reading %s: %s\n", src, err)
}
}
// Instantiate the server
srv, err := server.New(runtime.NumCPU(), cfg.Sources, shell.Bash)
if err != nil {
log.Fatalln(err)
}
// Set up handlers for the config
for method := range cfg.Handlers {
for path, cmd := range cfg.Handlers[method] {
fmt.Println("[echox]", method, path, "=>", cmd)
srv.AddRoute(method, path, cmd)
}
}
fmt.Println("[echox] listening on port 7000")
http.ListenAndServe(":7000", srv)
}