-
Notifications
You must be signed in to change notification settings - Fork 2
/
mussh.go
75 lines (60 loc) · 1.7 KB
/
mussh.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
package main
import (
"github.com/dancannon/gorethink"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"log"
"mussh/resources/command"
"mussh/resources/execution"
"mussh/resources/group"
"mussh/resources/server"
"net/http"
"time"
)
func main() {
session := connectToDb()
m := martini.Classic()
m.Use(render.Renderer())
m.Use(func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Access-Control-Allow-Origin", "*")
res.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
})
m.Use(martini.Static("public"))
m.Map(session)
m.Action(getRouter())
log.Println("listening on port: 7979")
http.ListenAndServe(":7979", m)
// if err := http.ListenAndServeTLS(":7979", "cert.pem", "key.pem", m); err != nil {
// log.Fatal(err)
// }
}
func connectToDb() *gorethink.Session {
session, err := gorethink.Connect(gorethink.ConnectOpts{
Address: "localhost:28015",
Database: "mussh",
MaxIdle: 10,
IdleTimeout: time.Second * 10,
})
if err != nil {
log.Println(err)
}
return session
}
func getRouter() martini.Handler {
r := martini.NewRouter()
r.Get("/servers", server.Get)
r.Post("/servers", binding.Bind(server.Server{}), server.Post)
r.Delete("/servers/:id", server.Delete)
r.Get("/groups", group.GetWithServer)
r.Post("/groups", binding.Bind(group.Group{}), group.Post)
r.Delete("/groups/:id", group.Delete)
r.Get("/commands", command.Get)
r.Post("/commands", binding.Bind(command.Command{}), command.Post)
r.Delete("/commands/:id", command.Delete)
r.Get("/executions", execution.Get)
r.Options("**", func(r render.Render) {
r.Status(http.StatusOK)
})
return r.Handle
}