Stan il y a 1 semaine
Parent
commit
b61edd9d63
5 fichiers modifiés avec 53 ajouts et 37 suppressions
  1. 5 5
      http_client.go
  2. 5 13
      http_server.go
  3. 0 19
      logging_log.go
  4. 22 0
      logging_log_binary.go
  5. 21 0
      logging_log_wasm.go

+ 5 - 5
http_client.go

@@ -25,10 +25,10 @@ type HTTPCLientResponse struct {
 type HTTPClient struct {
 	scheme string
 	host   string
-	port   uint
+	port   string
 }
 
-func NewHTTPClient(scheme string, host string, port uint) *HTTPClient {
+func NewHTTPClient(scheme string, host string, port string) *HTTPClient {
 	return &HTTPClient{
 		scheme: scheme,
 		host:   host,
@@ -46,7 +46,7 @@ func (this *HTTPClient) Request(method int, path string, headers HTTPHeaders, re
 	if method == METHOD_GET {
 		requestInternal, err = http.NewRequest(
 			"GET",
-			fmt.Sprintf("%s://%s:%d/%s", this.scheme, this.host, this.port, path),
+			fmt.Sprintf("%s://%s:%s%s", this.scheme, this.host, this.port, path),
 			nil,
 		)
 	}
@@ -54,7 +54,7 @@ func (this *HTTPClient) Request(method int, path string, headers HTTPHeaders, re
 	if method == METHOD_POST {
 		requestInternal, err = http.NewRequest(
 			"POST",
-			fmt.Sprintf("%s://%s:%d/%s", this.scheme, this.host, this.port, path),
+			fmt.Sprintf("%s://%s:%s%s", this.scheme, this.host, this.port, path),
 			bytes.NewBuffer(buffer),
 		)
 	}
@@ -87,7 +87,7 @@ func (this *HTTPClient) Request(method int, path string, headers HTTPHeaders, re
 	}
 
 	var contentType string
-	if contentTypeHeader == "text/plain" || contentTypeHeader == "application/octet-stream" {
+	if contentTypeHeader == "text/plain" || contentTypeHeader == "application/json" || contentTypeHeader == "application/octet-stream" {
 		contentType = contentTypeHeader
 	} else {
 		parts := strings.Split(contentTypeHeader, ";")

+ 5 - 13
http_server.go

@@ -130,19 +130,7 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
 			}
 		}
 
-		buffer, err := json.Marshal(query)
-		if err != nil {
-			this.log.Error("malformed request query", LogValue("path", requestStream.URL.Path), LogError(err))
-			responseStream.WriteHeader(http.StatusInternalServerError)
-			return
-		}
-
-		err = json.Unmarshal(buffer, handler.Data)
-		if err != nil {
-			this.log.Error("malformed request query", LogValue("path", requestStream.URL.Path), LogError(err))
-			responseStream.WriteHeader(http.StatusInternalServerError)
-			return
-		}
+		handler.Data = query
 	}
 
 	// Parse POST body
@@ -195,6 +183,10 @@ func (this *HTTPServer) ServeHTTP(responseStream http.ResponseWriter, requestStr
 		},
 	)
 
+	if response.Error != nil {
+		this.log.Error("error serving request", LogError(response.Error))
+	}
+
 	// Headers
 	for key, value := range response.Headers {
 		responseStream.Header().Add(key, value)

+ 0 - 19
logging_log.go

@@ -2,7 +2,6 @@ package fairwind
 
 import (
 	"sync"
-	"syscall/js"
 )
 
 type Log struct {
@@ -37,21 +36,3 @@ func (this *Log) Error(message string, parameters ...LoggingParameter) {
 func (this *Log) Critical(message string, parameters ...LoggingParameter) {
 	this.log(SEVERITY_CRITICAL, message, parameters...)
 }
-
-func (this *Log) log(severity int, message string, parameters ...LoggingParameter) {
-	this.mutex.Lock()
-	defer this.mutex.Unlock()
-
-	// TODO: detect OS
-	console := js.Global().Get("console")
-
-	line, err := this.formatter.Format(
-		*NewLogLine(severity, message, parameters),
-	)
-	if err != nil {
-		// TODO: log error OS-specific
-		return
-	}
-
-	console.Call("log", line)
-}

+ 22 - 0
logging_log_binary.go

@@ -0,0 +1,22 @@
+//go:build linux && amd64
+
+package fairwind
+
+import (
+	"fmt"
+)
+
+func (this *Log) log(severity int, message string, parameters ...LoggingParameter) {
+	this.mutex.Lock()
+	defer this.mutex.Unlock()
+
+	line, err := this.formatter.Format(
+		*NewLogLine(severity, message, parameters),
+	)
+	if err != nil {
+		// TODO: log error OS-specific
+		return
+	}
+
+	fmt.Println(line)
+}

+ 21 - 0
logging_log_wasm.go

@@ -0,0 +1,21 @@
+//go:build js && wasm
+
+package fairwind
+
+import "syscall/js"
+
+func (this *Log) log(severity int, message string, parameters ...LoggingParameter) {
+	this.mutex.Lock()
+	defer this.mutex.Unlock()
+
+	line, err := this.formatter.Format(
+		*NewLogLine(severity, message, parameters),
+	)
+	if err != nil {
+		// TODO: log error OS-specific
+		return
+	}
+
+	console := js.Global().Get("console")
+	console.Call("log", line)
+}