mirror of
https://github.com/labstack/echo.git
synced 2025-11-06 08:59:21 +02:00
59
engine/engine.go
Normal file
59
engine/engine.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package engine
|
||||
|
||||
import "io"
|
||||
|
||||
type (
|
||||
Type uint8
|
||||
|
||||
HandlerFunc func(Request, Response)
|
||||
|
||||
Engine interface {
|
||||
Start()
|
||||
}
|
||||
|
||||
Request interface {
|
||||
Header() Header
|
||||
// Proto() string
|
||||
// ProtoMajor() int
|
||||
// ProtoMinor() int
|
||||
RemoteAddress() string
|
||||
Method() string
|
||||
URI() string
|
||||
URL() URL
|
||||
Body() io.ReadCloser
|
||||
FormValue(string) string
|
||||
}
|
||||
|
||||
Response interface {
|
||||
Header() Header
|
||||
WriteHeader(int)
|
||||
Write(b []byte) (int, error)
|
||||
Status() int
|
||||
Size() int64
|
||||
Committed() bool
|
||||
}
|
||||
|
||||
Header interface {
|
||||
Add(string, string)
|
||||
Del(string)
|
||||
Get(string) string
|
||||
Set(string, string)
|
||||
}
|
||||
|
||||
URL interface {
|
||||
Scheme() string
|
||||
SetPath(string)
|
||||
Path() string
|
||||
Host() string
|
||||
QueryValue(string) string
|
||||
}
|
||||
|
||||
Config struct {
|
||||
Address string
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
Standard Type = iota
|
||||
FastHTTP
|
||||
)
|
||||
46
engine/fasthttp/header.go
Normal file
46
engine/fasthttp/header.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package fasthttp
|
||||
|
||||
import "github.com/valyala/fasthttp"
|
||||
|
||||
type (
|
||||
RequestHeader struct {
|
||||
fasthttp.RequestHeader
|
||||
}
|
||||
|
||||
ResponseHeader struct {
|
||||
fasthttp.ResponseHeader
|
||||
}
|
||||
)
|
||||
|
||||
func (h *RequestHeader) Add(key, val string) {
|
||||
// h.RequestHeader.Add(key, val)
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Del(key string) {
|
||||
h.RequestHeader.Del(key)
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Get(key string) string {
|
||||
return string(h.RequestHeader.Peek(key))
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Set(key, val string) {
|
||||
h.RequestHeader.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Add(key, val string) {
|
||||
// h.ResponseHeader.Add(key, val)
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Del(key string) {
|
||||
h.ResponseHeader.Del(key)
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Get(key string) string {
|
||||
// return h.ResponseHeader.Get(key)
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Set(key, val string) {
|
||||
h.ResponseHeader.Set(key, val)
|
||||
}
|
||||
45
engine/fasthttp/request.go
Normal file
45
engine/fasthttp/request.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package fasthttp
|
||||
|
||||
import "io"
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/engine"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type (
|
||||
Request struct {
|
||||
context *fasthttp.RequestCtx
|
||||
url engine.URL
|
||||
header engine.Header
|
||||
}
|
||||
)
|
||||
|
||||
func (r *Request) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
return r.context.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (r *Request) Method() string {
|
||||
return string(r.context.Method())
|
||||
}
|
||||
|
||||
func (r *Request) URI() string {
|
||||
return string(r.context.RequestURI())
|
||||
}
|
||||
|
||||
func (r *Request) URL() engine.URL {
|
||||
return r.url
|
||||
}
|
||||
|
||||
func (r *Request) Body() io.ReadCloser {
|
||||
// return r.context.PostBody()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) FormValue(name string) string {
|
||||
return ""
|
||||
}
|
||||
40
engine/fasthttp/response.go
Normal file
40
engine/fasthttp/response.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package fasthttp
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/engine"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type (
|
||||
Response struct {
|
||||
context *fasthttp.RequestCtx
|
||||
header engine.Header
|
||||
status int
|
||||
size int64
|
||||
committed bool
|
||||
}
|
||||
)
|
||||
|
||||
func (r *Response) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Response) WriteHeader(code int) {
|
||||
r.context.SetStatusCode(code)
|
||||
}
|
||||
|
||||
func (r *Response) Write(b []byte) (int, error) {
|
||||
return r.context.Write(b)
|
||||
}
|
||||
|
||||
func (r *Response) Status() int {
|
||||
return r.status
|
||||
}
|
||||
|
||||
func (r *Response) Size() int64 {
|
||||
return r.size
|
||||
}
|
||||
|
||||
func (r *Response) Committed() bool {
|
||||
return r.committed
|
||||
}
|
||||
43
engine/fasthttp/server.go
Normal file
43
engine/fasthttp/server.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package fasthttp
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
import (
|
||||
"github.com/labstack/echo/engine"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type (
|
||||
Server struct {
|
||||
*http.Server
|
||||
config *engine.Config
|
||||
handler engine.HandlerFunc
|
||||
}
|
||||
)
|
||||
|
||||
func NewServer(config *engine.Config, handler engine.HandlerFunc) *Server {
|
||||
return &Server{
|
||||
Server: new(http.Server),
|
||||
config: config,
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Start() {
|
||||
fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) {
|
||||
println("FastHTTP")
|
||||
req := &Request{
|
||||
context: ctx,
|
||||
url: &URL{ctx.URI()},
|
||||
header: &RequestHeader{ctx.Request.Header},
|
||||
}
|
||||
res := &Response{
|
||||
context: ctx,
|
||||
header: &ResponseHeader{ctx.Response.Header},
|
||||
}
|
||||
s.handler(req, res)
|
||||
})
|
||||
log.Fatal(s.ListenAndServe())
|
||||
}
|
||||
29
engine/fasthttp/url.go
Normal file
29
engine/fasthttp/url.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package fasthttp
|
||||
|
||||
import "github.com/valyala/fasthttp"
|
||||
|
||||
type (
|
||||
URL struct {
|
||||
*fasthttp.URI
|
||||
}
|
||||
)
|
||||
|
||||
func (u *URL) Scheme() string {
|
||||
return string(u.URI.Scheme())
|
||||
}
|
||||
|
||||
func (u *URL) Host() string {
|
||||
return string(u.URI.Host())
|
||||
}
|
||||
|
||||
func (u *URL) SetPath(path string) {
|
||||
// return string(u.URI.Path())
|
||||
}
|
||||
|
||||
func (u *URL) Path() string {
|
||||
return string(u.URI.Path())
|
||||
}
|
||||
|
||||
func (u *URL) QueryValue(name string) string {
|
||||
return ""
|
||||
}
|
||||
25
engine/standard/header.go
Normal file
25
engine/standard/header.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package standard
|
||||
|
||||
import "net/http"
|
||||
|
||||
type (
|
||||
Header struct {
|
||||
http.Header
|
||||
}
|
||||
)
|
||||
|
||||
func (h *Header) Add(key, val string) {
|
||||
h.Header.Add(key, val)
|
||||
}
|
||||
|
||||
func (h *Header) Del(key string) {
|
||||
h.Header.Del(key)
|
||||
}
|
||||
|
||||
func (h *Header) Get(key string) string {
|
||||
return h.Header.Get(key)
|
||||
}
|
||||
|
||||
func (h *Header) Set(key, val string) {
|
||||
h.Header.Set(key, val)
|
||||
}
|
||||
56
engine/standard/request.go
Normal file
56
engine/standard/request.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package standard
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/engine"
|
||||
)
|
||||
|
||||
type (
|
||||
Request struct {
|
||||
request *http.Request
|
||||
url engine.URL
|
||||
header engine.Header
|
||||
}
|
||||
)
|
||||
|
||||
func NewRequest(r *http.Request) *Request {
|
||||
return &Request{
|
||||
request: r,
|
||||
url: NewURL(r.URL),
|
||||
header: &Header{r.Header},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) Request() *http.Request {
|
||||
return r.request
|
||||
}
|
||||
|
||||
func (r *Request) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Request) URL() engine.URL {
|
||||
return r.url
|
||||
}
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
return r.request.RemoteAddr
|
||||
}
|
||||
|
||||
func (r *Request) Method() string {
|
||||
return r.request.Method
|
||||
}
|
||||
|
||||
func (r *Request) URI() string {
|
||||
return r.request.RequestURI
|
||||
}
|
||||
|
||||
func (r *Request) Body() io.ReadCloser {
|
||||
return r.request.Body
|
||||
}
|
||||
|
||||
func (r *Request) FormValue(name string) string {
|
||||
return r.request.FormValue(name)
|
||||
}
|
||||
53
engine/standard/response.go
Normal file
53
engine/standard/response.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package standard
|
||||
|
||||
import "net/http"
|
||||
import "github.com/labstack/echo/engine"
|
||||
|
||||
type (
|
||||
Response struct {
|
||||
response http.ResponseWriter
|
||||
header engine.Header
|
||||
status int
|
||||
size int64
|
||||
committed bool
|
||||
}
|
||||
)
|
||||
|
||||
func NewResponse(w http.ResponseWriter) *Response {
|
||||
return &Response{
|
||||
response: w,
|
||||
header: &Header{w.Header()},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Response) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *Response) WriteHeader(code int) {
|
||||
if r.committed {
|
||||
// r.echo.Logger().Warn("response already committed")
|
||||
return
|
||||
}
|
||||
r.status = code
|
||||
r.response.WriteHeader(code)
|
||||
r.committed = true
|
||||
}
|
||||
|
||||
func (r *Response) Write(b []byte) (n int, err error) {
|
||||
n, err = r.response.Write(b)
|
||||
r.size += int64(n)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Response) Status() int {
|
||||
return r.status
|
||||
}
|
||||
|
||||
func (r *Response) Size() int64 {
|
||||
return r.size
|
||||
}
|
||||
|
||||
func (r *Response) Committed() bool {
|
||||
return r.committed
|
||||
}
|
||||
32
engine/standard/server.go
Normal file
32
engine/standard/server.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package standard
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/engine"
|
||||
)
|
||||
|
||||
type (
|
||||
Server struct {
|
||||
*http.Server
|
||||
config *engine.Config
|
||||
handler engine.HandlerFunc
|
||||
}
|
||||
)
|
||||
|
||||
func NewServer(config *engine.Config, handler engine.HandlerFunc) *Server {
|
||||
return &Server{
|
||||
Server: new(http.Server),
|
||||
config: config,
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Start() {
|
||||
s.Addr = s.config.Address
|
||||
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
s.handler(NewRequest(r), NewResponse(w))
|
||||
})
|
||||
log.Fatal(s.ListenAndServe())
|
||||
}
|
||||
41
engine/standard/url.go
Normal file
41
engine/standard/url.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package standard
|
||||
|
||||
import "net/url"
|
||||
|
||||
type (
|
||||
URL struct {
|
||||
url *url.URL
|
||||
query url.Values
|
||||
}
|
||||
)
|
||||
|
||||
func NewURL(u *url.URL) *URL {
|
||||
return &URL{url: u}
|
||||
}
|
||||
|
||||
func (u *URL) URL() *url.URL {
|
||||
return u.url
|
||||
}
|
||||
|
||||
func (u *URL) Scheme() string {
|
||||
return u.url.Scheme
|
||||
}
|
||||
|
||||
func (u *URL) Host() string {
|
||||
return u.url.Host
|
||||
}
|
||||
|
||||
func (u *URL) SetPath(path string) {
|
||||
u.url.Path = path
|
||||
}
|
||||
|
||||
func (u *URL) Path() string {
|
||||
return u.url.Path
|
||||
}
|
||||
|
||||
func (u *URL) QueryValue(name string) string {
|
||||
if u.query == nil {
|
||||
u.query = u.url.Query()
|
||||
}
|
||||
return u.query.Get(name)
|
||||
}
|
||||
Reference in New Issue
Block a user