Skip to content

Commit 002492e

Browse files
committed
* Added BuildStream and AddRoute functions
* Moved existing example to examples/file_output * Added new exampe examples/stream_output * Updated readme.md
1 parent 9ce5aaf commit 002492e

File tree

16 files changed

+395
-4
lines changed

16 files changed

+395
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#internal/dist/openapi.yaml
44
testing_out.yaml
55
./testing_out.yaml
6-
internal/dist/openapi.yaml
6+
internal/dist/openapi.yaml
7+
.vscode

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ other parts of the system for that matter.
119119
To run examples, and checkout hosted documentation via Swagger UI, issue the following command:
120120
121121
```sh
122-
$ go run ./examples/*.go
122+
$ go run ./examples/file_output/*.go
123+
# or uncomment line 40 and comment line 38 in internal/dist/index.html before running:
124+
$ go run ./examples/stream_output/*.go
123125
```
124126
125127
And navigate to `http://localhost:3005/docs/api/` in case that you didn't change anything before running the example

build.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func getPathFromFirstElement(cbs []ConfigBuilder) string {
3838
func (o *OAS) BuildDocs(conf ...ConfigBuilder) error {
3939
o.initCallStackForRoutes()
4040

41-
yml, err := marshalToYAML(o)
41+
yml, err := o.marshalToYAML()
4242
if err != nil {
4343
return fmt.Errorf("marshaling issue occurred: %w", err)
4444
}
@@ -51,7 +51,23 @@ func (o *OAS) BuildDocs(conf ...ConfigBuilder) error {
5151
return nil
5252
}
5353

54-
func marshalToYAML(oas *OAS) ([]byte, error) {
54+
// BuildStream marshals the OAS struct to YAML and writes it to a stream.
55+
//
56+
// Returns an error if there is any.
57+
func (o *OAS) BuildStream(w io.Writer) error {
58+
yml, err := o.marshalToYAML()
59+
if err != nil {
60+
return fmt.Errorf("marshaling issue occurred: %w", err)
61+
}
62+
63+
err = writeAndFlush(yml, w)
64+
if err != nil {
65+
return fmt.Errorf("writing issue occurred: %w", err)
66+
}
67+
return nil
68+
}
69+
70+
func (oas *OAS) marshalToYAML() ([]byte, error) {
5571
transformedOAS := oas.transformToHybridOAS()
5672

5773
yml, err := yaml.Marshal(transformedOAS)
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/stream_output/logging.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"time"
7+
)
8+
9+
type responseData struct {
10+
status int
11+
size int
12+
}
13+
14+
// our http.ResponseWriter implementation
15+
type loggingResponseWriter struct {
16+
http.ResponseWriter // compose original http.ResponseWriter
17+
responseData *responseData
18+
}
19+
20+
func (r *loggingResponseWriter) Write(b []byte) (int, error) {
21+
size, err := r.ResponseWriter.Write(b) // write response using original http.ResponseWriter
22+
r.responseData.size += size // capture size
23+
return size, err
24+
}
25+
26+
func (r *loggingResponseWriter) WriteHeader(statusCode int) {
27+
r.ResponseWriter.WriteHeader(statusCode) // write status code using original http.ResponseWriter
28+
r.responseData.status = statusCode // capture status code
29+
}
30+
31+
func LogginMiddleware(h http.Handler) http.Handler {
32+
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
33+
var (
34+
start = time.Now()
35+
responseData = &responseData{}
36+
)
37+
38+
h.ServeHTTP(&loggingResponseWriter{
39+
ResponseWriter: rw,
40+
responseData: responseData,
41+
}, req)
42+
43+
duration := time.Since(start)
44+
45+
log.Printf("%s[%v] uri:%s duration:%v size:%d", req.Method, responseData.status, req.RequestURI, duration, responseData.size)
46+
})
47+
}

0 commit comments

Comments
 (0)