github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/server/internal.go (about)

     1  package server
     2  
     3  import (
     4  	"blockbook/api"
     5  	"blockbook/bchain"
     6  	"blockbook/common"
     7  	"blockbook/db"
     8  	"context"
     9  	"encoding/json"
    10  	"fmt"
    11  	"net/http"
    12  
    13  	"github.com/golang/glog"
    14  
    15  	"github.com/prometheus/client_golang/prometheus/promhttp"
    16  )
    17  
    18  // InternalServer is handle to internal http server
    19  type InternalServer struct {
    20  	https       *http.Server
    21  	certFiles   string
    22  	db          *db.RocksDB
    23  	txCache     *db.TxCache
    24  	chain       bchain.BlockChain
    25  	chainParser bchain.BlockChainParser
    26  	is          *common.InternalState
    27  	api         *api.Worker
    28  }
    29  
    30  // NewInternalServer creates new internal http interface to blockbook and returns its handle
    31  func NewInternalServer(binding, certFiles string, db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxCache, is *common.InternalState) (*InternalServer, error) {
    32  	api, err := api.NewWorker(db, chain, txCache, is)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	addr, path := splitBinding(binding)
    38  	serveMux := http.NewServeMux()
    39  	https := &http.Server{
    40  		Addr:    addr,
    41  		Handler: serveMux,
    42  	}
    43  	s := &InternalServer{
    44  		https:       https,
    45  		certFiles:   certFiles,
    46  		db:          db,
    47  		txCache:     txCache,
    48  		chain:       chain,
    49  		chainParser: chain.GetChainParser(),
    50  		is:          is,
    51  		api:         api,
    52  	}
    53  
    54  	serveMux.Handle(path+"favicon.ico", http.FileServer(http.Dir("./static/")))
    55  	serveMux.HandleFunc(path+"metrics", promhttp.Handler().ServeHTTP)
    56  	serveMux.HandleFunc(path, s.index)
    57  
    58  	return s, nil
    59  }
    60  
    61  // Run starts the server
    62  func (s *InternalServer) Run() error {
    63  	if s.certFiles == "" {
    64  		glog.Info("internal server: starting to listen on http://", s.https.Addr)
    65  		return s.https.ListenAndServe()
    66  	}
    67  	glog.Info("internal server: starting to listen on https://", s.https.Addr)
    68  	return s.https.ListenAndServeTLS(fmt.Sprint(s.certFiles, ".crt"), fmt.Sprint(s.certFiles, ".key"))
    69  }
    70  
    71  // Close closes the server
    72  func (s *InternalServer) Close() error {
    73  	glog.Infof("internal server: closing")
    74  	return s.https.Close()
    75  }
    76  
    77  // Shutdown shuts down the server
    78  func (s *InternalServer) Shutdown(ctx context.Context) error {
    79  	glog.Infof("internal server: shutdown")
    80  	return s.https.Shutdown(ctx)
    81  }
    82  
    83  func (s *InternalServer) index(w http.ResponseWriter, r *http.Request) {
    84  	si, err := s.api.GetSystemInfo(true)
    85  	if err != nil {
    86  		glog.Error(err)
    87  		w.WriteHeader(http.StatusInternalServerError)
    88  		return
    89  	}
    90  	buf, err := json.MarshalIndent(si, "", "    ")
    91  	if err != nil {
    92  		glog.Error(err)
    93  		w.WriteHeader(http.StatusInternalServerError)
    94  		return
    95  	}
    96  
    97  	w.Write(buf)
    98  }