-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from RyoJerryYu/feat-add-new-gatewayx
feat: add new gatewayx marshaler
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package gatewayx | ||
|
||
const ( | ||
MIMETextEventStream = "text/event-stream" | ||
MIMETextPlain = "text/plain" | ||
MIMEApplicationJSON = "application/json" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gatewayx | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
) | ||
|
||
type PlainTextMarshaler struct { | ||
runtime.Marshaler | ||
} | ||
|
||
func (m *PlainTextMarshaler) ContentType(_ interface{}) string { | ||
return MIMETextPlain | ||
} | ||
|
||
func (m *PlainTextMarshaler) Marshal(v interface{}) ([]byte, error) { | ||
switch v := v.(type) { | ||
case string: | ||
return []byte(v), nil | ||
case []byte: | ||
return v, nil | ||
} | ||
// if number, bool, or other types, use the default marshaller, | ||
// JSON marshal would work well for them. | ||
return m.Marshaler.Marshal(v) | ||
} | ||
|
||
func (m PlainTextMarshaler) NewEncoder(w io.Writer) runtime.Encoder { | ||
return runtime.EncoderFunc(func(v interface{}) error { | ||
data, err := m.Marshal(v) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = w.Write(data) | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package gatewayx | ||
|
||
import "net/http" | ||
|
||
/** | ||
Sometime Only use the gRPC-Gateway is not enough. | ||
You may need to add some middleware to the gateway to handle some special cases. | ||
*/ | ||
|
||
type pathMatcher func(string) bool | ||
|
||
func PathEqual(path string) pathMatcher { | ||
return func(p string) bool { | ||
return p == path | ||
} | ||
} | ||
|
||
func PathPrefix(prefix string) pathMatcher { | ||
return func(p string) bool { | ||
return len(p) >= len(prefix) && p[:len(prefix)] == prefix | ||
} | ||
} | ||
|
||
// OverwriteAccept returns a middleware that overwrites the Accept header of the request | ||
// It's useful when you cannot force the client to send the correct Accept header | ||
func OverwriteAccept(accept string, matchers ...pathMatcher) func(handler http.Handler) http.Handler { | ||
return func(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
for _, matcher := range matchers { | ||
if matcher(r.URL.Path) { | ||
r.Header.Set("Accept", accept) | ||
break | ||
} | ||
} | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} | ||
} | ||
|
||
// OverwriteContentType returns a middleware that overwrites the Content-Type header of the http request | ||
// It's useful when you cannot force the client to send the correct Content-Type header | ||
func OverwriteContentType(contentType string, matchers ...pathMatcher) func(handler http.Handler) http.Handler { | ||
return func(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
for _, matcher := range matchers { | ||
if matcher(r.URL.Path) { | ||
r.Header.Set("Content-Type", contentType) | ||
break | ||
} | ||
} | ||
handler.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |