This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathipe.go
106 lines (88 loc) · 2.31 KB
/
ipe.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package ipe
import (
"io/ioutil"
"math/rand"
"net/http"
"os"
"time"
log "github.com/golang/glog"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
"ipe/api"
"ipe/app"
"ipe/config"
"ipe/storage"
"ipe/websockets"
)
// Start Parse the configuration file and starts the ipe server
// It Panic if could not start the HTTP or HTTPS server
func Start(filename string) {
var conf config.File
rand.Seed(time.Now().Unix())
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Error(err)
return
}
// Expand env vars
data = []byte(os.ExpandEnv(string(data)))
// Decoding config
if err := yaml.UnmarshalStrict(data, &conf); err != nil {
log.Error(err)
return
}
// Using a in memory database
inMemoryStorage := storage.NewInMemory()
// Adding applications
for _, a := range conf.Apps {
application := app.NewApplication(
a.Name,
a.AppID,
a.Key,
a.Secret,
a.OnlySSL,
a.Enabled,
a.UserEvents,
a.WebHooks.Enabled,
a.WebHooks.URL,
)
if err := inMemoryStorage.AddApp(application); err != nil {
log.Error(err)
return
}
}
router := mux.NewRouter()
router.Use(handlers.RecoveryHandler())
router.Path("/app/{key}").Methods("GET").Handler(
websockets.NewWebsocket(inMemoryStorage),
)
appsRouter := router.PathPrefix("/apps/{app_id}").Subrouter()
appsRouter.Use(
api.CheckAppDisabled(inMemoryStorage),
api.Authentication(inMemoryStorage),
)
appsRouter.Path("/events").Methods("POST").Handler(
api.NewPostEvents(inMemoryStorage),
)
appsRouter.Path("/channels").Methods("GET").Handler(
api.NewGetChannels(inMemoryStorage),
)
appsRouter.Path("/channels/{channel_name}").Methods("GET").Handler(
api.NewGetChannel(inMemoryStorage),
)
appsRouter.Path("/channels/{channel_name}/users").Methods("GET").Handler(
api.NewGetChannelUsers(inMemoryStorage),
)
if conf.SSL.Enabled {
go func() {
log.Infof("Starting HTTPS service on %s ...", conf.SSL.Host)
log.Fatal(http.ListenAndServeTLS(conf.SSL.Host, conf.SSL.CertFile, conf.SSL.KeyFile, router))
}()
}
log.Infof("Starting HTTP service on %s ...", conf.Host)
log.Fatal(http.ListenAndServe(conf.Host, router))
}