Mercurial > gemma
annotate pkg/controllers/report.go @ 5612:a9bcaac49a7e erdms2
Beware of nil interfaces.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 07 Nov 2022 21:03:32 +0100 |
parents | 847de131893a |
children | 2dd155cc95ec |
rev | line source |
---|---|
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2021 by via donau |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
5333
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
17 "database/sql" |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "fmt" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 "net/http" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "os" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "path/filepath" |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
22 "sort" |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
23 "strings" |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "gemma.intevation.de/gemma/pkg/config" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 "gemma.intevation.de/gemma/pkg/xlsx" |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
28 "gemma.intevation.de/gemma/pkg/log" |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
29 mw "gemma.intevation.de/gemma/pkg/middleware" |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
30 |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 "github.com/gorilla/mux" |
5478
699048c86848
Updated 3rd-party libs of the gemma server to the latest bug fix release.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5333
diff
changeset
|
32 "github.com/xuri/excelize/v2" |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 ) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
35 func listReports(req *http.Request) (jr mw.JSONResult, err error) { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
36 path := config.ReportPath() |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
37 if path == "" { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
38 err = mw.JSONError{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
39 Code: http.StatusNotFound, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
40 Message: http.StatusText(http.StatusNotFound), |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
41 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
42 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
43 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
44 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
45 // This would be easier with Go 1.16+. |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
46 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
47 dir, err := os.Open(path) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
48 if err != nil { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
49 log.Errorf("%v\n", err) |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
50 err = mw.JSONError{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
51 Code: http.StatusInternalServerError, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
52 Message: "Listing report templates failed.", |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
53 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
54 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
55 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
56 defer dir.Close() |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
57 files, err := dir.Readdirnames(-1) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
58 if err != nil { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
59 log.Errorf("%v\n", err) |
5324
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
60 err = mw.JSONError{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
61 Code: http.StatusInternalServerError, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
62 Message: "Listing report templates failed.", |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
63 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
64 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
65 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
66 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
67 pairs := map[string]int{} |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
68 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
69 all: |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
70 for _, file := range files { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
71 var mask int |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
72 switch { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
73 case strings.HasSuffix(file, ".xlsx"): |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
74 mask = 1 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
75 case strings.HasSuffix(file, ".yaml"): |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
76 mask = 2 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
77 default: |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
78 continue all |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
79 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
80 basename := filepath.Base(file) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
81 name := strings.TrimSuffix(basename, filepath.Ext(basename)) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
82 pairs[name] |= mask |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
83 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
84 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
85 var reports []string |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
86 for name, mask := range pairs { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
87 if mask == 3 { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
88 reports = append(reports, name) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
89 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
90 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
91 sort.Strings(reports) |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
92 |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
93 out := struct { |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
94 Reports []string `json:"reports"` |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
95 }{ |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
96 Reports: reports, |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
97 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
98 jr = mw.JSONResult{Result: out} |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
99 return |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
100 } |
348d91848278
Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5322
diff
changeset
|
101 |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 func report(rw http.ResponseWriter, req *http.Request) { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 path := config.ReportPath() |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 if path == "" { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 http.NotFound(rw, req) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
110 if stat, err := os.Stat(path); err != nil { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
111 if os.IsNotExist(err) { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
112 log.Errorf("report dir '%s' does not exists.\n", path) |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
113 http.NotFound(rw, req) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
114 } else { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
115 log.Errorf("%v\n", err) |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
116 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
117 } |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
118 return |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
119 } else if !stat.Mode().IsDir() { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
120 log.Errorf("report dir '%s' is not a directory.\n", path) |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
121 http.NotFound(rw, req) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
123 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
125 vars := mux.Vars(req) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
126 name := vars["name"] |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
127 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 xlsxFilename := filepath.Join(path, name+".xlsx") |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 yamlFilename := filepath.Join(path, name+".yaml") |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
130 |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
131 for _, check := range []string{xlsxFilename, yamlFilename} { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
132 if _, err := os.Stat(check); err != nil { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
133 if os.IsNotExist(err) { |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
134 http.NotFound(rw, req) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
135 } else { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
136 log.Errorf("%v\n", err) |
5322
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
137 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
138 } |
80d9fd782f00
Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5321
diff
changeset
|
139 return |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
140 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
141 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
142 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
143 template, err := excelize.OpenFile(xlsxFilename) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
144 if err != nil { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
145 log.Errorf("%v\n", err) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 action, err := xlsx.ActionFromFile(yamlFilename) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 if err != nil { |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
152 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
153 log.Errorf("%v\n", err) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
154 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
155 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 ctx := req.Context() |
5596
847de131893a
Fixed duplicate import found by staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5595
diff
changeset
|
158 conn := mw.GetDBConn(req) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 |
5333
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
160 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) |
5595
bbc257dd9abf
Mixed bad error handling found by staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5490
diff
changeset
|
161 if err != nil { |
bbc257dd9abf
Mixed bad error handling found by staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5490
diff
changeset
|
162 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
bbc257dd9abf
Mixed bad error handling found by staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5490
diff
changeset
|
163 log.Errorf("%v\n", err) |
bbc257dd9abf
Mixed bad error handling found by staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5490
diff
changeset
|
164 return |
bbc257dd9abf
Mixed bad error handling found by staticcheck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5490
diff
changeset
|
165 } |
5333
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
166 defer tx.Rollback() |
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
167 |
6c0f40676984
Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5324
diff
changeset
|
168 if err := action.Execute(ctx, tx, template); err != nil { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
169 log.Errorf("%v\n", err) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 return |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
172 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
173 rw.Header().Set( |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
174 "Content-Disposition", |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 fmt.Sprintf("attachment; filename=%s.xlsx", name)) |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
176 rw.Header().Set( |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 "Content-Type", |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
178 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
179 |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
180 if _, err := template.WriteTo(rw); err != nil { |
5490
5f47eeea988d
Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
5478
diff
changeset
|
181 log.Errorf("%v\n", err) |
5321
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
182 } |
0919946f624b
Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
183 } |