Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
fix: request, http.Request에 맞게 변경 및 바이너리 파일 서빙 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
2rebi committed Oct 6, 2022
1 parent f1fc38b commit 903b5c3
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 438 deletions.
84 changes: 68 additions & 16 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package golam

import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"github.com/aws/aws-lambda-go/events"
"io"
"net"
"net/http"
"net/url"
"strings"
)

const (
Expand All @@ -20,9 +24,13 @@ type Context interface {

Get(key interface{}) interface{}

Request() *Req
SetRequest(r *http.Request)

Response() *Resp
Request() *http.Request

RequestBodyString() string

Response() *Response

Scheme() string

Expand Down Expand Up @@ -70,8 +78,10 @@ type Context interface {

XMLBytes(status int, b []byte) error

Result(status int, contentType string, b []byte) error
ResultStream(status int, contentType string, reader io.Reader) error

Result(status int, contentType string, b []byte) error

Write(b []byte) (int, error)

// TODO
Expand All @@ -93,8 +103,8 @@ var _ Context = (*contextImpl)(nil)

type contextImpl struct {
ctx context.Context
request *Req
response *Resp
request *http.Request
response *Response
path string
pathParams PathParams
query url.Values
Expand Down Expand Up @@ -130,20 +140,55 @@ func (c *contextImpl) writeContentType(value string) {
}
}

func (c *contextImpl) Request() *Req {
func (c *contextImpl) SetRequest(r *http.Request) {
c.request = r
}

func (c *contextImpl) Request() *http.Request {
return c.request
}

func (c *contextImpl) Response() *Resp {
func (c *contextImpl) RequestBodyString() string {
var buf bytes.Buffer
c.request.Body = io.NopCloser(io.TeeReader(c.request.Body, &buf))
return buf.String()
}

func (c *contextImpl) Response() *Response {
return c.response
}

func (c *contextImpl) Scheme() string {
return c.Request().Scheme()
header := c.request.Header
if scheme := header.Get(HeaderXForwardedProto); scheme != "" {
return scheme
}
if scheme := header.Get(HeaderXForwardedProtocol); scheme != "" {
return scheme
}
if ssl := header.Get(HeaderXForwardedSsl); ssl == "on" {
return "https"
}
if scheme := header.Get(HeaderXUrlScheme); scheme != "" {
return scheme
}
return "http"
}

func (c *contextImpl) RealIP() string {
return c.Request().RealIP()
header := c.request.Header
if ip := header.Get(HeaderXForwardedFor); ip != "" {
i := strings.IndexAny(ip, ",")
if i > 0 {
return strings.TrimSpace(ip[:i])
}
return ip
}
if ip := header.Get(HeaderXRealIP); ip != "" {
return ip
}
ra, _, _ := net.SplitHostPort(c.request.RemoteAddr)
return ra
}

func (c *contextImpl) Path() string {
Expand Down Expand Up @@ -187,15 +232,15 @@ func (c *contextImpl) NoContent(status int) error {
return nil
}

func (c contextImpl) HTML(status int, html string) error {
func (c *contextImpl) HTML(status int, html string) error {
return c.HTMLBytes(status, []byte(html))
}

func (c contextImpl) HTMLBytes(status int, b []byte) error {
func (c *contextImpl) HTMLBytes(status int, b []byte) error {
return c.Result(status, MIMETextHTMLCharsetUTF8, b)
}

func (c contextImpl) String(status int, s string) error {
func (c *contextImpl) String(status int, s string) error {
return c.Result(status, MIMETextPlainCharsetUTF8, []byte(s))
}

Expand All @@ -218,15 +263,15 @@ func (c *contextImpl) JSON(status int, i interface{}) error {
return c.json(status, i, "")
}

func (c contextImpl) JSONPretty(status int, i interface{}) error {
func (c *contextImpl) JSONPretty(status int, i interface{}) error {
return c.JSONWithIndent(status, i, defaultIndent)
}

func (c *contextImpl) JSONWithIndent(status int, i interface{}, indent string) error {
return c.json(status, i, indent)
}

func (c contextImpl) JSONBytes(status int, b []byte) (err error) {
func (c *contextImpl) JSONBytes(status int, b []byte) (err error) {
return c.Result(status, MIMEApplicationJSONCharsetUTF8, b)
}

Expand Down Expand Up @@ -270,13 +315,20 @@ func (c *contextImpl) XMLBytes(status int, b []byte) (err error) {
return
}

func (c *contextImpl) Result(status int, contentType string, b []byte) (err error) {
func (c *contextImpl) ResultStream(status int, contentType string, reader io.Reader) (err error) {
c.writeContentType(contentType)
if len(contentType) > 0 {
c.Response().isBinary = !notBinaryTable[contentType]
}
c.Response().WriteHeader(status)
_, err = c.Write(b)
_, err = io.Copy(c.Response(), reader)
return
}

func (c *contextImpl) Result(status int, contentType string, b []byte) (err error) {
return c.ResultStream(status, contentType, bytes.NewReader(b))
}

func (c *contextImpl) Write(b []byte) (int, error) {
return c.Response().Write(b)
}
Expand Down
80 changes: 80 additions & 0 deletions define.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package golam

const (
MIMEApplicationJSON = "application/json"
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
MIMEApplicationXML = "application/xml"
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
MIMETextHTML = "text/html"
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
MIMETextPlain = "text/plain"
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
)

const (
charsetUTF8 = "charset=UTF-8"
)

const (
HeaderAccept = "Accept"
HeaderAcceptEncoding = "Accept-Encoding"
HeaderAllow = "Allow"
HeaderAuthorization = "Authorization"
HeaderContentDisposition = "Content-Disposition"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderCookie = "Cookie"
HeaderSetCookie = "Set-Cookie"
HeaderIfModifiedSince = "If-Modified-Since"
HeaderLastModified = "Last-Modified"
HeaderLocation = "Location"
HeaderRetryAfter = "Retry-After"
HeaderUpgrade = "Upgrade"
HeaderVary = "Vary"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedFor = "X-Forwarded-For"
HeaderXForwardedProto = "X-Forwarded-Proto"
HeaderXForwardedProtocol = "X-Forwarded-Protocol"
HeaderXForwardedSsl = "X-Forwarded-Ssl"
HeaderXUrlScheme = "X-Url-Scheme"
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
HeaderXRealIP = "X-Real-Ip"
HeaderXRequestID = "X-Request-Id"
HeaderXCorrelationID = "X-Correlation-Id"
HeaderXRequestedWith = "X-Requested-With"
HeaderServer = "Server"
HeaderOrigin = "Origin"
HeaderHost = "Host"
HeaderCacheControl = "Cache-Control"
HeaderConnection = "Connection"
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderAccessControlMaxAge = "Access-Control-Max-Age"
HeaderStrictTransportSecurity = "Strict-Transport-Security"
HeaderXContentTypeOptions = "X-Content-Type-Options"
HeaderXXSSProtection = "X-XSS-Protection"
HeaderXFrameOptions = "X-Frame-Options"
HeaderContentSecurityPolicy = "Content-Security-Policy"
HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
HeaderXCSRFToken = "X-CSRF-Token"
HeaderReferrerPolicy = "Referrer-Policy"
)

var (
notBinaryTable = map[string]bool{
MIMEApplicationJSON: true,
MIMEApplicationJSONCharsetUTF8: true,
MIMEApplicationXML: true,
MIMEApplicationXMLCharsetUTF8: true,
MIMETextHTML: true,
MIMETextHTMLCharsetUTF8: true,
MIMETextPlain: true,
MIMETextPlainCharsetUTF8: true,
}
)
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/unsafe-risk/golam
go 1.18

require github.com/aws/aws-lambda-go v1.33.0

require golang.org/x/text v0.3.7 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
Expand Down
Loading

0 comments on commit 903b5c3

Please sign in to comment.