mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
server cleanup
This commit is contained in:
parent
18897d2e32
commit
1449d6902c
@ -9,12 +9,9 @@ import (
|
|||||||
"github.com/foomo/contentserver/requests"
|
"github.com/foomo/contentserver/requests"
|
||||||
"github.com/foomo/contentserver/responses"
|
"github.com/foomo/contentserver/responses"
|
||||||
"github.com/foomo/contentserver/status"
|
"github.com/foomo/contentserver/status"
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
||||||
|
|
||||||
func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, metrics *status.Metrics) (replyBytes []byte, err error) {
|
func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, metrics *status.Metrics) (replyBytes []byte, err error) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@ -8,11 +8,14 @@ import (
|
|||||||
|
|
||||||
"github.com/foomo/contentserver/log"
|
"github.com/foomo/contentserver/log"
|
||||||
"github.com/foomo/contentserver/repo"
|
"github.com/foomo/contentserver/repo"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
|
||||||
// profiling
|
// profiling
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
// Handler type
|
// Handler type
|
||||||
type Handler string
|
type Handler string
|
||||||
|
|
||||||
@ -64,12 +67,7 @@ func runWebserver(
|
|||||||
path string,
|
path string,
|
||||||
chanErr chan error,
|
chanErr chan error,
|
||||||
) {
|
) {
|
||||||
s, errNew := NewWebServer(path, r)
|
chanErr <- http.ListenAndServe(address, NewWebServer(path, r))
|
||||||
if errNew != nil {
|
|
||||||
chanErr <- errNew
|
|
||||||
return
|
|
||||||
}
|
|
||||||
chanErr <- http.ListenAndServe(address, s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runSocketServer(
|
func runSocketServer(
|
||||||
@ -78,12 +76,7 @@ func runSocketServer(
|
|||||||
chanErr chan error,
|
chanErr chan error,
|
||||||
) {
|
) {
|
||||||
// create socket server
|
// create socket server
|
||||||
s, errSocketServer := newSocketServer(repo)
|
s := newSocketServer(repo)
|
||||||
if errSocketServer != nil {
|
|
||||||
log.Error(errSocketServer)
|
|
||||||
chanErr <- errSocketServer
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// listen on socket
|
// listen on socket
|
||||||
ln, errListen := net.Listen("tcp", address)
|
ln, errListen := net.Listen("tcp", address)
|
||||||
|
|||||||
@ -18,14 +18,12 @@ type socketServer struct {
|
|||||||
metrics *status.Metrics
|
metrics *status.Metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
// @TODO: remove error ?
|
|
||||||
// newSocketServer returns a shiny new socket server
|
// newSocketServer returns a shiny new socket server
|
||||||
func newSocketServer(repo *repo.Repo) (s *socketServer, err error) {
|
func newSocketServer(repo *repo.Repo) *socketServer {
|
||||||
s = &socketServer{
|
return &socketServer{
|
||||||
repo: repo,
|
repo: repo,
|
||||||
metrics: status.NewMetrics("socketserver"),
|
metrics: status.NewMetrics("socketserver"),
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractHandlerAndJSONLentgh(header string) (handler Handler, jsonLength int, err error) {
|
func extractHandlerAndJSONLentgh(header string) (handler Handler, jsonLength int, err error) {
|
||||||
@ -104,12 +102,17 @@ func (s *socketServer) handleConnection(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
log.Debug(fmt.Sprintf(" found json with %d bytes", jsonLength))
|
log.Debug(fmt.Sprintf(" found json with %d bytes", jsonLength))
|
||||||
if jsonLength > 0 {
|
if jsonLength > 0 {
|
||||||
// let us try to read some json
|
|
||||||
jsonBytes := make([]byte, jsonLength)
|
var (
|
||||||
|
// let us try to read some json
|
||||||
|
jsonBytes = make([]byte, jsonLength)
|
||||||
|
jsonLengthCurrent = 1
|
||||||
|
readRound = 0
|
||||||
|
)
|
||||||
|
|
||||||
// that is "{"
|
// that is "{"
|
||||||
jsonBytes[0] = 123
|
jsonBytes[0] = 123
|
||||||
jsonLengthCurrent := 1
|
|
||||||
readRound := 0
|
|
||||||
for jsonLengthCurrent < jsonLength {
|
for jsonLengthCurrent < jsonLength {
|
||||||
readRound++
|
readRound++
|
||||||
readLength, jsonReadErr := conn.Read(jsonBytes[jsonLengthCurrent:jsonLength])
|
readLength, jsonReadErr := conn.Read(jsonBytes[jsonLengthCurrent:jsonLength])
|
||||||
|
|||||||
@ -12,19 +12,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type webServer struct {
|
type webServer struct {
|
||||||
r *repo.Repo
|
|
||||||
path string
|
path string
|
||||||
|
r *repo.Repo
|
||||||
metrics *status.Metrics
|
metrics *status.Metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWebServer returns a shiny new web server
|
// NewWebServer returns a shiny new web server
|
||||||
func NewWebServer(path string, r *repo.Repo) (s http.Handler, err error) {
|
func NewWebServer(path string, r *repo.Repo) http.Handler {
|
||||||
s = &webServer{
|
return &webServer{
|
||||||
r: r,
|
|
||||||
path: path,
|
path: path,
|
||||||
|
r: r,
|
||||||
metrics: status.NewMetrics("webserver"),
|
metrics: status.NewMetrics("webserver"),
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user