Git Product home page Git Product logo

suttacentral's Introduction

SuttaCentral server and client repository

SuttaCentral is a Python Flask server which serves a Progressive Web App (client) and its associated JSON API (server).

The API pulls its data in real time from an ArangoDB instance populated periodically with data from the sc-data repository.

Deploying for production

  1. $ git clone [email protected]:suttacentral/suttacentral.git
  2. $ cd suttacentral
  3. $ git checkout production
  4. $ make prepare-host
  5. $ make run-production-env -> Supply needed env variables, if you chose random you will be prompted back with generated values. Remember them! You will use some of them to access admin services.
  6. Done

Or updating in individual steps

  1. cd /opt/suttacentral
  2. git pull
  3. make generate-env-variables -> Supply needed env variables (only if env has been changed)
  4. make run-prod-no-logs -> run docker containers
  5. make delete-database -> OPTIONAL: Skip this if data hasn't seriously changed (do it, if texts have been deleted or renamed)
  6. make migrate -> only needed for delete-database, but harmless to run anyway
  7. make load-data -> load data into arangodb
  8. make index-arangosearch -> index arangosearch
  9. make reload-uwsgi -> make sure flask server is not serving cached stale data

Minimally disruptive update

If no containers need to be rebuilt then this is all that needs to be run:

  1. cd /opt/suttacentral
  2. git pull
  3. make load-data
  4. make reload-uwsgi
  5. make index-arangosearch
  6. make rebuild-frontend

Changing the branch(s) the server, or staging server, uses

  1. cd /opt/suttacentral
  2. git checkout <code-branch>
  3. cd server/sc-data
  4. git checkout <data-branch>

Then run the commands for updating, probably including the make delete-database step.

Development

1. Server

1.0 Setting up the project and first run

  1. Install docker and docker-compose.
  2. Clone the repo git clone [email protected]:suttacentral/suttacentral.git.
  3. Cd into the repo cd suttacentral.
  4. run make prepare-host in order to make some small adjustment on the host machine.
  5. run make run-preview-env - Build images, load data, index-arangosearch and more. This will run the project for the first time.

1.1 Running the project

  1. run make run-dev.

1.2 Loading the data

When changes are made on bilara-data and sc-data, they do not automatically get updated in suttacentral. During initial setup in step 1.0.4 above, the raw data from those repositories is brought into suttacentral and added to the database. So if you make changes in bilara-data and sc-data you must run the steps below to see them in the build.

  1. ensure server is up and run make load-data.
  2. To index arangosearch run make index-arangosearch.

1.3 Docs

API documentation is available at suttacentral.net/api/docs.

Swagger documentation is generated from doc strings in api methods. The docstring should use OpenAPI specification 2.0 yaml format. This yaml docstring will be interpreted as OpenAPI's Operation Object.

Development

In this mode server, nignx, client dirs are mounted in Docker's containers so that any local changes take place in the container as well.

In addition Uwsgi+Flask expose port 5001 on local host, arangodb port 8529.

1.4 Makefile

There is a Makefile with following commands:

  • prepare-host - Setup client git-hooks.
  • run-dev - Run containers in development mode.
  • run-dev-no-logs - Run containers in development mode without output to the console.
  • run-prod - Run containers in production mode.
  • run-prod-no-logs - Run containers in production mode without output to the console.
  • migrate - Run migrations in flask container.
  • clean-all - Remove all containers, volumes and built images
  • reload-nginx - Reloads Nginx.
  • reload-uwsgi - Reloads uWSGI+Flask.
  • prepare-tests - Starts containers in test mode and wait for start-ups to finnish.
  • test - Run tests inside containers.
  • test-client - Run only frontend tests.
  • test-server - Run only server test.
  • load-data - Pulls most recent data from github and loads it from server/sc-data folder to the db.
  • delete-database - Delete database from ArangoDB.
  • index-arangosearch - Index ArangoSearch with data from the db.
  • run-preview-env - Fully rebuild and run most recent development version.
  • run-preview-env-no-search - Fully rebuild and run most recent development version but does not index ArangoSearch.
  • run-production-env - Fully rebuild and run most recent production version. You will be prompted with questions regarding env variables.
  • generate-env-variables - Runs env_variables_setup.py script and generate env variables for production version.

In addition, the following rebuilds the front end.

  • docker-compose build sc-frontend
  • then make run-dev

1.5 Working with ArangoDB

Our project is using ArangoDB on the back-end. In the development mode it exposes port 8529 on the localhost. You can access its web interface on http://127.0.0.1:8529.

In the code that is running in the docker containers you can access the database on the adress sc-arangodb on the same port.

In the development mode:

Login: root

password: test

In order to change password you have to change ARANGO_ROOT_PASSWORD in env's .env fiel eg. If you want to change it in development env you have to edit .dev.env file.

1.6 Nginx proxy

Our project is using nginx as a HTTP reverse proxy. It is responsible for serving static files and passing /api/* endpoints to the uwsgi+flask server.

1.7 Flask + uWSGI

Flask is hidden behind uWSGI. uWsgi communicate with nignx with unix socket. The socket file (uwsgi.sock) is in socket-volume shared beetwen nginx and flask+uwsgi

Creating db migrations

In order to create database migration in out app you have to follow those simple steps:

  1. in server/server/migrations/migrations folder create file with name <migration_name>_<id of the last migration + 1>.py.
  2. Add this line at the top of the file: from ._base import Migration.
  3. Create class that inherits from Migration class.
  4. Set migration_id class attribute to match the file name.
  5. create some tasks. Each task should be separate method accepting only self as a parameter.
  6. Set tasks = ['first_task', 'second_task', ...] in class attributes.
  7. You are good to go just remember to never change the 'migration_id'. otherwise your migrations might fail.

For example:

from common.arangodb import get_db
from migrations.base import Migration


class InitialMigration(Migration):
    migration_id = 'initial_migration_001'
    tasks = ['create_collections']

    def create_collections(self):
        """
        Creates collections of suttas and collection of edges between them.
        """
        db = get_db()
        graph = db.create_graph('suttas_graph')
        suttas = graph.create_vertex_collection('suttas')
        parallels = graph.create_edge_definition(
            name='parallels',
            from_collections=['suttas'],
            to_collections=['suttas']
        )

Flask manage tasks

  1. python manage.py migrate - Run migrations.
  2. python manage.py list_routes - Lists all available routes/URLs.

1.10 Style guidelines

  • Follow PEP8 for Python code.

  • Try to keep line width under 120 characters.

  • Use formatted string literals for string formatting.

  • Use Type Hints whenever possible.

  • In views methods (get, post, etc.) Use YAML OpenAPI 2.0 object format in docstrings.

  • For the rest of docstrings use google style docstring.

  • Code for the API endpoints should be places in api folder, except of the search endpoint.

  • Names, variables, docstring, comments, etc. should be written in english.

  • Test files should be placed in tests dir in directory where tested file is.

2. Client

2.1 Style guidelines

General considerations:

  • Use template strings.

  • Use ES6 fat arrow functions.

  • Use ES6 classes (class MyElement extends Polymer.Element) instead of the old Polymer({...}) syntax when declaring an element inside your <script> tags.

  • Use const/let instead of var when declaring a variable.

  • Use === and !== instead of == and != when comparing values to avoid type coercion.

  • Comments explaining a function's purpose should be written on the line directly above the function declaration.

  • Internal HTML imports should come after external ones (from bower_components) and be separated by a newline.

  • When commenting Components at the top-level (above <dom-module>), keep HTML comment tags (\<!-- & -->) on their own separate lines.

  • Try to keep line width under 120 characters.

suttacentral's People

Contributors

alexander-vo avatar aminah-sc avatar ayya-vimala avatar benmneb avatar blake-sc avatar bosmanfrx avatar buddhist-uni avatar chansikpark avatar codigo-fuentes avatar dependabot[bot] avatar epicfaace avatar firepick1 avatar gradam avatar hejvi avatar ihongda avatar jakubsemikstx avatar janlat avatar nanotwerp avatar prograd avatar riwuko avatar sabbamitta avatar snyk-bot avatar sujato avatar thesunshade avatar thsutton avatar trebuhd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

suttacentral's Issues

Possible UI improvements

Just a list of possible UI issues:

  • The "tall" header on static pages should have a sexier animation and style.
    • Maybe when it contracts, put the title in the toolbar, and keep the slim toolbar and adaptive tab bar visible at all times.
  • Promote i18n icon on Home page, maybe make visible by default in toolbar.
  • In intro, add infographic for how to use.
  • Possibly indicate external links
  • Filter by difficulty
  • On suttaplex, rather than "More" use "change language" or something, more means "more introduction".
  • On text controls, readers don't read stuff, so they click things and want to see it works.
  • Make it clearer that suttas are to be accessed via sidebar

Paper-badge not updating at the right time

Basically, the problem and the hack are described here:

PolymerElements/paper-badge#66

I have not found another way to do it yet then to set a TimeOut on it. The problem is caused by the iron-resize-behaviour firing at the wrong time: it should fire when the entire page is loaded, not just the toolbar. So the badge appears 256 px too far to the right.

You will have to play around with the correct time for the setTimeOut. I have 1000 locally and that works OK.

Bug in iron-query-params.html

There is a bug in iron-location/iron-query-params.html because of which app-location does not accept any query-parameters. I fixed this by substituting this:

       paramsObject: {
          type: Object,
          notify: true,
          value: function() {
            return {};
         }

by this:

       paramsObject: {
          type: Object,
          notify: true
         }

But it can be overwritten again with the next bower-install until the Polymer team have fixed this. So just to be aware that if query parameters are not accepted, this is most likely the case.

Chrome on Ubuntu weird bug

Original ticket Creator:
ayya.vimala

Google Chrome: Version 64.0.3282.186 (Offizieller Build) (64-Bit)
Ubuntu: 16.04 LTS

See details here:
https://discourse.suttacentral.net/t/report-bugs-for-the-new-site-here-bug/8605/25?u=vimala

and here:

https://discourse.suttacentral.net/t/report-bugs-for-the-new-site-here-bug/8605/34?u=vimalaJira ID: SUCE-632
Jira Creation Date: 11/Mar/18 7:31 PM
Jira Epic:
Jira Priority: Highest
Jira Status (at import): Selected For Development

Comments:

22/Mar/18 10:32 AM
hubert.dworczynski
Is anyone here on Ubuntu 16.04 who could install that version of Chrome and try to reproduce this? I can't on OSX nor on Ubuntu 17.10.

App-header-layout forced update

The #contentContainer of app-header layout does not correctly recalculate the padding-top when going back to a page you have visited before. I hope to find a solution but for now I will leave it.

make index-elasticsearch fails

This has worked for me in the past, but running the following commands in the next-sc directory gave me the stack trace below.

git pull
sudo make prepare-host
sudo make build-all
sudo make load-data
sudo make run-dev-no-logs
sudo make load-data
sudo make index-elasticsearch
 26% 11/42 [05:56<08:18, 16.08s/it]INFO:search.indexer:Creating index lzh_c7be29301c because index does not exists
INFO:search.indexer:Creating index named lzh_c7be29301c, alias lzh
Traceback (most recent call last):
  File "manage.py", line 70, in <module>
    manager.run()
  File "/usr/local/lib/python3.6/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/usr/local/lib/python3.6/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/usr/local/lib/python3.6/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "manage.py", line 54, in index_elasticsearch
    update()
  File "/opt/sc/sc-flask/server/search/texts.py", line 211, in update
    indexer.update()
  File "/opt/sc/sc-flask/server/search/indexer.py", line 232, in update
    self.update_data()
  File "/opt/sc/sc-flask/server/search/texts.py", line 193, in update_data
    self.process_chunks(chunks)
  File "/opt/sc/sc-flask/server/search/indexer.py", line 250, in process_chunks
    for chunk in chunks:
  File "/opt/sc/sc-flask/server/search/texts.py", line 115, in yield_docs_from_dir
    for i, text in enumerate(html_texts):
  File "/usr/local/lib/python3.6/site-packages/arango/cursor.py", line 32, in __next__
    return self.next()
  File "/usr/local/lib/python3.6/site-packages/arango/cursor.py", line 124, in next
    raise CursorNextError(res)
arango.exceptions.CursorNextError: [HTTP 404][ERR 1600] cursor not found
Makefile:169: recipe for target 'index-elasticsearch' failed
make: *** [index-elasticsearch] Error 1

App-route does not handle dots in file or pathnames

This is a big problem because many of our files have dots in them. I cannot load the AN or SN files the way things are now. Apparently it will work going through Firebase but that will be difficult for me with development. @blake-sc any ideas?

Filed bug report here:
https://github.com/Polymer/polyserve/issues/147

Stackoverflow question filed here:
https://stackoverflow.com/questions/45531951/polymer-2-0-iron-location-app-route-not-recognising-route

Hash sum mismatch installing tk-dev on Ubuntu Artful

Hey folks,

I'm working through the developer setup on my new Ubuntu-running laptop. I can get all the dependencies except for tk-dev which gives the following error:

Get:1 http://au.archive.ubuntu.com/ubuntu artful/main amd64 x11proto-input-dev all 2.3.2-1 [118 kB]
Err:1 http://au.archive.ubuntu.com/ubuntu artful/main amd64 x11proto-input-dev all 2.3.2-1
  Hash Sum mismatch
  Hashes of expected file:
   - SHA256:3d3fabfdb59a0f9a8f6b09f7392f0958e0dd3e4426f3bb37798230b3540674e6
   - SHA1:27f1dbd5e1cdc4c28f7ea9bb279c79dd430ac0d3 [weak]
   - MD5Sum:93e74ea4ed86071a738a9b3d1616d23e [weak]
   - Filesize:118216 [weak]
  Hashes of received file:
   - SHA256:49ab70006e051c9462cf86801440d7d8099b5e769956a48b72f8a8b9ac98c8d1
   - SHA1:43e61dada4efe81e0e88d84d71c933c62017530a [weak]
   - MD5Sum:c307ec307b508e1b1f6b04704e0dc2af [weak]
   - Filesize:118216 [weak]
  Last modification reported: Fri, 13 May 2016 23:40:04 +0000

That said, the pyenv commands work fine so we may not need that package.

Add PTS 2nd images

Right now text images are PTS 1st edition. It would be better to have PTS 2nd edition so this involves:

  1. scanning and optimizing images
  2. uploading images
  3. changing js in frontend to show images

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.