-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.go
56 lines (50 loc) · 1.27 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
package main
import (
"net/http"
"os"
"github.com/codegangsta/cli"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func main() {
app := cli.NewApp()
app.Name = "Law"
app.Usage = "Law's room."
app.Author = "Cloud"
app.Email = "cloud@txthinking.com"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "listen",
Value: "",
Usage: "Listen address.",
},
cli.StringSliceFlag{
Name: "origin",
Usage: "Allow origins for CORS, can repeat more times.",
},
}
app.Action = func(c *cli.Context) error {
if c.String("listen") == "" {
return cli.NewExitError("Listen address is empty.", 86)
}
return run(c.String("listen"), c.GlobalStringSlice("origin"))
}
app.Run(os.Args)
}
func run(listen string, origins []string) error {
r := mux.NewRouter()
r.Methods("GET").Path("/signal/_/{id}").Handler(getSignalHandle(origins))
r.Methods("GET").Path("/random").HandlerFunc(redirect)
r.Methods("GET").Path("/room/{roomID}").HandlerFunc(redirect)
n := negroni.New()
n.Use(negroni.NewRecovery())
n.Use(negroni.NewLogger())
n.Use(cors.New(cors.Options{
AllowedOrigins: origins,
AllowedMethods: []string{"GET", "POST", "DELETE", "PUT"},
AllowCredentials: true,
}))
n.UseHandler(r)
return http.ListenAndServe(listen, n)
}