Make api take refresh-delay from configapi

pull/94/head
Aloïs Micard 3 years ago
parent 672c758897
commit 5429bb70d8
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -80,10 +80,12 @@ services:
--hub-uri amqp://guest:guest@rabbitmq:5672
--elasticsearch-uri http://elasticsearch:9200
--signing-key K==M5RsU_DQa4_XSbkX?L27s^xWmde25
--config-api-uri http://configapi:8080
restart: always
depends_on:
- rabbitmq
- elasticsearch
- configapi
ports:
- 15005:8080
configapi:

@ -6,7 +6,7 @@ import (
"github.com/creekorful/trandoshan/api"
"github.com/creekorful/trandoshan/internal/api/auth"
"github.com/creekorful/trandoshan/internal/api/database"
"github.com/creekorful/trandoshan/internal/duration"
configapi "github.com/creekorful/trandoshan/internal/configapi/client"
"github.com/creekorful/trandoshan/internal/event"
"github.com/creekorful/trandoshan/internal/process"
"github.com/gorilla/mux"
@ -27,7 +27,7 @@ var (
type State struct {
db database.Database
pub event.Publisher
refreshDelay time.Duration
configClient configapi.Client
}
// Name return the process name
@ -37,7 +37,7 @@ func (state *State) Name() string {
// CommonFlags return process common flags
func (state *State) CommonFlags() []string {
return []string{process.HubURIFlag}
return []string{process.HubURIFlag, process.ConfigAPIURIFlag}
}
// CustomFlags return process custom flags
@ -58,10 +58,6 @@ func (state *State) CustomFlags() []cli.Flag {
Usage: "List of API users. (Format user:password)",
Required: false,
},
&cli.StringFlag{
Name: "refresh-delay",
Usage: "Duration before allowing indexation of existing resource (none = never)",
},
}
}
@ -79,7 +75,11 @@ func (state *State) Initialize(provider process.Provider) error {
}
state.pub = pub
state.refreshDelay = duration.ParseDuration(provider.GetValue("refresh-delay"))
configClient, err := provider.ConfigClient([]string{configapi.RefreshDelayKey})
if err != nil {
return err
}
state.configClient = configClient
return nil
}
@ -163,8 +163,10 @@ func (state *State) addResource(w http.ResponseWriter, r *http.Request) {
// therefore the best thing to do is to make the API check if the resource should **really** be added by checking if
// it isn't present on the database. This may sounds hacky, but it's the best solution i've come up at this time.
endDate := time.Time{}
if state.refreshDelay != -1 {
endDate = time.Now().Add(-state.refreshDelay)
if refreshDelay, err := state.configClient.GetRefreshDelay(); err == nil {
if refreshDelay.Delay != -1 {
endDate = time.Now().Add(-refreshDelay.Delay)
}
}
count, err := state.db.CountResources(&api.ResSearchParams{

@ -7,6 +7,8 @@ import (
"github.com/creekorful/trandoshan/api"
"github.com/creekorful/trandoshan/internal/api/database"
"github.com/creekorful/trandoshan/internal/api/database_mock"
"github.com/creekorful/trandoshan/internal/configapi/client"
"github.com/creekorful/trandoshan/internal/configapi/client_mock"
"github.com/creekorful/trandoshan/internal/event"
"github.com/creekorful/trandoshan/internal/event_mock"
"github.com/golang/mock/gomock"
@ -157,6 +159,7 @@ func TestAddResource(t *testing.T) {
rec := httptest.NewRecorder()
dbMock := database_mock.NewMockDatabase(mockCtrl)
configClientMock := client_mock.NewMockClient(mockCtrl)
dbMock.EXPECT().CountResources(&searchParamsMatcher{target: api.ResSearchParams{
URL: "https://example.onion",
@ -174,7 +177,9 @@ func TestAddResource(t *testing.T) {
Headers: map[string]string{"Content-Type": "application/html", "Server": "Traefik"},
})
s := State{db: dbMock, refreshDelay: 5 * time.Hour}
configClientMock.EXPECT().GetRefreshDelay().Return(client.RefreshDelay{Delay: 5 * time.Hour}, nil)
s := State{db: dbMock, configClient: configClientMock}
s.addResource(rec, req)
if rec.Code != http.StatusOK {
@ -235,6 +240,7 @@ func TestAddResourceDuplicateNotAllowed(t *testing.T) {
rec := httptest.NewRecorder()
dbMock := database_mock.NewMockDatabase(mockCtrl)
configClientMock := client_mock.NewMockClient(mockCtrl)
dbMock.EXPECT().CountResources(&searchParamsMatcher{target: api.ResSearchParams{
URL: "https://example.onion",
@ -242,7 +248,9 @@ func TestAddResourceDuplicateNotAllowed(t *testing.T) {
PageNumber: 1,
}, endDateZero: true}).Return(int64(1), nil)
s := State{db: dbMock, refreshDelay: -1}
configClientMock.EXPECT().GetRefreshDelay().Return(client.RefreshDelay{Delay: -1}, nil)
s := State{db: dbMock, configClient: configClientMock}
s.addResource(rec, req)
if rec.Code != http.StatusOK {
@ -273,6 +281,7 @@ func TestAddResourceTooYoung(t *testing.T) {
rec := httptest.NewRecorder()
dbMock := database_mock.NewMockDatabase(mockCtrl)
configClientMock := client_mock.NewMockClient(mockCtrl)
dbMock.EXPECT().CountResources(&searchParamsMatcher{target: api.ResSearchParams{
URL: "https://example.onion",
@ -281,7 +290,9 @@ func TestAddResourceTooYoung(t *testing.T) {
PageNumber: 1,
}}).Return(int64(1), nil)
s := State{db: dbMock, refreshDelay: -10 * time.Minute}
configClientMock.EXPECT().GetRefreshDelay().Return(client.RefreshDelay{Delay: 10 * time.Minute}, nil)
s := State{db: dbMock, configClient: configClientMock}
s.addResource(rec, req)
if rec.Code != http.StatusOK {

Loading…
Cancel
Save