forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (48 loc) · 1.31 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
package main
import (
"flag"
"fmt"
"net/http"
"github.com/ergo-services/ergo"
"github.com/ergo-services/ergo/node"
)
var (
NodeName string
Cookie string
ListenRangeBegin int
ListenRangeEnd int = 35000
Listen string
ListenEPMD int
)
func init() {
flag.StringVar(&NodeName, "name", "web@127.0.0.1", "node name")
flag.StringVar(&Cookie, "cookie", "123", "cookie for interaction with erlang cluster")
}
func main() {
flag.Parse()
opts := node.Options{}
// Initialize new node with given name, cookie, listening port range and epmd port
nodeHTTP, _ := ergo.StartNode(NodeName, Cookie, opts)
// start application
if _, err := nodeHTTP.ApplicationLoad(&App{}); err != nil {
panic(err)
}
process, _ := nodeHTTP.ApplicationStart("WebApp")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
p := process.ProcessByName("handler_sup")
if p == nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if handlerProcess, err := handler_sup.StartChild(p, "handler", r); err == nil {
process.Send(handlerProcess.Self(), w)
handlerProcess.Wait()
return
}
w.WriteHeader(http.StatusInternalServerError)
})
go http.ListenAndServe(":8080", nil)
fmt.Println("HTTP is listening on http://127.0.0.1:8080")
process.Wait()
nodeHTTP.Stop()
}