-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prover-stats-api): add prover stats API (#635)
Co-authored-by: xinran chen <lawliet@xinran-m1x.local> Co-authored-by: georgehao <haohongfan@gmail.com> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
- Loading branch information
1 parent
2d6a195
commit e3b451c
Showing
31 changed files
with
2,230 additions
and
42 deletions.
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,80 @@ | ||
name: ProverStatsAPI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- staging | ||
- develop | ||
- alpha | ||
paths: | ||
- 'prover-stats-api/**' | ||
- '.github/workflows/prover_stats_api.yml' | ||
pull_request: | ||
types: | ||
- opened | ||
- reopened | ||
- synchronize | ||
- ready_for_review | ||
paths: | ||
- 'prover-stats-api/**' | ||
- '.github/workflows/prover_stats_api.yml' | ||
|
||
defaults: | ||
run: | ||
working-directory: 'prover-stats-api' | ||
|
||
jobs: | ||
check: | ||
if: github.event.pull_request.draft == false | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.19.x | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Lint | ||
run: | | ||
rm -rf $HOME/.cache/golangci-lint | ||
make lint | ||
test: | ||
if: github.event.pull_request.draft == false | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.19.x | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Test | ||
run: | | ||
make test | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/codecov-action@v3 | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
with: | ||
flags: prover-stats-api | ||
goimports-lint: | ||
if: github.event.pull_request.draft == false | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.19.x | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Install goimports | ||
run: go install golang.org/x/tools/cmd/goimports | ||
- run: goimports -local scroll-tech/prover-stats-api/ -w . | ||
- run: go mod tidy | ||
# If there are any diffs from goimports or go mod tidy, fail. | ||
- name: Verify no changes from goimports and go mod tidy | ||
run: | | ||
if [ -n "$(git status --porcelain)" ]; then | ||
exit 1 | ||
fi |
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ use ( | |
./database | ||
./roller | ||
./tests/integration-test | ||
./prover-stats-api | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
/build/bin |
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,16 @@ | ||
.PHONY: clean build test | ||
|
||
build: | ||
GOBIN=$(PWD)/build/bin go build -o $(PWD)/build/bin/prover-stats-api ./cmd | ||
|
||
clean: ## Empty out the bin folder | ||
@rm -rf build/bin | ||
|
||
swag: | ||
swag init -g ./cmd/main.go | ||
|
||
test: | ||
go test -v $(PWD)/... | ||
|
||
lint: ## Lint the files - used for CI | ||
GOBIN=$(PWD)/build/bin go run ../build/lint.go |
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,30 @@ | ||
# prover-stats-api | ||
|
||
## how to get the prover-stats-api docs | ||
|
||
### 1. start the prover-stats-api server | ||
|
||
``` | ||
cd ./prover-stats-api | ||
go build -o prover-stats ./cmd/ | ||
./prover-stats --config=./conf/config.json | ||
``` | ||
|
||
you will get server run log | ||
``` | ||
Listening and serving HTTP on :8990 | ||
``` | ||
|
||
### 2. browse the documents | ||
|
||
open this documents in your browser | ||
``` | ||
http://localhost:8990/swagger/index.html | ||
``` | ||
|
||
## how to update the prover-stats-api docs | ||
|
||
``` | ||
cd ./prover-stats-api | ||
make swag | ||
``` |
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,96 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/scroll-tech/go-ethereum/log" | ||
"github.com/urfave/cli/v2" | ||
|
||
"scroll-tech/common/database" | ||
"scroll-tech/common/metrics" | ||
"scroll-tech/common/utils" | ||
"scroll-tech/common/version" | ||
|
||
"scroll-tech/prover-stats-api/internal/config" | ||
"scroll-tech/prover-stats-api/internal/controller" | ||
"scroll-tech/prover-stats-api/internal/route" | ||
) | ||
|
||
var app *cli.App | ||
|
||
func init() { | ||
// Set up prover-stats-api info. | ||
app = cli.NewApp() | ||
app.Action = action | ||
app.Name = "Prover Stats API" | ||
app.Usage = "The Scroll L2 ZK Prover Stats API" | ||
app.Version = version.Version | ||
app.Flags = append(app.Flags, utils.CommonFlags...) | ||
app.Flags = append(app.Flags, apiFlags...) | ||
app.Before = func(ctx *cli.Context) error { | ||
return utils.LogSetup(ctx) | ||
} | ||
} | ||
|
||
func action(ctx *cli.Context) error { | ||
// Load config file. | ||
cfgFile := ctx.String(utils.ConfigFileFlag.Name) | ||
cfg, err := config.NewConfig(cfgFile) | ||
if err != nil { | ||
log.Crit("failed to load config file", "config file", cfgFile, "error", err) | ||
} | ||
|
||
// init db handler | ||
db, err := database.InitDB(cfg.DBConfig) | ||
if err != nil { | ||
log.Crit("failed to init db connection", "err", err) | ||
} | ||
defer func() { | ||
if err = database.CloseDB(db); err != nil { | ||
log.Error("can not close ormFactory", "error", err) | ||
} | ||
}() | ||
|
||
subCtx, cancel := context.WithCancel(ctx.Context) | ||
defer func() { | ||
cancel() | ||
}() | ||
|
||
// Start metrics server. | ||
metrics.Serve(subCtx, ctx) | ||
|
||
// init Prover Stats API | ||
port := ctx.String(httpPortFlag.Name) | ||
|
||
router := gin.Default() | ||
controller.InitController(db) | ||
route.Route(router, cfg) | ||
|
||
go func() { | ||
if runServerErr := router.Run(fmt.Sprintf(":%s", port)); runServerErr != nil { | ||
log.Crit("run http server failure", "error", runServerErr) | ||
} | ||
}() | ||
|
||
// Catch CTRL-C to ensure a graceful shutdown. | ||
interrupt := make(chan os.Signal, 1) | ||
signal.Notify(interrupt, os.Interrupt) | ||
|
||
// Wait until the interrupt signal is received from an OS signal. | ||
<-interrupt | ||
|
||
return nil | ||
} | ||
|
||
// Run run prover-stats-api. | ||
func Run() { | ||
// RunApp the prover-stats-api. | ||
if err := app.Run(os.Args); err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
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,15 @@ | ||
package app | ||
|
||
import "github.com/urfave/cli/v2" | ||
|
||
var ( | ||
apiFlags = []cli.Flag{ | ||
&httpPortFlag, | ||
} | ||
// httpPortFlag set http.port. | ||
httpPortFlag = cli.IntFlag{ | ||
Name: "http.port", | ||
Usage: "HTTP server listening port", | ||
Value: 8990, | ||
} | ||
) |
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,24 @@ | ||
package main | ||
|
||
import ( | ||
"scroll-tech/prover-stats-api/cmd/app" | ||
_ "scroll-tech/prover-stats-api/docs" | ||
) | ||
|
||
// @title Scroll Prover Stats API | ||
// @version 1.0 | ||
// @description This is an API server for Provers. | ||
|
||
// @contact.name Prover Stats API Support | ||
// @contact.email Be Pending | ||
|
||
// @license.name Apache 2.0 | ||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
// @host localhost:8990 | ||
// @BasePath /api/v1 | ||
|
||
// @securityDefinitions.basic BasicAuth | ||
func main() { | ||
app.Run() | ||
} |
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,12 @@ | ||
{ | ||
"db_config": { | ||
"driver_name": "postgres", | ||
"dsn": "postgres://localhost/scroll?sslmode=disable", | ||
"maxOpenNum": 200, | ||
"maxIdleNum": 20 | ||
}, | ||
"auth": { | ||
"secret": "prover secret key", | ||
"token_expire_duration": 3600 | ||
} | ||
} |
Oops, something went wrong.