github.com/grupokindynos/coins-explorer@v0.0.0-20210507172551-fa8983d19250/server/internal.go (about)

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