Fast, lightweight, full-featured blog API service

The installation guide

If you have no experience compiling and deploying Elixir or Erlang/OTP programs, it is recommended to use Docker for a fool-proof setup process without contaminating the file system (redundant Erlang/Elixir runtimes and caches).

However, you need to clone the code locally, go to the project home directory, and cut to the latest release before doing the following:

Git clone https://github.com/anyex-project/anyex.git CD git checkout v0.5.2Copy the code

Based on the Docker

  1. Add the configuration

    touch apps/storage/config/prod.secret.exs
    touch apps/web_server/config/prod.secret.exsCopy the code
  2. Generating documentation

    docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli \
    generate -i /local/apps/web_server/priv/static/doc.yaml -g html2 -o /local/apps/web_server/priv/static/docCopy the code
  3. Packaging application

    Docker run-ti --rm --env MIX_ENV=prod -v $PWD:/code bluerain/elixir:1.8.1-slim \ mix do clean, deps.get, releaseCopy the code
  4. Build the mirror

    docker build . -t bluerain/anyexCopy the code
  5. Start the application

    docker-compose -f prod.docker-compose.yml up -dCopy the code

    Application is already started here, use curl http://localhost:8080/ping command test in terminal output pong said run successfully.

    But it’s not over yet. Although both the database and the application container have been started, the parameters are configured (defined in prod.docker-comemage.yml). However, table data generation has not been performed, and any API that requires manipulation of the table will return an error if accessed.

  6. Data migration

    docker exec -ti anyex_server_1 anyex migrateCopy the code

    At this point, the entire AnyEx deployment is complete. Thanks to Docker Compose’s orchestration of the container, the app always runs automatically when the system restarts and restarts automatically when the process crashes.

    Note: the container anyex_server_1 in this step does not necessarily exist. The name of the container may vary depending on the docker-compose version.

Manually compile

  1. Install the Elixir

    Different operating systems (or Linux versions) cannot be installed in the same way, so installation instructions are not provided here. A more general approach is to use ASDF or follow the official installation guide.

  2. Add the configuration

    touch apps/storage/config/prod.secret.exs
    touch apps/web_server/config/prod.secret.exsCopy the code

    If you want to compile configuration data into a binary application, you need to do the following editing (if you don’t need to go straight to the next step) :

    Editing apps/storage/config/prod. Secret. Exs file:

    use Mix.Config
    
    config :storage, Storage.Repo,
      database: "anyex_prod",
      username: "postgres",
      hostname: "localhost",
      password: "sampledb123"Copy the code

    Editing apps/web_server/config/prod. Secret. Exs file:

    use Mix.Config
    
    config :web_server,
      port: 8080,
      username: "admin",
      password: "admin123",
      secret: "7EvrcO4jDM",
      default_limit: 15,
      max_limit: 50,
      markdown_enables: [:article, :tweet],
      cors_origins: ["*"]Copy the code

    The above are database configuration and Web service configuration respectively, and the configuration you edit here will be compiled permanently into the binary application (if packaged for distribution).

  3. Data migration

    If you do not need to deploy the application using Docker, it usually means that the database service is ready. If the database is not already created, the application can create it:

    MIX_ENV=prod mix db.createCopy the code

    Data migration:

    MIX_ENV=prod mix db.migrateCopy the code

    If you did not edit the.exs configuration file, you need to use environment variables to pass some database connection information to make a proper migration (the same goes for creating a database), for example:

    ANYEX_DB_NAME=anyex_prod \
    ANYEX_DB_USERNAME=postgres \
    ANYEX_DB_PASSWORD=sampledb123 \
    ANYEX_DB_HOSTNAME=localhost \
    MIX_ENV=prod db.migrateCopy the code
  4. Generating documentation

    The document is generated regardless of how it is deployed. If you have access to Docker, use the same command as above. Otherwise, you need to install openapi-generator-CLI by yourself and run the following command

    openapi-generator-cli generate -i apps/web_server/priv/static/doc.yaml -g html2 -o apps/web_server/priv/static/docCopy the code
  5. Packaging application

    MIX_ENV=prod mix do clean, deps.get, releaseCopy the code
  6. Run the application

    After the packaging in step 6, the runtime is embedded in the build directory and can run anywhere by copying the build results, independent of ERTS (the Erlang runtime).

    cp _build/prod/rel/anyex /usr/local/anyexCopy the code

    If you did not edit the.exs configuration file, you need to specify environment variables to start the application. The complete configuration variables are as follows:

    ANYEX_DB_NAME=anyex_prod \ ANYEX_DB_USERNAME=postgres \ ANYEX_DB_PASSWORD=sampledb123 \ ANYEX_DB_HOSTNAME=localhost \ ANYEX_SERVER_PORT=8080 \ ANYEX_SERVER_USERNAME=admin \ ANYEX_SERVER_PASSWORD=admin123 \ ANYEX_SERVER_SECRET=7EvrcO4jDM \  ANYEX_SERVER_MARKDOWN_ENABLES=article,tweet \ ANYEX_SERVER_DEFAULT_LIMIT=25 \ ANYEX_SERVER_MAX_LIMIT=25 \ ANYEX_SERVER_CORS_ORIGINS="*" \ /usr/local/anyex/bin/anyex foregroundCopy the code

Configuration instructions

The configuration of running with Docker containers and running locally is essentially the same. The above docker-based build can migrate data without “telling” the application connection information because all configuration variables are defined in prod.docker-comemage. yml, whereas manual compilation requires active definition of configuration variables if.exs does not provide configuration information.

When the application is running, the environment variable defined and the corresponding configuration in. Exs have a higher priority. For example, if port: 80 is defined in. Exs but ANYEX_SERVER_PORT=8080 exists at the same time, port will eventually be set to 8080.

Complete configuration variable description:

  • ANYEX_DB_NAME: Database name
  • ANYEX_DB_USERNAME: Database user
  • ANYEX_DB_PASSWORD: Database password
  • ANYEX_DB_HOSTNAME: Database host name
  • ANYEX_SERVER_PORT: Web service port
  • ANYEX_SERVER_USERNAME: Administrator user name (user name for applying for the Token)
  • ANYEX_SERVER_PASSWORD: Administrator password (password for applying for a Token)
  • ANYEX_SERVER_SECRET: Token ciphertext (used to encrypt and decrypt tokens)
  • ANYEX_SERVER_MARKDOWN_ENABLES: Enables the list of resources supported by Markdown
  • ANYEX_SERVER_DEFAULT_LIMIT: Default paging limit (when no limit parameter is provided)
  • ANYEX_SERVER_MAX_LIMIT: Maximum paging limit (limit beyond this value is reset to this value)
  • ANYEX_SERVER_CORS_ORIGINS: Allows cross-domain origin lists (all asterisks are allowed to be quoted: “*”)

Additional instructions

  1. Why is there a step called “data migration” when it is simply “create data”?

    Because migrate will only create all table data if the database is empty, in a database environment where migrate has been implemented, only incremental changes will be performed and the original data will be retained. Smooth transition to compatible state even in the face of new versions of incompatible table structure. After the upgrade, you will need to perform the MIGRATE task again to upgrade the database table data.

    You can perform the Migrate task using binary published Anyex or in the project root directory using Mix.