Completely rework the HTTP app layer structure. Replace a logger with an event dispatcher. Adjust tests to the new app architecture.

This commit is contained in:
ErickSkrauch
2020-01-29 01:34:15 +03:00
parent fbfe9f4516
commit b2ee10f72f
8 changed files with 156 additions and 174 deletions

View File

@@ -2,9 +2,32 @@ package http
import (
"encoding/json"
"net"
"net/http"
"time"
)
type Emitter interface {
Emit(name string, args ...interface{})
}
func Serve(address string, handler http.Handler) error {
listener, err := net.Listen("tcp", address)
if err != nil {
return err
}
server := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 16,
Handler: handler,
}
return server.Serve(listener)
}
func NotFound(response http.ResponseWriter, _ *http.Request) {
data, _ := json.Marshal(map[string]string{
"status": "404",