2018-01-18 08:44:52 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-09-19 07:59:49 -06:00
|
|
|
"blockbook/api"
|
2018-01-31 07:23:17 -07:00
|
|
|
"blockbook/bchain"
|
2018-06-12 14:57:46 -06:00
|
|
|
"blockbook/common"
|
2018-01-18 08:44:52 -07:00
|
|
|
"blockbook/db"
|
2018-01-18 09:33:20 -07:00
|
|
|
"context"
|
2018-01-18 08:44:52 -07:00
|
|
|
"encoding/json"
|
2018-01-18 15:05:26 -07:00
|
|
|
"fmt"
|
2018-01-18 08:44:52 -07:00
|
|
|
"net/http"
|
|
|
|
|
2018-01-30 10:22:25 -07:00
|
|
|
"github.com/golang/glog"
|
|
|
|
|
2018-03-13 04:34:49 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2018-01-18 08:44:52 -07:00
|
|
|
)
|
|
|
|
|
2018-06-21 07:37:46 -06:00
|
|
|
// InternalServer is handle to internal http server
|
|
|
|
type InternalServer struct {
|
2018-03-08 04:59:37 -07:00
|
|
|
https *http.Server
|
|
|
|
certFiles string
|
|
|
|
db *db.RocksDB
|
|
|
|
txCache *db.TxCache
|
|
|
|
chain bchain.BlockChain
|
|
|
|
chainParser bchain.BlockChainParser
|
2018-06-12 14:57:46 -06:00
|
|
|
is *common.InternalState
|
2018-09-19 07:59:49 -06:00
|
|
|
api *api.Worker
|
2018-01-18 08:44:52 -07:00
|
|
|
}
|
|
|
|
|
2018-06-21 07:37:46 -06:00
|
|
|
// NewInternalServer creates new internal http interface to blockbook and returns its handle
|
2018-09-19 07:59:49 -06:00
|
|
|
func NewInternalServer(binding, certFiles string, db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxCache, is *common.InternalState) (*InternalServer, error) {
|
|
|
|
api, err := api.NewWorker(db, chain, txCache, is)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, path := splitBinding(binding)
|
|
|
|
serveMux := http.NewServeMux()
|
2018-01-18 08:44:52 -07:00
|
|
|
https := &http.Server{
|
2018-09-19 07:59:49 -06:00
|
|
|
Addr: addr,
|
|
|
|
Handler: serveMux,
|
2018-01-18 08:44:52 -07:00
|
|
|
}
|
2018-06-21 07:37:46 -06:00
|
|
|
s := &InternalServer{
|
2018-03-08 04:59:37 -07:00
|
|
|
https: https,
|
|
|
|
certFiles: certFiles,
|
|
|
|
db: db,
|
|
|
|
txCache: txCache,
|
|
|
|
chain: chain,
|
|
|
|
chainParser: chain.GetChainParser(),
|
2018-06-12 14:57:46 -06:00
|
|
|
is: is,
|
2018-09-19 07:59:49 -06:00
|
|
|
api: api,
|
2018-01-18 08:44:52 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 03:44:42 -06:00
|
|
|
serveMux.Handle(path+"favicon.ico", http.FileServer(http.Dir("./static/")))
|
2018-09-19 07:59:49 -06:00
|
|
|
serveMux.HandleFunc(path+"metrics", promhttp.Handler().ServeHTTP)
|
|
|
|
serveMux.HandleFunc(path, s.index)
|
2018-01-18 08:44:52 -07:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 15:05:26 -07:00
|
|
|
// Run starts the server
|
2018-06-21 07:37:46 -06:00
|
|
|
func (s *InternalServer) Run() error {
|
2018-02-07 12:42:25 -07:00
|
|
|
if s.certFiles == "" {
|
2018-06-21 07:37:46 -06:00
|
|
|
glog.Info("internal server: starting to listen on http://", s.https.Addr)
|
2018-02-07 12:42:25 -07:00
|
|
|
return s.https.ListenAndServe()
|
|
|
|
}
|
2018-06-21 07:37:46 -06:00
|
|
|
glog.Info("internal server: starting to listen on https://", s.https.Addr)
|
2018-02-07 12:42:25 -07:00
|
|
|
return s.https.ListenAndServeTLS(fmt.Sprint(s.certFiles, ".crt"), fmt.Sprint(s.certFiles, ".key"))
|
2018-01-18 08:44:52 -07:00
|
|
|
}
|
|
|
|
|
2018-01-18 15:05:26 -07:00
|
|
|
// Close closes the server
|
2018-06-21 07:37:46 -06:00
|
|
|
func (s *InternalServer) Close() error {
|
|
|
|
glog.Infof("internal server: closing")
|
2018-01-18 08:44:52 -07:00
|
|
|
return s.https.Close()
|
|
|
|
}
|
|
|
|
|
2018-01-18 15:05:26 -07:00
|
|
|
// Shutdown shuts down the server
|
2018-06-21 07:37:46 -06:00
|
|
|
func (s *InternalServer) Shutdown(ctx context.Context) error {
|
|
|
|
glog.Infof("internal server: shutdown")
|
2018-01-18 09:33:20 -07:00
|
|
|
return s.https.Shutdown(ctx)
|
2018-01-18 08:44:52 -07:00
|
|
|
}
|
|
|
|
|
2018-06-21 07:37:46 -06:00
|
|
|
func (s *InternalServer) index(w http.ResponseWriter, r *http.Request) {
|
2018-09-19 07:59:49 -06:00
|
|
|
si, err := s.api.GetSystemInfo(true)
|
|
|
|
buf, err := json.MarshalIndent(si, "", " ")
|
2018-01-25 04:31:31 -07:00
|
|
|
if err != nil {
|
2018-06-12 14:57:46 -06:00
|
|
|
glog.Error(err)
|
2018-01-25 04:31:31 -07:00
|
|
|
}
|
2018-06-12 14:57:46 -06:00
|
|
|
w.Write(buf)
|
2018-01-18 08:44:52 -07:00
|
|
|
}
|