-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.go
More file actions
220 lines (182 loc) · 7.93 KB
/
platform.go
File metadata and controls
220 lines (182 loc) · 7.93 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// ██████╗ ██████╗ ██████╗ ███████╗██╗ ██╗███████╗██████╗ ███████╗████████╗
// ██╔════╝██╔═══██╗██╔══██╗██╔════╝██║ ██║██╔════╝██╔══██╗██╔════╝╚══██╔══╝
// ██║ ██║ ██║██║ ██║█████╗ ██║ █╗ ██║█████╗ ██████╔╝█████╗ ██║
// ██║ ██║ ██║██║ ██║██╔══╝ ██║███╗██║██╔══╝ ██╔══██╗██╔══╝ ██║
// ╚██████╗╚██████╔╝██████╔╝███████╗╚███╔███╔╝███████╗██║ ██║██║ ██║
// ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚══╝╚══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
//
// Copyright 2015 Codewerft UG (http://www.codewerft.net).
// All rights reserved.
package platform
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"path"
"path/filepath"
gcfg "gopkg.in/gcfg.v1"
"github.com/attilaolah/strict"
"github.com/martini-contrib/binding"
"github.com/oweidner/platform/apiserver"
"github.com/oweidner/platform/apiserver/authentication"
"github.com/oweidner/platform/config"
"github.com/oweidner/platform/database"
"github.com/oweidner/platform/logging"
)
var (
// ServerVersion holds the version of the platform server.
// Set externally via -ldflags "-X platform.ServerVersion x.y.z"
ServerVersion string
// APIVersion holds the version of the API.
// Set externally via -ldflags "-X platform.APIVersion x.y.z"
APIVersion string
)
// Platform represents the application.
type Platform struct {
Config *config.Config
Server *apiserver.Server
}
var cf config.Configuration
var ds database.Datastore
// New creates a bare bones Platform instance.
func New(configFile *string) *Platform {
// get the base path of the configuration file
configFileAbs, absErr := filepath.Abs(*configFile)
if absErr != nil {
// file doesn't exist
logging.Log.Fatal(fmt.Sprintf("Couldn't open configuration file %v", absErr))
}
basePath := path.Dir(configFileAbs)
var cfg config.Config
// Read the configuration.
err := gcfg.ReadFileInto(&cfg, configFileAbs)
if err != nil {
logging.Log.Fatalf("Error reading configuration file: %v", err)
}
// Check configuration semantics
err = config.CheckConfig(&cfg, configFileAbs, basePath)
if err != nil {
logging.Log.Fatal(err)
}
// Create the root account credentials from the username and password
// values defined in the config file.
// rootAccount := accounts.Account{}
// pwdHash1, _ := bcrypt.GenerateFromPassword([]byte(cfg.SERVER.AdminPassword), 0)
// rootAccount = accounts.Account{
// ID: int64(-1),
// Firstname: null.StringFrom("Root"),
// Lastname: null.StringFrom("Admin Account"),
// ContactEmail: null.StringFrom("root"),
// Username: null.StringFrom(cfg.SERVER.AdminAccount),
// Password: string(pwdHash1)}
// Load the JWT __PRIVATE__ key from the path / filename defined in
// the config file.
jwtPrivateKey, err1 := ioutil.ReadFile(cfg.JWT.PrivateKey)
if err1 != nil {
logging.Log.Fatal(fmt.Sprintf("Error reading private key: %v", err1))
}
logging.Log.Info(fmt.Sprintf("Loaded JWT private key from %v", cfg.JWT.PrivateKey))
// Load the JWT __PUBLIC__ key from the path / filename defined in
// the config file.
jwtPublicKey, err2 := ioutil.ReadFile(cfg.JWT.PublicKey)
if err2 != nil {
logging.Log.Fatal(fmt.Sprintf("Error reading public key: %v", err2))
}
logging.Log.Info(fmt.Sprintf("Loaded JWT public key from %v", cfg.JWT.PublicKey))
// Instantiate the storage database backend with the values defined
// in the config file.
ds = database.NewDefaultDatastore(cfg.MySQL.Host, cfg.MySQL.Database, cfg.MySQL.Username, cfg.MySQL.Password)
defer ds.Close()
cf = config.NewServerConfiguration(cfg)
// Finally, we start up the Platform API server and inject the storage
// and authentication backend instances.
server := apiserver.New(ds, cf, cfg.SERVER.PlatformPrefix,
!cfg.SERVER.DisableAuth, cfg.SERVER.EnablePlatformAPI,
jwtPrivateKey, jwtPublicKey, cfg.JWT.Expiration)
// Return the Platform handle.
return &Platform{Config: &cfg, Server: server}
}
// UnitTestServe launches the Platform HTTPS server for running the unit tests.
func (p *Platform) UnitTestServe() (*httptest.Server, error) {
server := httptest.NewServer(p.Server.Martini)
logging.Log.Info("Codewerft Platform unit test server running on %s", server.URL)
return server, nil
}
func (p *Platform) AddGORPTable(tableName string, indexName string, relType interface{}) error {
ds.GetDBMap().AddTableWithName(relType, tableName).SetKeys(true, indexName)
return nil
}
// Get adds a new HTTP GET resource to the application API
func (p *Platform) Get(path string, handleFunc interface{}) error {
if p.Config.SERVER.EnableApplicationAPI == false {
return nil
}
p.Server.Router.Get(path,
strict.Accept("application/json", "text/html"),
authentication.JWTAuth(p.Server.JWTConfig, nil),
handleFunc)
return nil
}
// Post adds a new HTTP POST resource to the application API
func (p *Platform) Post(path string, handleFunc interface{}, requestType interface{}) error {
if p.Config.SERVER.EnableApplicationAPI == false {
return nil
}
p.Server.Router.Post(path,
strict.Accept("application/json", "text/html"),
binding.Bind(requestType),
authentication.JWTAuth(p.Server.JWTConfig, nil),
handleFunc)
return nil
}
// Put adds a new HTTP PUT resource to the application API
func (p *Platform) Put(path string, handleFunc interface{}, requestType interface{}) error {
if p.Config.SERVER.EnableApplicationAPI == false {
return nil
}
p.Server.Router.Put(path,
strict.Accept("application/json", "text/html"),
binding.Bind(requestType),
authentication.JWTAuth(p.Server.JWTConfig, nil),
handleFunc)
return nil
}
// Delete adds a new HTTP DELETE resource to the application API
func (p *Platform) Delete(path string, handleFunc interface{}) error {
if p.Config.SERVER.EnableApplicationAPI == false {
return nil
}
p.Server.Router.Delete(path,
strict.Accept("application/json", "text/html"),
authentication.JWTAuth(p.Server.JWTConfig, nil),
handleFunc)
return nil
}
// Serve launches the Platform HTTP(S) server.
func (p *Platform) Serve() error {
p.Server.Martini.Action(p.Server.Router.Handle)
// if TLS is enable in the configuration file, we start
// an HTTPS server with the provided X.509 certificates,
// otherwise, start an HTTP server.without TLS.
if p.Config.TLS.EnableTLS == true {
logging.Log.Info(fmt.Sprintf("HTTPS/TLS enabled. Using X.509 keypair %v and %v", p.Config.TLS.CertFile, p.Config.TLS.KeyFile))
logging.Log.Info("Codewerft Platform server available at https://localhost%v", p.Config.SERVER.Listen)
if err := http.ListenAndServeTLS(
p.Config.SERVER.Listen,
p.Config.TLS.CertFile,
p.Config.TLS.KeyFile,
p.Server.Martini); err != nil {
logging.Log.Fatalf("Error starting Codewerft Platform server: %v", err)
}
} else {
logging.Log.Warning("***********************************************************************************")
logging.Log.Warning("!! HTTPS/TLS DISABLED -- DO NOT RUN THIS SERVER IN A PRODUCTION ENVIRONMENT !!")
logging.Log.Warning("***********************************************************************************")
logging.Log.Info("Codewerft Platform server available at http://localhost%v", p.Config.SERVER.Listen)
if err := http.ListenAndServe(p.Config.SERVER.Listen, p.Server.Martini); err != nil {
logging.Log.Fatal(fmt.Sprintf("Error starting Codewerft Platform server: %v", err))
}
}
return nil
}