Skip to content

Commit 4cacdc1

Browse files
all error to error log, improved conf print
1 parent 47d0b12 commit 4cacdc1

File tree

6 files changed

+56
-47
lines changed

6 files changed

+56
-47
lines changed

logger/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (l *Logger) Info(log string, message string) {
6767
return
6868
}
6969

70-
func (l *Logger) Error(log string, message string) {
71-
l.Log(log, l4g.ERROR, message)
70+
func (l *Logger) Error(message string) {
71+
l.Log("error", l4g.ERROR, message)
7272
return
7373
}
7474

shock-server/nodeController.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func handleAuthError(err error, cx *goweb.Context) {
2727
cx.RespondWithErrorMessage("Invalid Authorization header", http.StatusBadRequest)
2828
return
2929
}
30-
fmt.Println("Error at Auth:", err.Error())
30+
log.Error("Error at Auth: " + err.Error())
3131
cx.RespondWithError(http.StatusInternalServerError)
3232
return
3333
}
@@ -61,7 +61,7 @@ func (cr *NodeController) Create(cx *goweb.Context) {
6161
if err.Error() == "request Content-Type isn't multipart/form-data" {
6262
node, err := store.CreateNodeUpload(u, params, files)
6363
if err != nil {
64-
fmt.Println("Error at create empty:", err.Error())
64+
log.Error("Error at create empty: " + err.Error())
6565
cx.RespondWithError(http.StatusInternalServerError)
6666
return
6767
}
@@ -78,15 +78,15 @@ func (cr *NodeController) Create(cx *goweb.Context) {
7878
// Some error other than request encoding. Theoretically
7979
// could be a lost db connection between user lookup and parsing.
8080
// Blame the user, Its probaby their fault anyway.
81-
fmt.Println("Error parsing form:", err.Error())
81+
log.Error("Error parsing form: " + err.Error())
8282
cx.RespondWithError(http.StatusBadRequest)
8383
return
8484
}
8585
}
8686
// Create node
8787
node, err := store.CreateNodeUpload(u, params, files)
8888
if err != nil {
89-
fmt.Println("err", err.Error())
89+
log.Error("err " + err.Error())
9090
cx.RespondWithError(http.StatusBadRequest)
9191
return
9292
}
@@ -141,7 +141,6 @@ func (cr *NodeController) Read(id string, cx *goweb.Context) {
141141
node, err := store.LoadNode(id, u.Uuid)
142142
if err != nil {
143143
if err.Error() == e.UnAuth {
144-
fmt.Println("Unauthorized")
145144
cx.RespondWithError(http.StatusUnauthorized)
146145
return
147146
} else if err.Error() == e.MongoDocNotFound {
@@ -150,7 +149,7 @@ func (cr *NodeController) Read(id string, cx *goweb.Context) {
150149
} else {
151150
// In theory the db connection could be lost between
152151
// checking user and load but seems unlikely.
153-
fmt.Println("Err@node_Read:LoadNode:", err.Error())
152+
log.Error("Err@node_Read:LoadNode: " + err.Error())
154153
cx.RespondWithError(http.StatusInternalServerError)
155154
return
156155
}
@@ -176,7 +175,7 @@ func (cr *NodeController) Read(id string, cx *goweb.Context) {
176175
r, err := os.Open(node.FilePath())
177176
defer r.Close()
178177
if err != nil {
179-
fmt.Println("Err@node_Read:Open:", err.Error())
178+
log.Error("Err@node_Read:Open: " + err.Error())
180179
cx.RespondWithError(http.StatusInternalServerError)
181180
return
182181
}
@@ -212,22 +211,22 @@ func (cr *NodeController) Read(id string, cx *goweb.Context) {
212211
err = s.stream()
213212
if err != nil {
214213
// fix
215-
fmt.Println("err", err.Error())
214+
log.Error("err: " + err.Error())
216215
}
217216
} else {
218217
nf, err := os.Open(node.FilePath())
219218
if err != nil {
220219
// File not found or some sort of file read error.
221220
// Probably deserves more checking
222-
fmt.Println("err", err.Error())
221+
log.Error("err " + err.Error())
223222
cx.RespondWithError(http.StatusBadRequest)
224223
return
225224
}
226225
s := &streamer{rs: []io.ReadCloser{nf}, ws: cx.ResponseWriter, contentType: "application/octet-stream", filename: node.Id, size: node.File.Size, filter: fFunc}
227226
err = s.stream()
228227
if err != nil {
229228
// fix
230-
fmt.Println("err", err.Error())
229+
log.Error("err " + err.Error())
231230
}
232231
}
233232
return
@@ -303,15 +302,15 @@ func (cr *NodeController) ReadMany(cx *goweb.Context) {
303302
// Get nodes from db
304303
err := nodes.GetAllLimitOffset(q, lim, off)
305304
if err != nil {
306-
fmt.Println("err", err.Error())
305+
log.Error("err " + err.Error())
307306
cx.RespondWithError(http.StatusBadRequest)
308307
return
309308
}
310309
} else {
311310
// Get nodes from db
312311
err := nodes.GetAll(q)
313312
if err != nil {
314-
fmt.Println("err", err.Error())
313+
log.Error("err " + err.Error())
315314
cx.RespondWithError(http.StatusBadRequest)
316315
return
317316
}
@@ -342,7 +341,6 @@ func (cr *NodeController) Update(id string, cx *goweb.Context) {
342341
node, err := store.LoadNode(id, u.Uuid)
343342
if err != nil {
344343
if err.Error() == e.UnAuth {
345-
fmt.Println("Unauthorized")
346344
cx.RespondWithError(http.StatusUnauthorized)
347345
return
348346
} else if err.Error() == e.MongoDocNotFound {
@@ -351,7 +349,7 @@ func (cr *NodeController) Update(id string, cx *goweb.Context) {
351349
} else {
352350
// In theory the db connection could be lost between
353351
// checking user and load but seems unlikely.
354-
fmt.Println("Err@node_Update:LoadNode:", err.Error())
352+
log.Error("Err@node_Update:LoadNode: " + err.Error())
355353
cx.RespondWithError(http.StatusInternalServerError)
356354
return
357355
}
@@ -368,7 +366,7 @@ func (cr *NodeController) Update(id string, cx *goweb.Context) {
368366
idxer := newIndexer(f)
369367
err := idxer.Create()
370368
if err != nil {
371-
fmt.Println(err.Error())
369+
log.Error("err " + err.Error())
372370
}
373371
err = idxer.Dump(node.IndexPath() + "/record")
374372
if err != nil {
@@ -381,7 +379,7 @@ func (cr *NodeController) Update(id string, cx *goweb.Context) {
381379
} else {
382380
params, files, err := ParseMultipartForm(cx.Request)
383381
if err != nil {
384-
fmt.Println("err", err.Error())
382+
log.Error("err " + err.Error())
385383
cx.RespondWithError(http.StatusBadRequest)
386384
return
387385
}
@@ -395,7 +393,7 @@ func (cr *NodeController) Update(id string, cx *goweb.Context) {
395393
return
396394
}
397395
}
398-
fmt.Println("err", err.Error())
396+
log.Error("err " + err.Error())
399397
cx.RespondWithError(http.StatusBadRequest)
400398
return
401399
}

shock-server/shock-server.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func launchSite(control chan int, port int) {
2121
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", conf.SITE_PORT), r)
2222
if err != nil {
2323
fmt.Fprintf(os.Stderr, "ERROR: site: %v\n", err)
24+
log.Error("ERROR: site: " + err.Error())
2425
}
2526
control <- 1 //we are ending
2627
}
@@ -34,28 +35,19 @@ func launchAPI(control chan int, port int) {
3435
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", port), r)
3536
if err != nil {
3637
fmt.Fprintf(os.Stderr, "ERROR: api: %v\n", err)
38+
log.Error("ERROR: api: " + err.Error())
3739
}
3840
control <- 1 //we are ending
3941
}
4042

4143
func main() {
42-
fmt.Printf("%s\n######### Conf #########\ndata-root:\t%s\naccess-log:\t%s\nerror-log:\t%s\nmongodb:\t%s\nsecretkey:\t%s\nsite-port:\t%d\napi-port:\t%d\n\n####### Anonymous ######\nread:\t%t\nwrite:\t%t\ncreate-user:\t%t\n\n",
43-
logo,
44-
conf.DATA_PATH,
45-
conf.LOGS_PATH+"/access.log",
46-
conf.LOGS_PATH+"/error.log",
47-
conf.MONGODB,
48-
conf.SECRET_KEY,
49-
conf.SITE_PORT,
50-
conf.API_PORT,
51-
conf.ANON_READ,
52-
conf.ANON_WRITE,
53-
conf.ANON_CREATEUSER,
54-
)
44+
printLogo()
45+
printConf()
5546

5647
if _, err := os.Stat(conf.DATA_PATH + "/temp"); err != nil && os.IsNotExist(err) {
5748
if err := os.Mkdir(conf.DATA_PATH+"/temp", 0777); err != nil {
5849
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
50+
log.Error("ERROR: " + err.Error())
5951
}
6052
}
6153

@@ -65,6 +57,7 @@ func main() {
6557
err := reload(conf.RELOAD)
6658
if err != nil {
6759
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
60+
log.Error("ERROR: " + err.Error())
6861
}
6962
fmt.Println("Done")
7063
}

shock-server/userController.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ func (cr *UserController) Create(cx *goweb.Context) {
5959
if err != nil {
6060
// Duplicate key check
6161
if e.MongoDupKeyRegex.MatchString(err.Error()) {
62-
fmt.Println("Err@user_Create: duplicate key error")
62+
log.Error("Err@user_Create: duplicate key error")
6363
cx.RespondWithErrorMessage("Username not available", http.StatusBadRequest)
6464
return
6565
} else {
66-
fmt.Println("Err@user_Create:", err.Error())
66+
log.Error("Err@user_Create: " + err.Error())
6767
cx.RespondWithError(http.StatusInternalServerError)
6868
return
6969
}
@@ -94,7 +94,7 @@ func (cr *UserController) Read(id string, cx *goweb.Context) {
9494
cx.RespondWithError(http.StatusUnauthorized)
9595
return
9696
} else {
97-
fmt.Println("Err@user_Read:", err.Error())
97+
log.Error("Err@user_Read: " + err.Error())
9898
cx.RespondWithError(http.StatusInternalServerError)
9999
return
100100
}
@@ -111,7 +111,7 @@ func (cr *UserController) Read(id string, cx *goweb.Context) {
111111
cx.RespondWithNotFound()
112112
return
113113
} else {
114-
fmt.Println("Err@user_Read:Admin:", err.Error())
114+
log.Error("Err@user_Read:Admin: " + err.Error())
115115
cx.RespondWithError(http.StatusInternalServerError)
116116
return
117117
}
@@ -136,7 +136,7 @@ func (cr *UserController) ReadMany(cx *goweb.Context) {
136136
cx.RespondWithError(http.StatusUnauthorized)
137137
return
138138
} else {
139-
fmt.Println("Err@user_Read:", err.Error())
139+
log.Error("Err@user_Read: " + err.Error())
140140
cx.RespondWithError(http.StatusInternalServerError)
141141
return
142142
}

shock-server/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ var (
3737
" +-------------+ +----+ +----+ +--------------+ +--------------+ +----+ +----+\n"
3838
)
3939

40+
func printLogo() {
41+
fmt.Println(logo)
42+
return
43+
}
44+
45+
func printConf() {
46+
fmt.Printf("##### Admin #####\nemail:\t%s\nsecretkey:\t%s\n\n", conf.ADMIN_EMAIL, conf.SECRET_KEY)
47+
fmt.Printf("####### Anonymous ######\nread:\t%t\nwrite:\t%t\ncreate-user:\t%t\n\n", conf.ANON_READ, conf.ANON_WRITE, conf.ANON_CREATEUSER)
48+
if conf.AUTH_TYPE == "basic" {
49+
fmt.Printf("##### Auth #####\ntype:\tbasic\n\n")
50+
} else if conf.AUTH_TYPE == "globus" {
51+
fmt.Printf("##### Auth #####\ntype:\tglobus\ntoken_url:\t%s\nprofile_url:\t%s\n\n", conf.GLOBUS_TOKEN_URL, conf.GLOBUS_PROFILE_URL)
52+
}
53+
fmt.Printf("##### Directories #####\nsite:\t%s\ndata:\t%s\nlogs:\t%s\n\n", conf.SITE_PATH, conf.DATA_PATH, conf.LOGS_PATH)
54+
fmt.Printf("##### Mongodb #####\nhost(s):\t%s\n\n", conf.MONGODB)
55+
fmt.Printf("##### Ports #####\nsite:\t%d\napi:\t%d\n\n", conf.SITE_PORT, conf.API_PORT)
56+
}
57+
4058
type Query struct {
4159
list map[string][]string
4260
}

shock.cfg.template

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
[Admin]
2+
3+
secretkey=supersecretkey
4+
15
[Anonymous]
26
# Controls an anonymous user's ability to read/write
37
# values: true/false
48
read=true
59
write=false
610
create-user=false
711

8-
[Ports]
9-
# Ports for site/api
10-
# Note: use of port 80 may require root access
11-
site-port=80
12-
api-port=8000
13-
1412
[Auth]
1513
# defaults to local user management with basis auth
1614
type=basic
@@ -19,10 +17,6 @@ type=basic
1917
#globus_token_url=https://nexus.api.globusonline.org/goauth/token?grant_type=client_credentials
2018
#globus_profile_url=https://nexus.api.globusonline.org/users
2119

22-
[Admin]
23-
24-
secretkey=supersecretkey
25-
2620
[Directories]
2721
# See documentation for details of deploying Shock
2822
site=/usr/local/shock/site
@@ -38,3 +32,9 @@ hosts=localhost
3832
# See http://www.mongodb.org/display/DOCS/Indexes#Indexes-CreationOptions for more info on mongodb index options.
3933
# key=unique:true/false[,dropDups:true/false][,sparse:true/false]
4034
id=unique:true
35+
36+
[Ports]
37+
# Ports for site/api
38+
# Note: use of port 80 may require root access
39+
site-port=80
40+
api-port=8000

0 commit comments

Comments
 (0)