Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp/lighthorizon: Incorporate tool subcommands into the webserver. #4579

Merged
merged 5 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions exp/lighthorizon/actions/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package actions

import (
"encoding/json"
"net/http"

"github.com/stellar/go/support/log"
supportProblem "github.com/stellar/go/support/render/problem"
)

type RootResponse struct {
Version string `json:"version"`
LedgerSource string `json:"ledger_source"`
IndexSource string `json:"index_source"`
LatestLedger uint32 `json:"latest_indexed_ledger"`
}

func Root(config RootResponse) func(http.ResponseWriter, *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, is it considered slightly more idiomatic/standard to represent as struct method:
func (config *RootResponse) Handle func(http.ResponseWriter, *http.Request)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly? I have no idea. I was following the pattern for the other actions.

return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/hal+json; charset=utf-8")
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(config)
if err != nil {
log.Error(err)
sendErrorResponse(r.Context(), w, supportProblem.ServerError)
}
}
}
7 changes: 4 additions & 3 deletions exp/lighthorizon/actions/static/api_docs.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
openapi: 3.0.3
openapi: 3.1.0
info:
title: Horizon Light API
title: Horizon Lite API
version: 0.0.1
description: |-
The Light API is a published web service on port parameter specified when running Horizon Light `--port=<port>`.
The Horizon Lite API is a published web service on port 8080. It's considered
extremely experimental and only provides a minimal subset of endpoints.
servers:
- url: http://localhost:8080/
paths:
Expand Down
2 changes: 0 additions & 2 deletions exp/lighthorizon/build/k8s/lighthorizon_web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,3 @@ spec:
- hosts:
- lighthorizon-pubnet.prototypes.kube001.services.stellar-ops.com
secretName: lighthorizon-pubnet-web-cert


10 changes: 4 additions & 6 deletions exp/lighthorizon/build/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ RUN apt-get clean

COPY --from=builder /go/bin/lighthorizon ./

ENTRYPOINT ./lighthorizon \
-source "$TXMETA_SOURCE" \
-indexes "$INDEXES_SOURCE" \
-network-passphrase "$NETWORK_PASSPHRASE" \
-ledger-cache "$CACHE_PATH"

ENTRYPOINT ./lighthorizon serve \
--network-passphrase "$NETWORK_PASSPHRASE" \
--ledger-cache "$CACHE_PATH" \
"$TXMETA_SOURCE" "$INDEXES_SOURCE" \
6 changes: 5 additions & 1 deletion exp/lighthorizon/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ func lightHorizonHTTPHandler(registry *prometheus.Registry, lightHorizon service
r.MethodFunc(http.MethodGet, "/operations", actions.NewOpsByAccountHandler(lightHorizon))
})

router.MethodFunc(http.MethodGet, "/", actions.ApiDocs())
router.MethodFunc(http.MethodGet, "/", actions.Root(actions.RootResponse{
Version: HorizonLiteVersion,
// by default, no other fields are known yet
}))
router.MethodFunc(http.MethodGet, "/api", actions.ApiDocs())
router.Method(http.MethodGet, "/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))

problem.RegisterHost("")
Expand Down
44 changes: 31 additions & 13 deletions exp/lighthorizon/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

Shaptic marked this conversation as resolved.
Show resolved Hide resolved
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/go/exp/lighthorizon/actions"
"github.com/stellar/go/exp/lighthorizon/services"
"github.com/stellar/go/support/render/problem"
)
Expand All @@ -20,22 +21,12 @@ func TestUnknownUrl(t *testing.T) {
request, err := http.NewRequest("GET", "/unknown", nil)
require.NoError(t, err)

mockOperationService := &services.MockOperationService{}
mockTransactionService := &services.MockTransactionService{}
registry := prometheus.NewRegistry()

lh := services.LightHorizon{
Operations: mockOperationService,
Transactions: mockTransactionService,
}

handler := lightHorizonHTTPHandler(registry, lh)
handler.ServeHTTP(recorder, request)
prepareTestHttpHandler().ServeHTTP(recorder, request)

resp := recorder.Result()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)

raw, err := ioutil.ReadAll(resp.Body)
raw, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

var problem problem.P
Expand All @@ -44,3 +35,30 @@ func TestUnknownUrl(t *testing.T) {
assert.Equal(t, "Resource Missing", problem.Title)
assert.Equal(t, "not_found", problem.Type)
}

func TestRootResponse(t *testing.T) {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err)

prepareTestHttpHandler().ServeHTTP(recorder, request)

var root actions.RootResponse
raw, err := io.ReadAll(recorder.Result().Body)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(raw, &root))
require.Equal(t, HorizonLiteVersion, root.Version)
}

func prepareTestHttpHandler() http.Handler {
mockOperationService := &services.MockOperationService{}
mockTransactionService := &services.MockTransactionService{}
registry := prometheus.NewRegistry()

lh := services.LightHorizon{
Operations: mockOperationService,
Transactions: mockTransactionService,
}

return lightHorizonHTTPHandler(registry, lh)
}
162 changes: 109 additions & 53 deletions exp/lighthorizon/main.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,137 @@
package main

import (
"flag"
"net/http"

"github.com/go-chi/chi"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/stellar/go/exp/lighthorizon/actions"
"github.com/stellar/go/exp/lighthorizon/archive"
"github.com/stellar/go/exp/lighthorizon/index"
"github.com/stellar/go/exp/lighthorizon/services"
"github.com/stellar/go/exp/lighthorizon/tools"

"github.com/stellar/go/network"
"github.com/stellar/go/support/log"
)

const (
defaultCacheSize = (60 * 60 * 24) / 6 // 1 day of ledgers @ 6s each
HorizonLiteVersion = "0.0.1-alpha"
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
defaultCacheSize = (60 * 60 * 24) / 6 // 1 day of ledgers @ 6s each
)

func main() {
sourceUrl := flag.String("source", "gcs://horizon-archive-poc", "history archive url to read txmeta files")
indexesUrl := flag.String("indexes", "file://indexes", "url of the indexes")
networkPassphrase := flag.String("network-passphrase", network.PublicNetworkPassphrase, "network passphrase")
cacheDir := flag.String("ledger-cache", "", `path to cache frequently-used ledgers;
if left empty, uses a temporary directory`)
cacheSize := flag.Int("ledger-cache-size", defaultCacheSize,
"number of ledgers to store in the cache")
logLevelParam := flag.String("log-level", "info",
"logging level, info, debug, warn, error, panic, fatal, trace, default is info")
flag.Parse()

L := log.WithField("service", "horizon-lite")
logLevel, err := logrus.ParseLevel(*logLevelParam)
if err != nil {
log.Warnf("Failed to parse -log-level '%s', defaulting to 'info'.", *logLevelParam)
logLevel = log.InfoLevel
}
L.SetLevel(logLevel)
L.Info("Starting lighthorizon!")

registry := prometheus.NewRegistry()
indexStore, err := index.ConnectWithConfig(index.StoreConfig{
URL: *indexesUrl,
Log: L.WithField("subservice", "index"),
Metrics: registry,
})
if err != nil {
panic(err)
}
log.SetLevel(logrus.InfoLevel) // default for subcommands

ingestArchive, err := archive.NewIngestArchive(archive.ArchiveConfig{
SourceUrl: *sourceUrl,
NetworkPassphrase: *networkPassphrase,
CacheDir: *cacheDir,
CacheSize: *cacheSize,
})
if err != nil {
panic(err)
cmd := &cobra.Command{
Use: "lighthorizon <subcommand>",
Long: "Horizon Lite command suite",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage() // require a subcommand
},
}
defer ingestArchive.Close()

Config := services.Config{
Archive: ingestArchive,
Passphrase: *networkPassphrase,
IndexStore: indexStore,
Metrics: services.NewMetrics(registry),
}
serve := &cobra.Command{
Use: "serve <txmeta source> <index source>",
Long: `Starts the Horizon Lite server, binding it to port 8080 on all
local interfaces of the host. You can refer to the OpenAPI documentation located
at the /api endpoint to see what endpoints are supported.

lightHorizon := services.LightHorizon{
Transactions: &services.TransactionRepository{
Config: Config,
},
Operations: &services.OperationRepository{
Config: Config,
The <txmeta source> should be a URL to meta archives from which to read unpacked
ledger files, while the <index source> should be a URL containing indices that
break down accounts by active ledgers.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Usage()
return
}

sourceUrl, indexStoreUrl := args[0], args[1]

networkPassphrase, _ := cmd.Flags().GetString("network-passphrase")
switch networkPassphrase {
case "testnet":
networkPassphrase = network.TestNetworkPassphrase
case "pubnet":
networkPassphrase = network.PublicNetworkPassphrase
}

cacheDir, _ := cmd.Flags().GetString("ledger-cache")
cacheSize, _ := cmd.Flags().GetUint("ledger-cache-size")
logLevelParam, _ := cmd.Flags().GetString("log-level")

L := log.WithField("service", "horizon-lite")
logLevel, err := logrus.ParseLevel(logLevelParam)
if err != nil {
log.Warnf("Failed to parse log level '%s', defaulting to 'info'.", logLevelParam)
logLevel = log.InfoLevel
}
L.SetLevel(logLevel)
L.Info("Starting lighthorizon!")

registry := prometheus.NewRegistry()
indexStore, err := index.ConnectWithConfig(index.StoreConfig{
URL: indexStoreUrl,
Log: L.WithField("service", "index"),
Metrics: registry,
})
if err != nil {
log.Fatal(err)
return
}

ingester, err := archive.NewIngestArchive(archive.ArchiveConfig{
SourceUrl: sourceUrl,
NetworkPassphrase: networkPassphrase,
CacheDir: cacheDir,
CacheSize: int(cacheSize),
})
if err != nil {
log.Fatal(err)
return
}

Config := services.Config{
Archive: ingester,
Passphrase: networkPassphrase,
IndexStore: indexStore,
Metrics: services.NewMetrics(registry),
}

lightHorizon := services.LightHorizon{
Transactions: &services.TransactionRepository{
Config: Config,
},
Operations: &services.OperationRepository{
Config: Config,
},
}

// Inject our config into the root response.
router := lightHorizonHTTPHandler(registry, lightHorizon).(*chi.Mux)
router.MethodFunc(http.MethodGet, "/", actions.Root(actions.RootResponse{
Version: HorizonLiteVersion,
LedgerSource: sourceUrl,
IndexSource: indexStoreUrl,
}))

log.Fatal(http.ListenAndServe(":8080", router))
},
}

log.Fatal(http.ListenAndServe(":8080", lightHorizonHTTPHandler(registry, lightHorizon)))
serve.Flags().String("log-level", "info",
"logging level: 'info', 'debug', 'warn', 'error', 'panic', 'fatal', or 'trace'")
serve.Flags().String("network-passphrase", "pubnet", "network passphrase")
serve.Flags().String("ledger-cache", "", "path to cache frequently-used ledgers; "+
"if left empty, uses a temporary directory")
serve.Flags().Uint("ledger-cache-size", defaultCacheSize,
"number of ledgers to store in the cache")

cmd.AddCommand(serve)
tools.AddCacheCommands(cmd)
tools.AddIndexCommands(cmd)
cmd.Execute()
}
4 changes: 2 additions & 2 deletions exp/lighthorizon/tools/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tools

import (
"context"
Expand All @@ -21,7 +21,7 @@ const (
defaultCacheCount = (60 * 60 * 24) / 5 // ~24hrs worth of ledgers
)

func addCacheCommands(parent *cobra.Command) *cobra.Command {
func AddCacheCommands(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "cache",
Long: "Manages the on-disk cache of ledgers.",
Expand Down
4 changes: 2 additions & 2 deletions exp/lighthorizon/tools/explorer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package tools

import (
"context"
Expand All @@ -24,7 +24,7 @@ var (
checkpointMgr = historyarchive.NewCheckpointManager(0)
)

func addIndexCommands(parent *cobra.Command) *cobra.Command {
func AddIndexCommands(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Long: "Lets you view details about an index source.",
Expand Down
25 changes: 0 additions & 25 deletions exp/lighthorizon/tools/main.go

This file was deleted.