This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
70 lines (59 loc) · 1.67 KB
/
app.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
package actions
import (
"log"
"net/http"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/envy"
contenttype "github.com/gobuffalo/mw-contenttype"
paramlogger "github.com/gobuffalo/mw-paramlogger"
validator "gopkg.in/go-playground/validator.v9"
"github.com/gobuffalo/x/sessions"
socketio "github.com/googollee/go-socket.io"
"github.com/rs/cors"
)
// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = envy.Get("GO_ENV", "development")
var app *buffalo.App
var validate *validator.Validate
var socket *socketio.Server
// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionStore: sessions.Null{},
PreWares: []buffalo.PreWare{
cors.Default().Handler,
},
SessionName: "_lg_session",
})
// If no content type is sent by the client
// the application/json will be set, otherwise the client's
// content type will be used.
app.Use(contenttype.Add("application/json"))
// validator
validate = validator.New()
if ENV == "development" {
app.Use(paramlogger.ParameterLogger)
}
// socket.io initiation
sio, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
socket = sio // moves it into global scope
// Routes
// swagger ui
app.ServeFiles("/swagger", http.Dir("swagger"))
app.GET("/about", AboutHandler)
app.Muxer().Handle("/socket.io/", sio) // handles the socket io
api := app.Group("/api")
{
api.Resource("/instances", InstancesResource{})
}
}
return app
}