func main() { logger.Init(logger.Config{"server.log", true}) client.ProxyCheckUrl = *checkProxyMSEndpoint + "/check/proxy" ps, err := db.New(*dbDir) if err != nil { logger.Errf("Creating proxy storage: %v", err) os.Exit(1) } s := checker.CheckerService{Ps: ps} s.Start() ms := microservice.Microservice{Addr: ":8000"} ms.RegEP( endpoint.Endpoint{ Path: "/proxylist/new", Method: "POST", EdpHandler: endpoint.Adapt(endpoint.EdpHandlerFunc(handleNewProxyList), mw.JsonConv(), mw.JsonHeaders(), db.ProxyStorageAdapter(ps)), ErrHandler: mw.DefaultJsonErrHandler{}, }, ) ms.RegEP( endpoint.Endpoint{ Path: "/proxylist", Method: "GET", EdpHandler: endpoint.Adapt(endpoint.EdpHandlerFunc(handleProxyList), mw.JsonConv(), mw.JsonHeaders(), db.ProxyStorageAdapter(ps)), ErrHandler: mw.DefaultJsonErrHandler{}, }, ) ms.Start() }
func JsonHeaders() endpoint.Adapter { return func(e endpoint.EdpHandler) endpoint.EdpHandler { return endpoint.EdpHandlerFunc(func(c context.Context, r http.ResponseWriter, req *http.Request) (*endpoint.EPErr, context.Context) { r.Header()["Content-type"] = []string{"application/json"} return e.Handle(c, r, req) }) } }
func JsonConv() endpoint.Adapter { return func(e endpoint.EdpHandler) endpoint.EdpHandler { return endpoint.EdpHandlerFunc(func(c context.Context, r http.ResponseWriter, req *http.Request) (*endpoint.EPErr, context.Context) { nc := context.WithValue(c, jsonConvKey, &JsonConverter{in: req.Body, out: r}) return e.Handle(nc, r, req) }) } }
func ProxyStorageAdapter(storage *ProxyStorage) endpoint.Adapter { return func(e endpoint.EdpHandler) endpoint.EdpHandler { return endpoint.EdpHandlerFunc(func(c context.Context, r http.ResponseWriter, req *http.Request) (*endpoint.EPErr, context.Context) { nc := context.WithValue(c, proxyStorageKey, storage) return e.Handle(nc, r, req) }) } }
func main() { logger.Init(logger.Config{"server.log", true}) ms := microservice.Microservice{Addr: ":6060"} ms.RegEP( endpoint.Endpoint{ Path: "/check/proxy", Method: "POST", EdpHandler: endpoint.Adapt(endpoint.EdpHandlerFunc(handleProxyCheck), mw.JsonConv(), mw.JsonHeaders()), ErrHandler: mw.DefaultJsonErrHandler{}, }, ) ms.Start() }