forked from 0xERR0R/blocky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): deduplicate HTTP server setup with new
httpServer
- Loading branch information
1 parent
17b2a94
commit 35b1c16
Showing
2 changed files
with
85 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type httpServer struct { | ||
inner http.Server | ||
|
||
name string | ||
} | ||
|
||
func newHTTPServer(name string, handler http.Handler) *httpServer { | ||
const ( | ||
readHeaderTimeout = 20 * time.Second | ||
readTimeout = 20 * time.Second | ||
writeTimeout = 20 * time.Second | ||
) | ||
|
||
return &httpServer{ | ||
inner: http.Server{ | ||
ReadTimeout: readTimeout, | ||
ReadHeaderTimeout: readHeaderTimeout, | ||
WriteTimeout: writeTimeout, | ||
|
||
Handler: handler, | ||
}, | ||
|
||
name: name, | ||
} | ||
} | ||
|
||
func (s *httpServer) String() string { | ||
return s.name | ||
} | ||
|
||
func (s *httpServer) Serve(ctx context.Context, l net.Listener) error { | ||
go func() { | ||
<-ctx.Done() | ||
|
||
s.inner.Close() | ||
}() | ||
|
||
return s.inner.Serve(l) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters