Skip to content

Commit 4ee964f

Browse files
committed
Add in logs Request id
1 parent b93a657 commit 4ee964f

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

docker-files/opt/webroot/templates/upload-template.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
action="{{placeholder "http.request.scheme"}}://{{placeholder "http.request.hostport" }}{{placeholder "http.request.orig_uri"}}"
1313
method="post"
1414
>
15+
Maxfilesize is: {{ placeholder "http.upload.max_filesize"}}   Bytes
16+
<br>
1517
<input type="file" name="myFile" />
1618
<input type="submit" value="upload" />
1719
</form>

notify.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package upload
2+
3+
// TODO: Implent https notification
4+
func (u Upload) SendNotify() error {
5+
6+
/* netClient := &http.Client{
7+
Timeout: 5 * time.Second,
8+
Transport: http.DefaultTransport,
9+
}
10+
11+
response, _ := netClient.(u.NotifyMethod)(u.NotifyURL)
12+
*/
13+
return nil
14+
}

upload.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Upload struct {
2525
DestDir string `json:"dest_dir,omitempty"`
2626
MaxFilesize int64 `json:"max_filesize,omitempty"`
2727
ResponseTemplate string `json:"response_template,omitempty"`
28+
NotifyURL string `json:"notify_url,omitempty"`
29+
NotifyMethod string `json:"notify_method,omitempty"`
2830

2931
ctx caddy.Context
3032
logger *zap.Logger
@@ -65,7 +67,9 @@ func (u *Upload) Provision(ctx caddy.Context) error {
6567
u.logger.Info("Current Config",
6668
zap.String("Destinaton Directory (dest_dir)", u.DestDir),
6769
zap.Int64("Max filesize in bytes (max_filesize)", u.MaxFilesize),
68-
zap.String("Response Template (response_template)", u.ResponseTemplate))
70+
zap.String("Response Template (response_template)", u.ResponseTemplate),
71+
zap.String("Notify URL (notify_url)", u.NotifyURL),
72+
)
6973

7074
return nil
7175
}
@@ -85,16 +89,20 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
8589
if !requuiderr {
8690
requuid = "0"
8791
u.logger.Error("http.request.uuid",
88-
zap.Bool("requuiderr", requuiderr))
92+
zap.Bool("requuiderr", requuiderr),
93+
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
8994
}
9095

96+
repl.Set("http.upload.max_filesize", u.MaxFilesize)
97+
9198
r.Body = http.MaxBytesReader(w, r.Body, u.MaxFilesize)
9299
if max_size_err := r.ParseMultipartForm(u.MaxFilesize); max_size_err != nil {
93100
u.logger.Error("ServeHTTP",
94101
zap.String("Request uuid", requuid),
95102
zap.String("message", "The uploaded file is too big. Please choose an file that's less than MaxFilesize."),
96103
zap.Int64("MaxFilesize in Bytes", u.MaxFilesize),
97-
zap.Error(max_size_err))
104+
zap.Error(max_size_err),
105+
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
98106
return caddyhttp.Error(http.StatusRequestEntityTooLarge, max_size_err)
99107
}
100108

@@ -106,7 +114,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
106114
u.logger.Error("FormFile Error",
107115
zap.String("Request uuid", requuid),
108116
zap.String("message", "Error Retrieving the File"),
109-
zap.Error(ff_err))
117+
zap.Error(ff_err),
118+
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
110119
return caddyhttp.Error(http.StatusInternalServerError, ff_err)
111120
}
112121
defer file.Close()
@@ -119,7 +128,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
119128
u.logger.Error("TempFile Error",
120129
zap.String("Request uuid", requuid),
121130
zap.String("message", "Error at TempFile"),
122-
zap.Error(tmpf_err))
131+
zap.Error(tmpf_err),
132+
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
123133
return caddyhttp.Error(http.StatusInternalServerError, tmpf_err)
124134
}
125135
defer tempFile.Close()
@@ -131,7 +141,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
131141
u.logger.Error("ReadAll Error",
132142
zap.String("Request uuid", requuid),
133143
zap.String("message", "Error at ReadAll"),
134-
zap.Error(io_err))
144+
zap.Error(io_err),
145+
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
135146
return caddyhttp.Error(http.StatusInternalServerError, io_err)
136147
}
137148
// write this byte array to our temporary file
@@ -141,7 +152,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
141152
zap.String("Request uuid", requuid),
142153
zap.String("Uploaded File", handler.Filename),
143154
zap.Int64("File Size", handler.Size),
144-
zap.Any("MIME Header", handler.Header))
155+
zap.Any("MIME Header", handler.Header),
156+
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
145157

146158
repl.Set("http.upload.filename", handler.Filename)
147159
repl.Set("http.upload.filesize", handler.Size)
@@ -150,6 +162,9 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
150162
r.URL.Path = "/" + u.ResponseTemplate
151163
}
152164

165+
if u.NotifyURL != "" {
166+
u.SendNotify()
167+
}
153168
return next.ServeHTTP(w, r)
154169
}
155170

0 commit comments

Comments
 (0)