From 910e69ac1b504ba0b9616d84280690c719cfaa3c Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Wed, 28 Aug 2019 16:10:01 -0400 Subject: endpoint for version response --- assets/tmpl/index.html | 49 ++++++++++++++++++++++++++++++------------------- svc/handlers.go | 12 ++++++++++++ svc/svc.go | 2 +- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/assets/tmpl/index.html b/assets/tmpl/index.html index 4eaca5c..9006bec 100644 --- a/assets/tmpl/index.html +++ b/assets/tmpl/index.html @@ -1,5 +1,6 @@ + @@ -7,6 +8,7 @@ {{.Name}} - twtxt Registry +

API base URL:

-
/api
+
/api

Formats available:

-
/api/plain
+
/api/plain

Endpoints:

-
/api/plain/users
+      
/api/plain/users
 /api/plain/mentions
 /api/plain/tweets
-/api/plain/tags
-

Add new user by submitting a POST request to the /api/plain/users endpoint. - If both ?url=X and ?nickname=X are not passed, or the user already exists in +/api/plain/tags +/api/plain/version

+

Retrieve the version of the instance by issuing a GET request to the + /api/plain/version + endpoint.

+
$ curl '{{.URL}}/api/plain/version'
+getwtxt {{.Vers}}
+        
+

Add new user by submitting a POST request to the /api/plain/users endpoint. + If both ?url=X and ?nickname=X are not passed, or the user already exists in this registry, you will receive 400 Bad Request as a response. If you are unsure what went wrong, the error message should provide enough information for you to correct the request.

-
$ curl -X POST '{{.URL}}/api/plain/users?url=https://foo.ext/twtxt.txt&nickname=foobar'
+      
$ curl -X POST '{{.URL}}/api/plain/users?url=https://foo.ext/twtxt.txt&nickname=foobar'
 200 OK

Query responses are in descending chronological order. This means the newest user or tweet will be in the - first row of the response, with older users or tweets in subsequent rows. Additionally, All queries accept ?page=N - as a parameter, returning groups of 20 results. This may be omitted for the first page of results.

+ first row of the response, with older users or tweets in subsequent rows. Additionally, All queries accept + ?page=N + as a parameter, returning groups of 20 results. This may be omitted for the first page of results.

Get all users:

-
$ curl '{{.URL}}/api/plain/users'
+      
$ curl '{{.URL}}/api/plain/users'
 foo               https://example.com/twtxt.txt     2019-05-09T08:42:23.000Z
 foobar            https://example2.com/twtxt.txt    2019-04-14T19:23:00.000Z
 foo_barrington    https://example3.com/twtxt.txt    2019-03-01T15:59:39.000Z

Query for user by URL:

-
$ curl '{{.URL}}/api/plain/users?url=https://example3.com/twtxt.txt'
+      
$ curl '{{.URL}}/api/plain/users?url=https://example3.com/twtxt.txt'
 foo_barrington    https://example3.com/twtxt.txt    2019-05-01T15:59:39.000Z

Query for user by keyword:

-
$ curl '{{.URL}}/api/plain/users?q=bar'
+      
$ curl '{{.URL}}/api/plain/users?q=bar'
 foobar            https://example2.com/twtxt.txt    2019-05-14T19:23:00.000Z
 foo_barrington    https://example3.com/twtxt.txt    2019-04-01T15:59:39.000Z

Get all tweets:

-
$ curl '{{.URL}}/api/plain/tweets'
+      
$ curl '{{.URL}}/api/plain/tweets'
 foobar    https://example2.com/twtxt.txt    2019-05-13T12:46:20.000Z    It's been a busy day at work!
 ...

Query tweets by keyword:

-
$ curl '{{.URL}}/api/plain/tweets?q=getwtxt'
+      
$ curl '{{.URL}}/api/plain/tweets?q=getwtxt'
 foo_barrington    https://example3.com/twtxt.txt    2019-04-30T06:00:09.000Z    I just installed getwtxt

Get all tweets with tags:

-
$ curl '{{.URL}}/api/plain/tags'
+      
$ curl '{{.URL}}/api/plain/tags'
 foo    https://example.com/twtxt.txt    2019-03-01T09:33:12.000Z    No, seriously, I need #help
 foo    https://example.com/twtxt.txt    2019-03-01T09:32:05.000Z    Seriously, I love #programming!
 foo    https://example.com/twtxt.txt    2019-03-01T09:31:02.000Z    I love #programming!

Query tweets by tag:

-
$ curl '{{.URL}}/api/plain/tags/programming'
+      
$ curl '{{.URL}}/api/plain/tags/programming'
 foo    https://example.com/twtxt.txt    2019-03-01T09:31:02.000Z    I love #programming!

Get all tweets with mentions:

-
$ curl '{{.URL}}/api/plain/mentions'
+      
$ curl '{{.URL}}/api/plain/mentions'
 foo               https://example.com/twtxt.txt     2019-02-28T11:06:44.000Z    @<foo_barrington https://example3.com/twtxt.txt> Hey!! Are you still working on that project?
 bar               https://mxmmplm.com/twtxt.txt     2019-02-27T11:06:44.000Z    @<foobar https://example2.com/twtxt.txt> How's your day going, bud?
 foo_barrington    https://example3.com/twtxt.txt    2019-02-26T11:06:44.000Z    @<foo https://example.com/twtxt.txt> Did you eat my lunch?

Query tweets by mention URL:

-
$ curl '{{.URL}}/api/plain/mentions?url=https://foobarrington.co.uk/twtxt.txt'
+      
$ curl '{{.URL}}/api/plain/mentions?url=https://foobarrington.co.uk/twtxt.txt'
 foo    https://example.com/twtxt.txt    2019-02-26T11:06:44.000Z    @<foo_barrington https://example3.com/twtxt.txt> Hey!! Are you still working on that project?
- + + \ No newline at end of file diff --git a/svc/handlers.go b/svc/handlers.go index 5a46521..bd9ec62 100644 --- a/svc/handlers.go +++ b/svc/handlers.go @@ -141,6 +141,18 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { out, err = twtxtCache.QueryAllStatuses() out = registry.ReduceToPage(page, out) + case "/api/plain/version": + etag := fmt.Sprintf("%x", sha256.Sum256([]byte(Vers))) + w.Header().Set("ETag", "\""+etag+"\"") + w.Header().Set("Content-Type", txtutf8) + _, err := w.Write([]byte(strings.TrimSpace("getwtxt " + Vers))) + if err != nil { + errHTTP(w, r, err, http.StatusInternalServerError) + return + } + log200(r) + return + default: errHTTP(w, r, fmt.Errorf("endpoint not found"), http.StatusNotFound) return diff --git a/svc/svc.go b/svc/svc.go index b20ac7a..8b5c113 100644 --- a/svc/svc.go +++ b/svc/svc.go @@ -123,7 +123,7 @@ func setEndpointRouting(api *mux.Router) { // Specifying the endpoint with and without query information. // Will return 404 on empty queries otherwise. - api.Path("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets)}"). + api.Path("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets|version)}"). Methods("GET", "HEAD"). HandlerFunc(apiEndpointHandler) api.Path("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets)}"). -- cgit 1.4.1-2-gfad0