58 lines
921 B
Go
58 lines
921 B
Go
package tcp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net"
|
|
)
|
|
|
|
type Method string
|
|
|
|
const (
|
|
MethodGET = "GET"
|
|
MethodPOST = "POST"
|
|
MethodPUT = "PUT"
|
|
MethodDELETE = "DELETE"
|
|
|
|
MethodSOCKET = "SOCKET"
|
|
)
|
|
|
|
type RequestHeader struct {
|
|
Method Method `json:"method"`
|
|
Route string `json:"route"`
|
|
Params map[string]string `json:"params"`
|
|
}
|
|
|
|
type Request struct {
|
|
Method Method
|
|
Route string
|
|
Params map[string]string
|
|
|
|
RemoteAddr string
|
|
|
|
Body []byte
|
|
}
|
|
|
|
func (self *Request) Header() ([]byte, error) {
|
|
b, err := json.Marshal(RequestHeader{
|
|
Method: self.Method,
|
|
Route: self.Route,
|
|
Params: self.Params,
|
|
})
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
func NewRequest(conn net.Conn, header RequestHeader, body []byte) *Request {
|
|
return &Request{
|
|
Method: header.Method,
|
|
Route: header.Route,
|
|
Params: header.Params,
|
|
|
|
RemoteAddr: conn.RemoteAddr().String(),
|
|
|
|
Body: body,
|
|
}
|
|
}
|