Compare commits

...

2 Commits

Author SHA1 Message Date
Ryan Rix d8a4b13c11 only local localapi in development environment 2024-03-18 17:18:26 -07:00
Ryan Rix fee6eed87e [localapi] clean up elisp API a bit 2024-03-18 17:18:06 -07:00
5 changed files with 35 additions and 19 deletions

View File

@ -528,6 +528,7 @@ These are the route [[https://docs.djangoproject.com/en/3.2/topics/http/urls/][u
#+begin_src python :tangle arcology/urls.py
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from arcology import views
@ -540,11 +541,14 @@ urlpatterns = [
path("feeds.json", views.feed_list, name="feed-list"),
path("", include("django_prometheus.urls")),
path("", include("sitemap.urls")),
path("api/v1/", include("localapi.urls")),
# ensure these ones are last because they're greedy!
re_path("(?P<key>[0-9a-zA-Z/_\-]+\.xml)", views.feed, name="feed"),
re_path("(?P<key>[0-9a-zA-Z/_\-]+)", views.org_page, name="org-page"),
]
if settings.ARCOLOGY_ENVIRONMENT != "production":
urlpatterns = [
path("api/v1/", include("localapi.urls")),
] + urlpatterns
#+end_src
This is the topmatter for the views described below:

View File

@ -48,7 +48,6 @@ INSTALLED_APPS = [
"generators", # [[id:arroyo/django/generators][The Arroyo Generators]]
"syncthonk", # [[id:20231218T183551.765340][Arcology watchsync Command]]
"sitemap", # [[id:20240226T132507.817450][The Arcology's Site Maps and Discovery Mechanisms]]
"localapi", # [[id:20240313T153901.656967][A Localhost API for the Arcology]]
"django_htmx",
"django_prometheus",
@ -59,6 +58,8 @@ INSTALLED_APPS = [
"django.contrib.messages",
"django.contrib.staticfiles",
]
if ARCOLOGY_ENVIRONMENT != "production":
INSTALLED_APPS = ["localapi"] + INSTALLED_APPS
# The Arcology is Modular:1 ends here
# [[file:../../configuration.org::*Internationalization][Internationalization:1]]

View File

@ -1,6 +1,7 @@
# [[file:../arcology.org::*The Web Server][The Web Server:1]]
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from arcology import views
@ -13,9 +14,12 @@ urlpatterns = [
path("feeds.json", views.feed_list, name="feed-list"),
path("", include("django_prometheus.urls")),
path("", include("sitemap.urls")),
path("api/v1/", include("localapi.urls")),
# ensure these ones are last because they're greedy!
re_path("(?P<key>[0-9a-zA-Z/_\-]+\.xml)", views.feed, name="feed"),
re_path("(?P<key>[0-9a-zA-Z/_\-]+)", views.org_page, name="org-page"),
]
if settings.ARCOLOGY_ENVIRONMENT != "production":
urlpatterns = [
path("api/v1/", include("localapi.urls")),
] + urlpatterns
# The Web Server:1 ends here

View File

@ -286,7 +286,6 @@ basically, each org file in this repository, and maybe one or two of your own, a
| "generators" | [[id:arroyo/django/generators][The Arroyo Generators]] |
| "syncthonk" | [[id:20231218T183551.765340][Arcology watchsync Command]] |
| "sitemap" | [[id:20240226T132507.817450][The Arcology's Site Maps and Discovery Mechanisms]] |
| "localapi" | [[id:20240313T153901.656967][A Localhost API for the Arcology]] |
#+BEGIN_SRC python :tangle arcology/settings/__init__.py :noweb yes
# Application definition
@ -301,6 +300,8 @@ INSTALLED_APPS = [
"django.contrib.messages",
"django.contrib.staticfiles",
]
if ARCOLOGY_ENVIRONMENT != "production":
INSTALLED_APPS = ["localapi"] + INSTALLED_APPS
#+END_SRC
** Internationalization

View File

@ -13,12 +13,14 @@ I think it's worth building a small HTTP JSON API which can serve some of the co
Here's how to use this from Emacs Lisp:
- =(arcology-localapi-call method path)= is an API helper which given an API path will fetch the data and return a deserialized JSON structure
- =(arcology-fetch-localapi-bearer-token)= fetches the bearer token file shared with the =localapi= deployment.
- =(arcology-localapi-call method path)= is an API helper which given an API path will fetch the data with authorization and return a deserialized JSON structure
- these interactive commands will fetch a URL, putting it on your kill ring or clipboard if you call it with =M-x= or equivalent:
- =(arcology-key-to-url page-key &optional heading-id)= will take an =ARCOLOGY_KEY= and return a URL
- =(arcology-file-to-url file-path &optional heading-id)= will do the same with a file path from =(buffer-file-name)= or so.
- =(arcology-url-at-point)= gets the org-id of the heading your cursor is in if it has one, and makes a URL that links directly to that.
- =(arcology-read-url)= pops up a list of all the org-roam headings, and returns a URL to it.
- =(arcology-key-to-url page-key &optional heading-id)= will take an =ARCOLOGY_KEY= and return a URL
- =(arcology-file-to-url file-path &optional heading-id)= will do the same with a file path from =(buffer-file-name)= or so.
- =(arcology-url-at-point)= gets the org-id of the heading your cursor is in if it has one, and makes a URL that links directly to that.
- =(arcology-read-url)= pops up a list of all the org-roam headings, and returns a URL to it.
- =(arcology-api-generator)= calls in to [[id:arroyo/django/generators][The Arroyo Generators]] and returns the string of files they generate
* Server view and Arroyo Emacs lisp scaffolding
@ -31,13 +33,17 @@ The API will be simple, with a bearer token provided by a local state file:
(defun arcology-fetch-localapi-bearer-token ()
(interactive)
(customize-save-variable
'arcology-localapi-bearer-token
(or (getenv "ARCOLOGY_LOCALAPI_BEARER_TOKEN")
(thread-last
". ~/sync/private-files/.arcology-env && echo $ARCOLOGY_LOCALAPI_BEARER_TOKEN"
(shell-command-to-string)
(s-chop-suffix "\n" )))))
(let ((tok (or arcology-localapi-bearer-token
(getenv "ARCOLOGY_LOCALAPI_BEARER_TOKEN")
(thread-last
"~/sync/private-files/.arcology-env"
(format ". %s && echo $ARCOLOGY_LOCALAPI_BEARER_TOKEN")
(shell-command-to-string)
(s-chop-suffix "\n" )))))
(when (or (called-interactively-p)
(not arcology-localapi-bearer-token))
(customize-save-variable 'arcology-localapi-bearer-token tok))
tok))
(defcustom arcology-api-base "http://127.0.0.1:29543/api/v1"
"localapi base url")
@ -45,7 +51,7 @@ The API will be simple, with a bearer token provided by a local state file:
(defun arcology-localapi-call (method path &rest plz-args)
(let ((plz-args (or plz-args '(:as json-read))))
(apply 'plz method (format "%s%s" arcology-api-base path)
:headers `(("Authorization" . ,(format "Bearer %s" arcology-localapi-bearer-token)))
:headers `(("Authorization" . ,(format "Bearer %s" (arcology-fetch-localapi-bearer-token))))
plz-args)))
#+end_src
@ -370,11 +376,11 @@ let
arcology = cfg.packages.arcology;
env = [
"ARCOLOGY_ENVIRONMENT=development"
"ARCOLOGY_BASE_DIR=${cfg.orgDir}"
"ARCOLOGY_DB_PATH=${cfg.dataDir}/db.sqlite3"
"ARCOLOGY_LOG_LEVEL=${cfg.logLevel}"
"ARCOLOGY_CACHE_PATH=${cfg.cacheDir}"
"GUNICORN_CMD_ARGS='--bind=${cfg.address}:${toString cfg.port} -w ${toString cfg.workerCount}'"
];
pyenv = pkgs.python3.withPackages(pp: [arcology]);
@ -388,7 +394,7 @@ let
${arcology}/bin/arcology watchsync -f ${cfg.folderId}
'';
localapiScript = pkgs.writeScriptBin "arcology-localapi" ''
${pyenv}/bin/python -m gunicorn arcology.wsgi
${arcology}/bin/arcology runserver ${toString cfg.address}:${toString cfg.port}
'';
in {
options.services.arcology2 = with lib; {