Feat: done tcp
This commit is contained in:
53
tcp/request.go
Normal file
53
tcp/request.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Method Method
|
||||
Route string
|
||||
|
||||
RemoteAddr string
|
||||
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (self *Request) Header() ([]byte, error) {
|
||||
b, err := json.Marshal(RequestHeader{
|
||||
Method: self.Method,
|
||||
Route: self.Route,
|
||||
})
|
||||
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,
|
||||
|
||||
RemoteAddr: conn.RemoteAddr().String(),
|
||||
|
||||
Body: body,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user