fmt: whole hsp package
This commit is contained in:
@ -33,4 +33,3 @@ func ParseAddress(address string) (*Adddress, error) {
|
|||||||
func (a *Adddress) String() string {
|
func (a *Adddress) String() string {
|
||||||
return fmt.Sprintf("%s:%s", a.Host, HSP_PORT)
|
return fmt.Sprintf("%s:%s", a.Host, HSP_PORT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +46,18 @@ type DataFormat struct {
|
|||||||
Encoding string
|
Encoding string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TextDataFormat() *DataFormat {
|
||||||
|
return &DataFormat{Format: DF_TEXT, Encoding: E_UTF8}
|
||||||
|
}
|
||||||
|
|
||||||
|
func JsonDataFormat() *DataFormat {
|
||||||
|
return &DataFormat{Format: DF_JSON, Encoding: E_UTF8}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BytesDataFormat() *DataFormat {
|
||||||
|
return &DataFormat{Format: DF_BYTES}
|
||||||
|
}
|
||||||
|
|
||||||
func ParseDataFormat(format string) (*DataFormat, error) {
|
func ParseDataFormat(format string) (*DataFormat, error) {
|
||||||
parts := strings.Split(format, ":")
|
parts := strings.Split(format, ":")
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
@ -79,4 +91,3 @@ func (df *DataFormat) String() string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf("%s:%s", df.Format, df.Encoding)
|
return fmt.Sprintf("%s:%s", df.Format, df.Encoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ func ParseHeaders(rawHeaders []byte, headers *map[string]string) error {
|
|||||||
|
|
||||||
func SerializeHeaders(headers *map[string]string) []byte {
|
func SerializeHeaders(headers *map[string]string) []byte {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
for k, v := range(*headers) {
|
for k, v := range *headers {
|
||||||
fmt.Fprintf(buf, "%s:%s\n", k, v)
|
fmt.Fprintf(buf, "%s:%s\n", k, v)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(buf, "\n")
|
fmt.Fprintf(buf, "\n")
|
||||||
@ -185,4 +185,3 @@ func (r *PacketDuplex) WritePacket(packet *Packet) (int, error) {
|
|||||||
|
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package hsp
|
package hsp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@ -54,6 +55,19 @@ func (req *Request) ExtractText() (string, error) {
|
|||||||
return string(req.packet.Payload), nil
|
return string(req.packet.Payload), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (req *Request) ExtractJson(out any) error {
|
||||||
|
df, err := req.GetDataFormat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !slices.Contains([]string{DF_JSON}, df.Format) {
|
||||||
|
return errors.New(fmt.Sprintf("Data format '%s' cannot be extracted as json", df.Format))
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Unmarshal(req.packet.Payload, out)
|
||||||
|
}
|
||||||
|
|
||||||
func (req *Request) ExtractBytes() ([]byte, error) {
|
func (req *Request) ExtractBytes() ([]byte, error) {
|
||||||
df, err := req.GetDataFormat()
|
df, err := req.GetDataFormat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,4 +80,3 @@ func (req *Request) ExtractBytes() ([]byte, error) {
|
|||||||
|
|
||||||
return req.packet.Payload, nil
|
return req.packet.Payload, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ func (s *Server) Start() error {
|
|||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !s.IsRunning() {
|
if !s.IsRunning() {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -65,7 +65,6 @@ func (s *Server) Start() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) Stop() error {
|
func (s *Server) Stop() error {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
@ -86,5 +85,3 @@ func (s *Server) IsRunning() bool {
|
|||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
return s.Running
|
return s.Running
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user