-
Notifications
You must be signed in to change notification settings - Fork 501
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a81222d
Incorporate subcommands into the web server
Shaptic 0e090b3
Add correct subcommand for serving
Shaptic df6fffa
Move OpenAPI spec under its own URL
Shaptic fd697e9
PR feedback: drop useless main.go & improve docs
Shaptic f2e013d
Add default behavior to / and tests
Shaptic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.