blockbook/server/internal.go

100 lines
2.6 KiB
Go
Raw Permalink Normal View History

2018-01-18 08:44:52 -07:00
package server
import (
"context"
2018-01-18 08:44:52 -07:00
"encoding/json"
"fmt"
2018-01-18 08:44:52 -07:00
"net/http"
"github.com/golang/glog"
2018-03-13 04:34:49 -06:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2021-04-05 12:59:11 -06:00
"spacecruft.org/spacecruft/blockbook/api"
"spacecruft.org/spacecruft/blockbook/bchain"
"spacecruft.org/spacecruft/blockbook/common"
"spacecruft.org/spacecruft/blockbook/db"
2018-01-18 08:44:52 -07:00
)
// InternalServer is handle to internal http server
type InternalServer struct {
https *http.Server
certFiles string
db *db.RocksDB
txCache *db.TxCache
chain bchain.BlockChain
chainParser bchain.BlockChainParser
mempool bchain.Mempool
is *common.InternalState
2018-09-19 07:59:49 -06:00
api *api.Worker
2018-01-18 08:44:52 -07:00
}
// NewInternalServer creates new internal http interface to blockbook and returns its handle
func NewInternalServer(binding, certFiles string, db *db.RocksDB, chain bchain.BlockChain, mempool bchain.Mempool, txCache *db.TxCache, is *common.InternalState) (*InternalServer, error) {
api, err := api.NewWorker(db, chain, mempool, txCache, is)
2018-09-19 07:59:49 -06:00
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
}
s := &InternalServer{
https: https,
certFiles: certFiles,
db: db,
txCache: txCache,
chain: chain,
chainParser: chain.GetChainParser(),
mempool: mempool,
is: is,
2018-09-19 07:59:49 -06:00
api: api,
2018-01-18 08:44:52 -07: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
}
// Run starts the server
func (s *InternalServer) Run() error {
if s.certFiles == "" {
glog.Info("internal server: starting to listen on http://", s.https.Addr)
return s.https.ListenAndServe()
}
glog.Info("internal server: starting to listen on https://", s.https.Addr)
return s.https.ListenAndServeTLS(fmt.Sprint(s.certFiles, ".crt"), fmt.Sprint(s.certFiles, ".key"))
2018-01-18 08:44:52 -07:00
}
// Close closes the server
func (s *InternalServer) Close() error {
glog.Infof("internal server: closing")
2018-01-18 08:44:52 -07:00
return s.https.Close()
}
// Shutdown shuts down the server
func (s *InternalServer) Shutdown(ctx context.Context) error {
glog.Infof("internal server: shutdown")
return s.https.Shutdown(ctx)
2018-01-18 08:44:52 -07: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)
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
2018-09-19 07:59:49 -06:00
buf, err := json.MarshalIndent(si, "", " ")
if err != nil {
glog.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(buf)
2018-01-18 08:44:52 -07:00
}