Merge branch 'main' of github.com:LandaMm/hsp-go
This commit is contained in:
@ -12,9 +12,9 @@ import (
|
|||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
StatusCode int
|
StatusCode int
|
||||||
Format DataFormat
|
Format DataFormat
|
||||||
Headers map[string]string
|
Headers map[string]string
|
||||||
Payload []byte
|
Payload []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPacketResponse(packet *Packet) *Response {
|
func NewPacketResponse(packet *Packet) *Response {
|
||||||
@ -49,9 +49,9 @@ func NewPacketResponse(packet *Packet) *Response {
|
|||||||
func NewStatusResponse(status int) *Response {
|
func NewStatusResponse(status int) *Response {
|
||||||
return &Response{
|
return &Response{
|
||||||
StatusCode: status,
|
StatusCode: status,
|
||||||
Headers: make(map[string]string),
|
Headers: make(map[string]string),
|
||||||
Format: DataFormat{
|
Format: DataFormat{
|
||||||
Format: DF_BYTES,
|
Format: DF_BYTES,
|
||||||
Encoding: "",
|
Encoding: "",
|
||||||
},
|
},
|
||||||
Payload: make([]byte, 0),
|
Payload: make([]byte, 0),
|
||||||
@ -61,9 +61,9 @@ func NewStatusResponse(status int) *Response {
|
|||||||
func NewTextResponse(text string) *Response {
|
func NewTextResponse(text string) *Response {
|
||||||
return &Response{
|
return &Response{
|
||||||
StatusCode: STATUS_SUCCESS,
|
StatusCode: STATUS_SUCCESS,
|
||||||
Headers: make(map[string]string),
|
Headers: make(map[string]string),
|
||||||
Format: DataFormat{
|
Format: DataFormat{
|
||||||
Format: DF_TEXT,
|
Format: DF_TEXT,
|
||||||
Encoding: E_UTF8,
|
Encoding: E_UTF8,
|
||||||
},
|
},
|
||||||
Payload: []byte(text),
|
Payload: []byte(text),
|
||||||
@ -78,21 +78,37 @@ func NewJsonResponse(data map[string]string) (*Response, error) {
|
|||||||
|
|
||||||
return &Response{
|
return &Response{
|
||||||
StatusCode: STATUS_SUCCESS,
|
StatusCode: STATUS_SUCCESS,
|
||||||
Headers: make(map[string]string),
|
Headers: make(map[string]string),
|
||||||
Format: DataFormat{
|
Format: DataFormat{
|
||||||
Format: DF_JSON,
|
Format: DF_JSON,
|
||||||
Encoding: E_UTF8,
|
Encoding: E_UTF8,
|
||||||
},
|
},
|
||||||
Payload: jsonBytes,
|
Payload: jsonBytes,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewErrorResponse(err error) *Response {
|
||||||
|
return &Response{
|
||||||
|
StatusCode: STATUS_INTERNALERR,
|
||||||
|
Format: DataFormat{
|
||||||
|
Format: DF_TEXT,
|
||||||
|
Encoding: E_UTF8,
|
||||||
|
},
|
||||||
|
Headers: map[string]string{},
|
||||||
|
Payload: []byte(err.Error()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (res *Response) ToPacket() *Packet {
|
func (res *Response) ToPacket() *Packet {
|
||||||
headers := make(map[string]string)
|
headers := make(map[string]string)
|
||||||
|
|
||||||
maps.Copy(headers, res.Headers)
|
maps.Copy(headers, res.Headers)
|
||||||
|
|
||||||
headers[H_DATA_FORMAT] = fmt.Sprintf("%s:%s", res.Format.Format, res.Format.Encoding)
|
if res.Format.Format == DF_BYTES {
|
||||||
|
headers[H_DATA_FORMAT] = DF_BYTES
|
||||||
|
} else {
|
||||||
|
headers[H_DATA_FORMAT] = fmt.Sprintf("%s:%s", res.Format.Format, res.Format.Encoding)
|
||||||
|
}
|
||||||
headers[H_STATUS] = strconv.Itoa(res.StatusCode)
|
headers[H_STATUS] = strconv.Itoa(res.StatusCode)
|
||||||
|
|
||||||
return BuildPacket(headers, res.Payload)
|
return BuildPacket(headers, res.Payload)
|
||||||
@ -117,4 +133,3 @@ func (res *Response) Write(p []byte) (int, error) {
|
|||||||
|
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,6 +37,7 @@ func (r *Router) Handle(conn net.Conn) error {
|
|||||||
// TODO: Ability to keep connection alive
|
// TODO: Ability to keep connection alive
|
||||||
packet, err := dupl.ReadPacket()
|
packet, err := dupl.ReadPacket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
dupl.WritePacket(NewErrorResponse(err).ToPacket())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,4 +52,3 @@ func (r *Router) Handle(conn net.Conn) error {
|
|||||||
}
|
}
|
||||||
return errors.New("Not Found")
|
return errors.New("Not Found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
Hello, everyone!
|
|
||||||
I'm a txt file
|
|
Reference in New Issue
Block a user