Mercurial > gemma
comparison pkg/controllers/report.go @ 5490:5f47eeea988d logging
Use own logging package.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 20 Sep 2021 17:45:39 +0200 |
parents | 699048c86848 |
children | bbc257dd9abf |
comparison
equal
deleted
inserted
replaced
5488:a726a92ea5c9 | 5490:5f47eeea988d |
---|---|
14 package controllers | 14 package controllers |
15 | 15 |
16 import ( | 16 import ( |
17 "database/sql" | 17 "database/sql" |
18 "fmt" | 18 "fmt" |
19 "log" | |
20 "net/http" | 19 "net/http" |
21 "os" | 20 "os" |
22 "path/filepath" | 21 "path/filepath" |
23 "sort" | 22 "sort" |
24 "strings" | 23 "strings" |
25 | 24 |
26 "gemma.intevation.de/gemma/pkg/config" | 25 "gemma.intevation.de/gemma/pkg/config" |
27 "gemma.intevation.de/gemma/pkg/middleware" | 26 "gemma.intevation.de/gemma/pkg/middleware" |
28 "gemma.intevation.de/gemma/pkg/xlsx" | 27 "gemma.intevation.de/gemma/pkg/xlsx" |
29 | 28 |
29 "gemma.intevation.de/gemma/pkg/log" | |
30 mw "gemma.intevation.de/gemma/pkg/middleware" | 30 mw "gemma.intevation.de/gemma/pkg/middleware" |
31 | 31 |
32 "github.com/gorilla/mux" | 32 "github.com/gorilla/mux" |
33 "github.com/xuri/excelize/v2" | 33 "github.com/xuri/excelize/v2" |
34 ) | 34 ) |
45 | 45 |
46 // This would be easier with Go 1.16+. | 46 // This would be easier with Go 1.16+. |
47 | 47 |
48 dir, err := os.Open(path) | 48 dir, err := os.Open(path) |
49 if err != nil { | 49 if err != nil { |
50 log.Printf("error: %v\n", err) | 50 log.Errorf("%v\n", err) |
51 err = mw.JSONError{ | 51 err = mw.JSONError{ |
52 Code: http.StatusInternalServerError, | 52 Code: http.StatusInternalServerError, |
53 Message: "Listing report templates failed.", | 53 Message: "Listing report templates failed.", |
54 } | 54 } |
55 return | 55 return |
56 } | 56 } |
57 defer dir.Close() | 57 defer dir.Close() |
58 files, err := dir.Readdirnames(-1) | 58 files, err := dir.Readdirnames(-1) |
59 if err != nil { | 59 if err != nil { |
60 log.Printf("error: %v\n", err) | 60 log.Errorf("%v\n", err) |
61 err = mw.JSONError{ | 61 err = mw.JSONError{ |
62 Code: http.StatusInternalServerError, | 62 Code: http.StatusInternalServerError, |
63 Message: "Listing report templates failed.", | 63 Message: "Listing report templates failed.", |
64 } | 64 } |
65 return | 65 return |
108 return | 108 return |
109 } | 109 } |
110 | 110 |
111 if stat, err := os.Stat(path); err != nil { | 111 if stat, err := os.Stat(path); err != nil { |
112 if os.IsNotExist(err) { | 112 if os.IsNotExist(err) { |
113 log.Printf("error: report dir '%s' does not exists.\n", path) | 113 log.Errorf("report dir '%s' does not exists.\n", path) |
114 http.NotFound(rw, req) | 114 http.NotFound(rw, req) |
115 } else { | 115 } else { |
116 log.Printf("error: %v\n", err) | 116 log.Errorf("%v\n", err) |
117 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | 117 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
118 } | 118 } |
119 return | 119 return |
120 } else if !stat.Mode().IsDir() { | 120 } else if !stat.Mode().IsDir() { |
121 log.Printf("error: report dir '%s' is not a directory.\n", path) | 121 log.Errorf("report dir '%s' is not a directory.\n", path) |
122 http.NotFound(rw, req) | 122 http.NotFound(rw, req) |
123 return | 123 return |
124 } | 124 } |
125 | 125 |
126 vars := mux.Vars(req) | 126 vars := mux.Vars(req) |
132 for _, check := range []string{xlsxFilename, yamlFilename} { | 132 for _, check := range []string{xlsxFilename, yamlFilename} { |
133 if _, err := os.Stat(check); err != nil { | 133 if _, err := os.Stat(check); err != nil { |
134 if os.IsNotExist(err) { | 134 if os.IsNotExist(err) { |
135 http.NotFound(rw, req) | 135 http.NotFound(rw, req) |
136 } else { | 136 } else { |
137 log.Printf("error: %v\n", err) | 137 log.Errorf("%v\n", err) |
138 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | 138 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
139 } | 139 } |
140 return | 140 return |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 template, err := excelize.OpenFile(xlsxFilename) | 144 template, err := excelize.OpenFile(xlsxFilename) |
145 if err != nil { | 145 if err != nil { |
146 log.Printf("error: %v\n", err) | 146 log.Errorf("%v\n", err) |
147 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | 147 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
148 return | 148 return |
149 } | 149 } |
150 | 150 |
151 action, err := xlsx.ActionFromFile(yamlFilename) | 151 action, err := xlsx.ActionFromFile(yamlFilename) |
152 if err != nil { | 152 if err != nil { |
153 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | 153 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
154 log.Printf("error: %v\n", err) | 154 log.Errorf("%v\n", err) |
155 return | 155 return |
156 } | 156 } |
157 | 157 |
158 ctx := req.Context() | 158 ctx := req.Context() |
159 conn := middleware.GetDBConn(req) | 159 conn := middleware.GetDBConn(req) |
160 | 160 |
161 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) | 161 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) |
162 defer tx.Rollback() | 162 defer tx.Rollback() |
163 | 163 |
164 if err := action.Execute(ctx, tx, template); err != nil { | 164 if err := action.Execute(ctx, tx, template); err != nil { |
165 log.Printf("error: %v\n", err) | 165 log.Errorf("%v\n", err) |
166 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) | 166 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
167 return | 167 return |
168 } | 168 } |
169 rw.Header().Set( | 169 rw.Header().Set( |
170 "Content-Disposition", | 170 "Content-Disposition", |
172 rw.Header().Set( | 172 rw.Header().Set( |
173 "Content-Type", | 173 "Content-Type", |
174 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") | 174 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
175 | 175 |
176 if _, err := template.WriteTo(rw); err != nil { | 176 if _, err := template.WriteTo(rw); err != nil { |
177 log.Printf("error: %v\n", err) | 177 log.Errorf("%v\n", err) |
178 } | 178 } |
179 } | 179 } |