-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
132 lines (115 loc) · 4.69 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
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
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"http"
"os"
"url"
"github.com/codeforsystemsbiology/rest.go"
"github.com/codeforsystemsbiology/verboselogger.go"
"goconf.googlecode.com/hg"
"launchpad.net/mgo"
)
var logger *log4go.VerboseLogger
var configFile *conf.ConfigFile
// starts HTTP server with REST resources for configured domains
func main() {
InitConfigFile()
InitLogger()
StartREST()
StartHtmlHandler()
ListenAndServe()
}
// sets up global config file
// looks for "restjsonmgo.config" in execution PATH
func InitConfigFile() {
if cf, err := conf.ReadConfigFile("restjsonmgo.config"); err != nil {
logger.Fatal(err.String())
} else {
configFile = cf
}
}
// sets global logger based on verbosity level in configuration
// optional parameter: default.verbose (defaults to true if not present or incorrectly set)
func InitLogger() {
verbose, err := configFile.GetBool("default", "verbose")
logger = log4go.NewVerboseLogger(verbose, nil, "")
if err != nil {
logger.Warn(err)
verbose = true
}
logger.Printf("verbose set [%v]", verbose)
if verbose {
mgo.SetLogger(logger)
mgo.SetDebug(verbose)
}
}
// starts service based on the given configuration file
// required parameters: sections for each domain object accepted (e.g. jobs for /jobs)
// optional parameters: [domain_group] contentType=application/json (default)
func StartREST() {
domains := configFile.GetSections()
for _, domain := range domains {
if domain != "default" {
dbhost := GetRequiredString(configFile, domain, "dbHost")
dbstore := GetRequiredString(configFile, domain, "dbName")
jsonParameter := GetRequiredString(configFile, domain, "jsonParameter")
proxyTarget, err := configFile.GetString(domain, "serviceProxy")
if err != nil {
logger.Warn(err)
logger.Print("no service proxy configured")
}
logger.Debug("starting REST: Domain [%v]", domain)
logger.Debug("starting REST: MongoDB at [%v,%v]", dbhost, dbstore)
logger.Debug("starting REST: JSON Param [%v]", jsonParameter)
logger.Debug("starting REST: PostCommit Hook [%v]", proxyTarget)
targetUrl, _ := url.Parse(proxyTarget)
store := &JsonStore{Domain: domain, Host: dbhost, Database: dbstore}
rest.Resource(domain, RestJsonMongo{Store: store, Target: targetUrl, JsonParam: jsonParameter})
contentType, err := configFile.GetString(domain, "contentType")
if err != nil {
logger.Warn(err)
contentType = "application/json"
}
logger.Debug("starting REST: Content Type [%v]", contentType)
rest.ResourceContentType(domain, contentType)
}
}
}
// starts http handlers for HTML content based on the given configuration file
// optional parameters: default.contentDirectory (location of html content to be served at https://example.com/ or https://example.com/html/index.html
func StartHtmlHandler() {
if contentDir, _ := configFile.GetString("default", "contentDirectory"); contentDir != "" {
logger.Printf("StartHtmlHandler(): serving HTML content from [%v]", contentDir)
http.Handle("/html/", http.StripPrefix("/html/", http.FileServer(http.Dir(contentDir))))
http.Handle("/", http.RedirectHandler("/html/", http.StatusTemporaryRedirect))
}
}
// starts HTTP server based on hostname in configuration file
func ListenAndServe() {
hostname := GetRequiredString(configFile, "default", "hostname")
logger.Debug("ListenAndServe():%v", hostname)
if err := http.ListenAndServe(hostname, nil); err != nil {
logger.Fatal(err.String())
}
}
func GetRequiredString(config *conf.ConfigFile, section string, key string) (value string) {
var err os.Error
if value, err = config.GetString(section, key); err != nil {
logger.Fatalf("[CONFIG] [%v] required in section [%v]", key, section)
}
return value
}