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

Shorten handler names #48

Merged
merged 1 commit into from
Aug 6, 2024
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
2 changes: 1 addition & 1 deletion cmd/scrape-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
api.WithURLFetcher(sbf),
api.WithHeadlessIf(headlessFetcher),
api.WithAuthorizationIf(*signingKey.Get()),
api.WithSettingsStorage(dbh),
api.WithSettingsFrom(dbh),
)

if ss.AuthEnabled() {
Expand Down
19 changes: 12 additions & 7 deletions internal/server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"github.com/efixler/webutil/jsonarray"
)

const (
TokenCookieName = "token"
)

func WithURLFetcher(f fetch.URLFetcher) option {
return func(s *Server) error {
if f == nil {
Expand Down Expand Up @@ -51,7 +55,7 @@ func WithFeedFetcher(ff fetch.FeedFetcher) option {
}
}

func WithSettingsStorage(db *database.DBHandle) option {
func WithSettingsFrom(db *database.DBHandle) option {
return func(s *Server) error {
if db == nil {
return errors.New("nil database handle provided")
Expand All @@ -61,6 +65,7 @@ func WithSettingsStorage(db *database.DBHandle) option {
}
}

// Sets the HMAC key and enables JWT authorization if the key is non-empty.
func WithAuthorizationIf(key auth.HMACBase64Key) option {
return func(s *Server) error {
if len(key) > 0 {
Expand Down Expand Up @@ -123,21 +128,21 @@ func (ss Server) AuthEnabled() bool {
func (ss Server) withAuthIfEnabled(ms ...middleware.Step) []middleware.Step {
if len(ss.signingKey) > 0 {
ms = append([]middleware.Step{
auth.JWTAuthzMiddleware(ss.signingKey, auth.WithCookie("token"))},
auth.JWTAuthzMiddleware(ss.signingKey, auth.WithCookie(TokenCookieName))},
ms...,
)
}
return ms
}

func (ss *Server) ExtractHandler() http.HandlerFunc {
func (ss *Server) Extract() http.HandlerFunc {
return middleware.Chain(
ss.extract,
ss.withAuthIfEnabled(middleware.MaxBytes(4096), parseSinglePayload())...,
)
}

func (ss *Server) ExtractHeadlessHandler() http.HandlerFunc {
func (ss *Server) ExtractHeadless() http.HandlerFunc {
ms := ss.withAuthIfEnabled(middleware.MaxBytes(4096), parseSinglePayload())
return middleware.Chain(extractWithFetcher(ss.headlessFetcher), ms...)
}
Expand Down Expand Up @@ -212,7 +217,7 @@ func (h *Server) extract(w http.ResponseWriter, r *http.Request) {
encoder.Encode(page)
}

func (ss *Server) BatchHandler() http.HandlerFunc {
func (ss *Server) Batch() http.HandlerFunc {
ms := ss.withAuthIfEnabled(
middleware.MaxBytes(32768),
middleware.DecodeJSONBody[BatchRequest](payloadKey{}),
Expand Down Expand Up @@ -258,7 +263,7 @@ func (h *Server) batch(w http.ResponseWriter, r *http.Request) {
}
}

func (ss *Server) DeleteHandler() http.HandlerFunc {
func (ss *Server) Delete() http.HandlerFunc {
ms := ss.withAuthIfEnabled(middleware.MaxBytes(4096), parseSinglePayload())
return middleware.Chain(ss.delete, ms...)
}
Expand Down Expand Up @@ -305,7 +310,7 @@ func (h *Server) synchronousBatch(urls []string, encoder *jsonarray.Encoder[*res
}
}

func (ss *Server) FeedHandler() http.HandlerFunc {
func (ss *Server) Feed() http.HandlerFunc {
ms := ss.withAuthIfEnabled(middleware.MaxBytes(4096), parseSinglePayload())
return middleware.Chain(ss.feed, ms...)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/server/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestFeedSourceErrors(t *testing.T) {
)

urlBase := "http://foo.bar" // just make the initial URL valid
handler := scrapeServer.FeedHandler()
handler := scrapeServer.Feed()
for _, test := range tests {
url := urlBase + test.urlPath
request := httptest.NewRequest("GET", url, nil)
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestBatchReponseIsValid(t *testing.T) {
WithURLFetcher(fetcher),
)

batchHandler := ss.BatchHandler()
batchHandler := ss.Batch()
urlPath := "/batch"
var batchPayload BatchRequest
batchPayload.Urls = []string{"/", "/1", "/2"}
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestHeadless503WhenUnavailable(t *testing.T) {
ss := MustAPIServer(context.Background(), WithURLFetcher(&mockUrlFetcher{}))

// ss := &scrapeServer{headlessFetcher: nil}
handler := ss.ExtractHeadlessHandler()
handler := ss.ExtractHeadless()
req := httptest.NewRequest("GET", "http://foo.bar?url=http://example.com", nil)
w := httptest.NewRecorder()
handler(w, req)
Expand All @@ -180,13 +180,13 @@ func TestSingleHandler(t *testing.T) {
{
name: "client",
url: "http://foo.bar",
handler: ss.ExtractHandler(),
handler: ss.Extract(),
expectMethod: resource.DefaultClient,
},
{
name: "headless",
url: "http://example.com",
handler: ss.ExtractHeadlessHandler(),
handler: ss.ExtractHeadless(),
expectMethod: resource.HeadlessChromium,
},
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestDeleteHandler(t *testing.T) {
for _, test := range tests {
req := httptest.NewRequest("DELETE", "http://foo.bar", strings.NewReader(test.body))
w := httptest.NewRecorder()
ss.DeleteHandler()(w, req)
ss.Delete()(w, req)
resp := w.Result()
if resp.StatusCode != test.expectedResult {
t.Errorf("[%s] Expected %d, got %d", test.name, test.expectedResult, resp.StatusCode)
Expand Down
8 changes: 4 additions & 4 deletions internal/server/api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type batchDomainSettingsResponse struct {

type dsKey struct{}

func (ss *Server) GetDomainSettingsHandler() http.HandlerFunc {
func (ss *Server) DomainSettings() http.HandlerFunc {
ms := ss.withAuthIfEnabled(middleware.MaxBytes(4096), extractDomainFromPath(dsKey{}))
return middleware.Chain(ss.getSingleDomainSettings, ms...)
}
Expand All @@ -56,7 +56,7 @@ func (ss *Server) getSingleDomainSettings(w http.ResponseWriter, r *http.Request
middleware.WriteJSONOutput(w, ds, req.PrettyPrint, http.StatusOK)
}

func (ss *Server) PutDomainSettingsHandler() http.HandlerFunc {
func (ss *Server) WriteDomainSettings() http.HandlerFunc {
ms := ss.withAuthIfEnabled(
middleware.MaxBytes(4096),
extractDomainFromPath(dsKey{}),
Expand Down Expand Up @@ -111,7 +111,7 @@ func extractDomainFromPath(key ...any) middleware.Step {
}
}

func (ss *Server) SearchDomainSettingsHandler() http.HandlerFunc {
func (ss *Server) SearchDomainSettings() http.HandlerFunc {
ms := ss.withAuthIfEnabled(
middleware.MaxBytes(4096),
extractBatchDomainSettingsQuery(payloadKey{}),
Expand Down Expand Up @@ -186,7 +186,7 @@ func extractBatchDomainSettingsQuery(pkey any) middleware.Step {
}
}

func (ss *Server) DeleteDomainSettingsHandler() http.HandlerFunc {
func (ss *Server) DeleteDomainSettings() http.HandlerFunc {
ms := ss.withAuthIfEnabled(middleware.MaxBytes(4096), extractDomainFromPath(dsKey{}))
return middleware.Chain(ss.deleteDomainSettings, ms...)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/healthchecks/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func HealthHandler(db database.StatsProvider) http.Handler {
return h
}

func (h health) MarshalJSON() ([]byte, error) {
func (h *health) MarshalJSON() ([]byte, error) {
type alias health
var dbStats interface{}
if h.database != nil {
Expand All @@ -63,7 +63,7 @@ func (h health) MarshalJSON() ([]byte, error) {
*alias
Database interface{} `json:"database,omitempty"`
}{
alias: (*alias)(&h),
alias: (*alias)(h),
Database: dbStats,
})
}
Expand Down
18 changes: 9 additions & 9 deletions internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ func InitMux(
)

// API routes
h := ss.ExtractHandler()
h := ss.Extract()
mux.HandleFunc("GET /extract", h)
mux.HandleFunc("POST /extract", h)
h = ss.ExtractHeadlessHandler()
h = ss.ExtractHeadless()
mux.HandleFunc("GET /extract/headless", h)
mux.HandleFunc("POST /extract/headless", h)
mux.HandleFunc("POST /batch", ss.BatchHandler())
mux.HandleFunc("DELETE /extract", ss.DeleteHandler())
h = ss.FeedHandler()
mux.HandleFunc("POST /batch", ss.Batch())
mux.HandleFunc("DELETE /extract", ss.Delete())
h = ss.Feed()
mux.HandleFunc("GET /feed", h)
mux.HandleFunc("POST /feed", h)
// settings
// Until settings migrations for MySQL are in place
if (db != nil) && db.Engine.Driver() == string(database.SQLite) {
mux.HandleFunc("GET /settings/domain/{DOMAIN}", ss.GetDomainSettingsHandler())
mux.HandleFunc("PUT /settings/domain/{DOMAIN}", ss.PutDomainSettingsHandler())
mux.HandleFunc("GET /settings/domain", ss.SearchDomainSettingsHandler())
mux.HandleFunc("DELETE /settings/domain/{DOMAIN}", ss.DeleteDomainSettingsHandler())
mux.HandleFunc("GET /settings/domain/{DOMAIN}", ss.DomainSettings())
mux.HandleFunc("PUT /settings/domain/{DOMAIN}", ss.WriteDomainSettings())
mux.HandleFunc("GET /settings/domain", ss.SearchDomainSettings())
mux.HandleFunc("DELETE /settings/domain/{DOMAIN}", ss.DeleteDomainSettings())
} else {
mux.HandleFunc("/settings/domain/", serviceUnavailable)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/server/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,37 @@ func TestAPIRoutesAreProtected(t *testing.T) {
{
name: "POST /extract",
method: http.MethodPost,
handler: ss.ExtractHandler,
handler: ss.Extract,
},
{
name: "GET /extract",
method: http.MethodGet,
handler: ss.ExtractHandler,
handler: ss.Extract,
},
{
name: "POST /extract/headless",
method: http.MethodPost,
handler: ss.ExtractHeadlessHandler,
handler: ss.ExtractHeadless,
},
{
name: "POST /extract/batch",
method: http.MethodPost,
handler: ss.BatchHandler,
handler: ss.Batch,
},
{
name: "DELETE /extract",
method: http.MethodDelete,
handler: ss.DeleteHandler,
handler: ss.Delete,
},
{
name: "GET /feed",
method: http.MethodGet,
handler: ss.FeedHandler,
handler: ss.Feed,
},
{
name: "POST /feed",
method: http.MethodPost,
handler: ss.FeedHandler,
handler: ss.Feed,
},
}
for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package version

const (
Commit = "3a0a6ff"
Commit = "7fba048"
Tag = "v0.8.6"
RepoURL = "https://github.com/efixler/scrape"
)