Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 498abc18b4 | |||
| 5ea1af7fb1 | |||
| bc578cf25c |
@@ -44,7 +44,7 @@ func server() error {
|
||||
serv := dsrpc.NewService()
|
||||
|
||||
cont := NewController()
|
||||
serv.Handler(api.HelloMethod, cont.HelloHandler)
|
||||
serv.Handle(api.HelloMethod, cont.HelloHandler)
|
||||
|
||||
serv.PreMiddleware(dsrpc.LogRequest)
|
||||
serv.PostMiddleware(dsrpc.LogResponse)
|
||||
@@ -220,9 +220,9 @@ func sampleServ(quiet bool) error {
|
||||
serv.PreMiddleware(authMiddleware)
|
||||
serv.PreMiddleware(dsrpc.LogRequest)
|
||||
|
||||
serv.Handler(HelloMethod, helloHandler)
|
||||
serv.Handler(SaveMethod, saveHandler)
|
||||
serv.Handler(LoadMethod, loadHandler)
|
||||
serv.Handle(HelloMethod, helloHandler)
|
||||
serv.Handle(SaveMethod, saveHandler)
|
||||
serv.Handle(LoadMethod, loadHandler)
|
||||
|
||||
serv.PostMiddleware(dsrpc.LogResponse)
|
||||
serv.PostMiddleware(dsrpc.LogAccess)
|
||||
|
||||
@@ -26,7 +26,7 @@ func server() error {
|
||||
serv := dsrpc.NewService()
|
||||
|
||||
cont := NewController()
|
||||
serv.Handler(api.HelloMethod, cont.HelloHandler)
|
||||
serv.Handle(api.HelloMethod, cont.HelloHandler)
|
||||
|
||||
serv.PreMiddleware(dsrpc.LogRequest)
|
||||
serv.PostMiddleware(dsrpc.LogResponse)
|
||||
|
||||
47
exec_test.go
47
exec_test.go
@@ -67,7 +67,8 @@ func TestLocalSave(t *testing.T) {
|
||||
|
||||
reader := bytes.NewReader(binBytes)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = LocalPut(ctx, SaveMethod, reader, binSize, ¶ms, &result, auth, saveHandler)
|
||||
@@ -88,7 +89,8 @@ func TestLocalLoad(t *testing.T) {
|
||||
binBytes := make([]byte, 0)
|
||||
writer := bytes.NewBuffer(binBytes)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = LocalGet(ctx, LoadMethod, writer, ¶ms, &result, auth, loadHandler)
|
||||
@@ -101,7 +103,7 @@ func TestLocalLoad(t *testing.T) {
|
||||
|
||||
func TestNetExec(t *testing.T) {
|
||||
go testServ(false)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
err := clientHello()
|
||||
|
||||
require.NoError(t, err)
|
||||
@@ -109,21 +111,21 @@ func TestNetExec(t *testing.T) {
|
||||
|
||||
func TestNetSave(t *testing.T) {
|
||||
go testServ(false)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
err := clientSave()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestNetLoad(t *testing.T) {
|
||||
go testServ(false)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
err := clientLoad()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func BenchmarkNetPut(b *testing.B) {
|
||||
go testServ(true)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
clientSave()
|
||||
|
||||
pBench := func(pb *testing.PB) {
|
||||
@@ -148,10 +150,11 @@ func clientHello() error {
|
||||
binBytes := make([]byte, binSize)
|
||||
rand.Read(binBytes)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = Exec(ctx, "127.0.0.1:8081", HelloMethod, ¶ms, &result, auth)
|
||||
err = Exec(ctx, "127.0.0.1:18081", HelloMethod, ¶ms, &result, auth)
|
||||
if err != nil {
|
||||
logError("method err:", err)
|
||||
return err
|
||||
@@ -176,10 +179,11 @@ func clientSave() error {
|
||||
|
||||
reader := bytes.NewReader(binBytes)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = Put(ctx, "127.0.0.1:8081", SaveMethod, reader, binSize, ¶ms, &result, auth)
|
||||
err = Put(ctx, "127.0.0.1:18081", SaveMethod, reader, binSize, ¶ms, &result, auth)
|
||||
if err != nil {
|
||||
logError("method err:", err)
|
||||
return err
|
||||
@@ -200,10 +204,11 @@ func clientLoad() error {
|
||||
binBytes := make([]byte, 0)
|
||||
writer := bytes.NewBuffer(binBytes)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = Get(ctx, "127.0.0.1:8081", LoadMethod, writer, ¶ms, &result, auth)
|
||||
err = Get(ctx, "127.0.0.1:18081", LoadMethod, writer, ¶ms, &result, auth)
|
||||
if err != nil {
|
||||
logError("method err:", err)
|
||||
return err
|
||||
@@ -228,10 +233,11 @@ func testServ(quiet bool) error {
|
||||
SetAccessWriter(io.Discard)
|
||||
SetMessageWriter(io.Discard)
|
||||
}
|
||||
|
||||
serv := NewService()
|
||||
serv.Handler(HelloMethod, helloHandler)
|
||||
serv.Handler(SaveMethod, saveHandler)
|
||||
serv.Handler(LoadMethod, loadHandler)
|
||||
serv.Handle(HelloMethod, helloHandler)
|
||||
serv.Handle(SaveMethod, saveHandler)
|
||||
serv.Handle(LoadMethod, loadHandler)
|
||||
|
||||
serv.PreMiddleware(LogRequest)
|
||||
serv.PreMiddleware(auth)
|
||||
@@ -239,7 +245,7 @@ func testServ(quiet bool) error {
|
||||
serv.PostMiddleware(LogResponse)
|
||||
serv.PostMiddleware(LogAccess)
|
||||
|
||||
err = serv.Listen(":8081")
|
||||
err = serv.Listen(":18081")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -277,7 +283,8 @@ func helloHandler(content *Content) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = content.ReadBin(ctx, io.Discard)
|
||||
@@ -308,7 +315,8 @@ func saveHandler(content *Content) error {
|
||||
bufferBytes := make([]byte, 0, 1024)
|
||||
binWriter := bytes.NewBuffer(bufferBytes)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = content.ReadBin(ctx, binWriter)
|
||||
@@ -336,7 +344,8 @@ func loadHandler(content *Content) error {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
err = content.ReadBin(ctx, io.Discard)
|
||||
|
||||
19
logger.go
19
logger.go
@@ -10,30 +10,27 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var messageWriter io.Writer = os.Stdout
|
||||
var accessWriter io.Writer = os.Stdout
|
||||
var (
|
||||
messageWriter io.Writer = os.Stdout
|
||||
accessWriter io.Writer = os.Stdout
|
||||
)
|
||||
|
||||
func logDebug(messages ...any) {
|
||||
stamp := time.Now().Format(time.RFC3339)
|
||||
fmt.Fprintln(messageWriter, stamp, "debug", messages)
|
||||
fmt.Fprintln(messageWriter, "debug:", messages)
|
||||
}
|
||||
|
||||
func logInfo(messages ...any) {
|
||||
stamp := time.Now().Format(time.RFC3339)
|
||||
fmt.Fprintln(messageWriter, stamp, "info", messages)
|
||||
fmt.Fprintln(messageWriter, "info:", messages)
|
||||
}
|
||||
|
||||
func logError(messages ...any) {
|
||||
stamp := time.Now().Format(time.RFC3339)
|
||||
fmt.Fprintln(messageWriter, stamp, "error", messages)
|
||||
fmt.Fprintln(messageWriter, "error:", messages)
|
||||
}
|
||||
|
||||
func logAccess(messages ...any) {
|
||||
stamp := time.Now().Format(time.RFC3339)
|
||||
fmt.Fprintln(accessWriter, stamp, "access", messages)
|
||||
fmt.Fprintln(accessWriter, "access:", messages)
|
||||
}
|
||||
|
||||
func SetAccessWriter(writer io.Writer) {
|
||||
|
||||
Reference in New Issue
Block a user