Skip to content

Commit

Permalink
Merge branch 'master' into monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 authored Apr 21, 2022
2 parents fc511ce + c3b107c commit 74ead53
Show file tree
Hide file tree
Showing 16 changed files with 4,314 additions and 1,716 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ linters-settings:
max-blank-identifiers: 2
dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
threshold: 200
errcheck:
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
Expand Down
15 changes: 7 additions & 8 deletions actions/access_keys/search.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package accessKeys

import (
"encoding/json"
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux-server/actions"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)
Expand All @@ -16,21 +16,20 @@ func (a *Action) search(w http.ResponseWriter, req *http.Request, _ httprouter.P

// Parse the params
params := apirouter.GetParams(req)
metadataReq := params.GetJSON(bux.ModelMetadata.String())
var metadata *bux.Metadata
if len(metadataReq) > 0 {
// marshal the metadata into the Metadata model
metaJSON, _ := json.Marshal(metadataReq) // nolint: errchkjson // ignore for now
_ = json.Unmarshal(metaJSON, &metadata)
queryParams, metadata, conditions, err := actions.GetQueryParameters(params)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Record a new transaction (get the hex from parameters)a
var err error
var accessKeys []*bux.AccessKey
if accessKeys, err = a.Services.Bux.GetAccessKeys(
req.Context(),
reqXPubID,
metadata,
conditions,
queryParams,
); err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
Expand Down
17 changes: 7 additions & 10 deletions actions/destinations/search.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package destinations

import (
"encoding/json"
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux-server/actions"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)
Expand All @@ -16,23 +16,20 @@ func (a *Action) search(w http.ResponseWriter, req *http.Request, _ httprouter.P

// Parse the params
params := apirouter.GetParams(req)

// todo GetJSON should be able to marshal into a model
metadataReq := params.GetJSON(bux.ModelMetadata.String())
var metadata *bux.Metadata
if len(metadataReq) > 0 {
// marshal the metadata into the Metadata model
metaJSON, _ := json.Marshal(metadataReq) // nolint: errchkjson // ignore for now
_ = json.Unmarshal(metaJSON, &metadata)
queryParams, metadata, conditions, err := actions.GetQueryParameters(params)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Record a new transaction (get the hex from parameters)a
var err error
var destinations []*bux.Destination
if destinations, err = a.Services.Bux.GetDestinations(
req.Context(),
reqXPubID,
metadata,
conditions,
queryParams,
); err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
Expand Down
38 changes: 37 additions & 1 deletion actions/graphql/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"net/http"
"regexp"

"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/BuxOrg/bux"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/BuxOrg/bux-server/dictionary"
"github.com/BuxOrg/bux-server/graph"
"github.com/BuxOrg/bux-server/graph/generated"
"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
"github.com/mrz1836/go-logger"
Expand All @@ -26,6 +29,14 @@ const (
allowOriginHeader string = "Access-Control-Allow-Origin"
)

type requestInfo struct {
id uuid.UUID
method string
path string
ip string
userAgent string
}

// RegisterRoutes register all the package specific routes
func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, services *config.AppServices) {

Expand All @@ -39,12 +50,28 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi
serverPath = defaultServerPath
}

srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
if appConfig.RequestLogging {
re := regexp.MustCompile(`[\r?\n|\s+]`)
srv.AroundOperations(func(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
oc := graphql.GetOperationContext(ctx)
reqInfo := ctx.Value(config.GraphRequestInfo).(requestInfo)
params := map[string]interface{}{
"query": re.ReplaceAllString(oc.RawQuery, " "),
"variables": oc.Variables,
}
// LogParamsFormat "request_id=\"%s\" method=\"%s\" path=\"%s\" ip_address=\"%s\" user_agent=\"%s\" params=\"%v\"\n"
logger.NoFilePrintf(apirouter.LogParamsFormat, reqInfo.id, reqInfo.method, reqInfo.path, reqInfo.ip, reqInfo.userAgent, params)
return next(ctx)
})
}

// Set the handle
h := require.Wrap(wrapHandler(
router,
a.AppConfig,
a.Services,
handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}})),
srv,
true,
))

Expand Down Expand Up @@ -124,6 +151,15 @@ func wrapHandler(router *apirouter.Router, appConfig *config.AppConfig, services
AuthError: err,
})

guid, _ := uuid.NewV4()
ctx = context.WithValue(ctx, config.GraphRequestInfo, requestInfo{
id: guid,
method: req.Method,
path: req.RequestURI,
ip: req.RemoteAddr,
userAgent: req.UserAgent(),
})

// Call your original http.Handler
h.ServeHTTP(w, req.WithContext(ctx))
}
Expand Down
35 changes: 35 additions & 0 deletions actions/methods.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package actions

import (
"encoding/json"
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux-server/dictionary"
"github.com/BuxOrg/bux/datastore"
"github.com/julienschmidt/httprouter"
"github.com/mrz1836/go-parameters"
"github.com/newrelic/go-agent/v3/newrelic"
)

Expand Down Expand Up @@ -45,3 +49,34 @@ func MethodNotAllowed(w http.ResponseWriter, req *http.Request) {
req.Method,
)
}

// GetQueryParameters get all filtering parameters related to the db query
func GetQueryParameters(params *parameters.Params) (*datastore.QueryParams, *bux.Metadata, *map[string]interface{}, error) {
queryParams := &datastore.QueryParams{
Page: params.GetInt("page"),
PageSize: params.GetInt("page_size"),
OrderByField: params.GetString("order_by_field"),
SortDirection: params.GetString("sort_direction"),
}

metadataReq := params.GetJSON(MetadataField)
var metadata *bux.Metadata
if len(metadataReq) > 0 {
// marshal the metadata into the Metadata model
metaJSON, _ := json.Marshal(metadataReq) // nolint: errchkjson // ignore for now
if err := json.Unmarshal(metaJSON, &metadata); err != nil {
return nil, nil, nil, err
}
}
conditionsReq := params.GetJSON("conditions")
var conditions *map[string]interface{}
if len(conditionsReq) > 0 {
// marshal the conditions into the Map
conditionsJSON, _ := json.Marshal(conditionsReq) // nolint: errchkjson // ignore for now
if err := json.Unmarshal(conditionsJSON, &conditions); err != nil {
return nil, nil, nil, err
}
}

return queryParams, metadata, conditions, nil
}
21 changes: 5 additions & 16 deletions actions/transactions/search.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transactions

import (
"encoding/json"
"net/http"

"github.com/BuxOrg/bux"
Expand All @@ -17,30 +16,20 @@ func (a *Action) search(w http.ResponseWriter, req *http.Request, _ httprouter.P

// Parse the params
params := apirouter.GetParams(req)

metadataReq := params.GetJSON(actions.MetadataField)
var metadata *bux.Metadata
if len(metadataReq) > 0 {
// marshal the metadata into the Metadata model
metaJSON, _ := json.Marshal(metadataReq) // nolint: errchkjson // ignore for now
_ = json.Unmarshal(metaJSON, &metadata)
}
conditionsReq := params.GetJSON("conditions")
var conditions *map[string]interface{}
if len(conditionsReq) > 0 {
// marshal the conditions into the Map
conditionsJSON, _ := json.Marshal(conditionsReq) // nolint: errchkjson // ignore for now
_ = json.Unmarshal(conditionsJSON, &conditions)
queryParams, metadata, conditions, err := actions.GetQueryParameters(params)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Record a new transaction (get the hex from parameters)a
var err error
var transactions []*bux.Transaction
if transactions, err = a.Services.Bux.GetTransactions(
req.Context(),
reqXPubID,
metadata,
conditions,
queryParams,
); err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
Expand Down
6 changes: 5 additions & 1 deletion actions/utxos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux/utils"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)
Expand All @@ -18,7 +19,10 @@ func (a *Action) get(w http.ResponseWriter, req *http.Request, _ httprouter.Para
var utxos []*bux.Utxo
if utxos, err = a.Services.Bux.GetUtxos(
req.Context(),
params.GetString("key"),
utils.Hash(params.GetString("key")),
nil,
nil,
nil,
); err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
Expand Down
1 change: 1 addition & 0 deletions actions/utxos/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi

// V1 Requests
router.HTTPRouter.GET("/"+config.CurrentMajorVersion+"/utxo", action.Request(router, require.Wrap(action.get)))
router.HTTPRouter.POST("/"+config.CurrentMajorVersion+"/utxo/search", action.Request(router, require.Wrap(action.search)))
}
40 changes: 40 additions & 0 deletions actions/utxos/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utxos

import (
"net/http"

"github.com/BuxOrg/bux"
"github.com/BuxOrg/bux-server/actions"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// search will fetch a list of transactions filtered on conditions and metadata
func (a *Action) search(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {

reqXPubID, _ := bux.GetXpubIDFromRequest(req)

// Parse the params
params := apirouter.GetParams(req)
queryParams, metadata, conditions, err := actions.GetQueryParameters(params)
if err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Record a new transaction (get the hex from parameters)a
var utxos []*bux.Utxo
if utxos, err = a.Services.Bux.GetUtxos(
req.Context(),
reqXPubID,
metadata,
conditions,
queryParams,
); err != nil {
apirouter.ReturnResponse(w, req, http.StatusExpectationFailed, err.Error())
return
}

// Return response
apirouter.ReturnResponse(w, req, http.StatusOK, bux.DisplayModels(utxos))
}
3 changes: 3 additions & 0 deletions config/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ type graphContextKey string
var (
// GraphConfigKey is the ctx key for the
GraphConfigKey graphContextKey = "graphql_config"

// GraphRequestInfo is the ctx key for the request info passed down to graphql for logging
GraphRequestInfo graphContextKey = "request_info"
)
Loading

0 comments on commit 74ead53

Please sign in to comment.