Skip to content

Commit 4f91143

Browse files
committed
api add listen address flag
1 parent 1f1fe50 commit 4f91143

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

cmd/crawler/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
Action: startAPI,
2323
Flags: []cli.Flag{
2424
&apiDBFlag,
25+
&apiListenAddrFlag,
2526
&autovacuumFlag,
2627
&busyTimeoutFlag,
2728
&crawlerDBFlag,
@@ -70,7 +71,8 @@ func startAPI(ctx *cli.Context) error {
7071
go dropDeamon(&wg, nodeDB, ctx.Duration(dropNodesTimeFlag.Name))
7172

7273
// Start the API deamon
73-
apiDeamon := api.New(nodeDB)
74+
apiAddress := ctx.String(apiListenAddrFlag.Name)
75+
apiDeamon := api.New(apiAddress, nodeDB)
7476
go apiDeamon.HandleRequests(&wg)
7577
wg.Wait()
7678

pkg/api/api.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,24 @@ import (
1414
)
1515

1616
type Api struct {
17-
db *sql.DB
18-
cache *lru.Cache
17+
address string
18+
cache *lru.Cache
19+
db *sql.DB
1920
}
2021

21-
func New(sdb *sql.DB) *Api {
22+
func New(address string, sdb *sql.DB) *Api {
2223
cache, err := lru.New(256)
2324
if err != nil {
2425
return nil
2526
}
26-
api := &Api{db: sdb, cache: cache}
27+
28+
api := &Api{
29+
address: address,
30+
cache: cache,
31+
db: sdb,
32+
}
2733
go api.dropCacheLoop()
34+
2835
return api
2936
}
3037

@@ -47,8 +54,8 @@ func (a *Api) HandleRequests(wg *sync.WaitGroup) {
4754
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello")) })
4855
router.HandleFunc("/v1/dashboard", a.handleDashboard).Queries("filter", "{filter}")
4956
router.HandleFunc("/v1/dashboard", a.handleDashboard)
50-
fmt.Println("Start serving on port 10000")
51-
http.ListenAndServe(":10000", router)
57+
fmt.Println("Starting API on:", a.address)
58+
http.ListenAndServe(a.address, router)
5259
}
5360

5461
type client struct {

0 commit comments

Comments
 (0)