<commit_before>package blockchain

import (
	"net/http"
	"time"

	log "github.com/sirupsen/logrus"

	"github.com/bytom/blockchain/accesstoken"
	"github.com/bytom/dashboard"
	"github.com/bytom/errors"
	"github.com/bytom/net/http/authn"
	"github.com/bytom/net/http/httpjson"
	"github.com/bytom/net/http/static"
)

var (
	errNotAuthenticated = errors.New("not authenticated")
)

// json handler
func jsonHandler(f interface{}) http.Handler {
	h, err := httpjson.Handler(f, errorFormatter.Write)
	if err != nil {
		panic(err)
	}
	return h
}

// error handler
func alwaysError(err error) http.Handler {
	return jsonHandler(func() error { return err })
}

// serve http
func (bcr *BlockchainReactor) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	bcr.handler.ServeHTTP(rw, req)
}

func webAssetsHandler(next http.Handler) http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/dashboard/", http.StripPrefix("/dashboard/", static.Handler{
		Assets:  dashboard.Files,
		Default: "index.html",
	}))
	mux.Handle("/", next)

	return mux
}

// BuildHandler is in charge of all the rpc handling.
func (bcr *BlockchainReactor) BuildHandler() {
	m := bcr.mux
	if bcr.accounts != nil && bcr.assets != nil {
		m.Handle("/create-account", jsonHandler(bcr.createAccount))
		m.Handle("/update-account-tags", jsonHandler(bcr.updateAccountTags))
		m.Handle("/create-account-address", jsonHandler(bcr.createAccountAddress))
		m.Handle("/list-accounts", jsonHandler(bcr.listAccounts))
		m.Handle("/delete-account", jsonHandler(bcr.deleteAccount))

		m.Handle("/create-asset", jsonHandler(bcr.createAsset))
		m.Handle("/update-asset-alias", jsonHandler(bcr.updateAssetAlias))
		m.Handle("/update-asset-tags", jsonHandler(bcr.updateAssetTags))
		m.Handle("/list-assets", jsonHandler(bcr.listAssets))

		m.Handle("/create-key", jsonHandler(bcr.pseudohsmCreateKey))
		m.Handle("/list-keys", jsonHandler(bcr.pseudohsmListKeys))<commit_msg>fix bytomd panic when launch more web dashboard (#358)<commit_after>package blockchain

import (
	"net/http"
	"time"

	log "github.com/sirupsen/logrus"

	"github.com/bytom/blockchain/accesstoken"
	"github.com/bytom/dashboard"
	"github.com/bytom/errors"
	"github.com/bytom/net/http/authn"
	"github.com/bytom/net/http/httpjson"
	"github.com/bytom/net/http/static"
)

var (
	errNotAuthenticated = errors.New("not authenticated")
)

// json handler
func jsonHandler(f interface{}) http.Handler {
	h, err := httpjson.Handler(f, errorFormatter.Write)
	if err != nil {
		panic(err)
	}
	return h
}

// error handler
func alwaysError(err error) http.Handler {
	return jsonHandler(func() error { return err })
}

// serve http
func (bcr *BlockchainReactor) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	if bcr.handler != nil {
		bcr.handler.ServeHTTP(rw, req)
	}
}

func webAssetsHandler(next http.Handler) http.Handler {
	mux := http.NewServeMux()
	mux.Handle("/dashboard/", http.StripPrefix("/dashboard/", static.Handler{
		Assets:  dashboard.Files,
		Default: "index.html",
	}))
	mux.Handle("/", next)

	return mux
}

// BuildHandler is in charge of all the rpc handling.
func (bcr *BlockchainReactor) BuildHandler() {
	m := bcr.mux
	if bcr.accounts != nil && bcr.assets != nil {
		m.Handle("/create-account", jsonHandler(bcr.createAccount))
		m.Handle("/update-account-tags", jsonHandler(bcr.updateAccountTags))
		m.Handle("/create-account-address", jsonHandler(bcr.createAccountAddress))
		m.Handle("/list-accounts", jsonHandler(bcr.listAccounts))
		m.Handle("/delete-account", jsonHandler(bcr.deleteAccount))

		m.Handle("/create-asset", jsonHandler(bcr.createAsset))
		m.Handle("/update-asset-alias", jsonHandler(bcr.updateAssetAlias))
		m.Handle("/update-asset-tags", jsonHandler(bcr.updateAssetTags))
		m.Handle("/list-assets", jsonHandler(bcr.listAssets))

		m.Handle("/create-key", jsonHandler(bcr.pseudohsmCreateKey))
		m.Handle("/list-keys", jsonHandler(bcr.pseudohsmListKeys))
{'fraction_non_alphanumeric': 0.15805626598465472, 'fraction_numerical': 0.0007672634271099745, 'mean_word_length': 4.934749620637329, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '207177', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1374, 'n_words': 238}