Deploy

There are several options for sanic server deployment:

Every method has own benefits and disadvantages.

Running sanic servers behind nginx makes several advantages:

But this way requires more complex configuration.

Template configuration files for nginx are presented below. For more information, you can visit sanic.readthedocs.io.

Configuration file for nginx

upstream aiohttp {
    server 127.0.0.1:5021 fail_timeout=0;
    server 127.0.0.1:5022 fail_timeout=0;
}

server {
    listen 5020;
    client_max_body_size 4G;

    server_name 127.0.0.1;

    location / {
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect off;
      proxy_buffering off;
      proxy_pass http://aiohttp;
    }
}

Configuration file for nginx with static files sharing

upstream aiohttp {
    server 127.0.0.1:5021 fail_timeout=0;
    server 127.0.0.1:5022 fail_timeout=0;
}

server {
    listen 5020;
    client_max_body_size 1G;

    server_name 127.0.0.1;

    location / {
      error_page 451 = @imageStore;
      error_page 452 = @staticImage;
      error_page 453 = @staticObject;

      if ($request_method = GET)
      {
        set $REWRITE_CONDITION GET_REQ;
      }

      if ($uri ~ ./buckets/(.*)/images/(....)(.*)$)
      {
        set $REWRITE_CONDITION "${REWRITE_CONDITION}_IMAGE";
        set $BUCKET $1;
        set $PATH $2;
        set $UUID $2$3;
        set $RT /var/lib/luna/current/luna-image-store/luna_image_store/local_storage;
      }
      if ($uri ~ ./buckets/(.*)/objects/(....)(.*)$)
      {
        set $REWRITE_CONDITION "${REWRITE_CONDITION}_OBJECT";
        set $BUCKET $1;
        set $PATH $2;
        set $UUID $2$3;
        set $RT /var/lib/luna/current/luna-image-store/luna_image_store/local_storage;
      }
      if ($REWRITE_CONDITION = GET_REQ_IMAGE)
      {
        return 452;
      }
      if ($REWRITE_CONDITION = GET_REQ_OBJECT)
      {
        return 453;
      }
      return 451;
    }


    location @imageStore
    {
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect off;
      proxy_buffering off;
      proxy_pass http://aiohttp;
    }

    location @staticImage
    {
      error_page 404 = @imageNotFound;
      root $RT;
      try_files /$BUCKET/$PATH/$UUID.jpg @imageNotFound;
    }

    location @staticObject
    {
      error_page 404 = @objectNotFound;
      root $RT;
      try_files /$BUCKET/$PATH/$UUID @objectNotFound;
    }

    location @imageNotFound {
      default_type application/json;

      if (!-d /var/lib/luna/current/luna-image-store/luna_image_store/local_storage/$BUCKET)
      {
        return 404 '{"error_code": 13005, "desc": "Object not found", "detail": "Bucket with name \'${BUCKET}\' not found"}';
      }

      return 404 '{"error_code": 13003, "desc": "Object not found", "detail": "Image with id \'${UUID}\' not found"}';
    }

    location @objectNotFound {
      default_type application/json;

      if (!-d /var/lib/luna/current/luna-image-store/luna_image_store/local_storage/$BUCKET)
      {
        return 404 '{"error_code": 13005, "desc": "Object not found", "detail": "Bucket with name \'${BUCKET}\' not found"}';
      }

      return 404 '{"error_code": 13007, "desc": "Object not found", "detail": "Object with id \'${UUID}\' not found"}';
    }

}

Make sure staticImage location at nginx_imagestore_disk.conf meet to the DEFAULT_IMAGE_EXTENSION at config.conf.

Configuration file for systemd

Config should be located in directory /etc/systemd/system/

[Unit]
Description=Luna-Image-Store on port %i
After=network.target

[Service]
Restart=always
RestartSec=5
User=luna
Group=luna
SyslogIdentifier=luna-image-store-%i
WorkingDirectory=/var/lib/luna/current/luna-image-store/luna_image_store
# Run from luna-configurator
ExecStart=/var/lib/luna/current/luna-image-store/venv/bin/python ./run.py --port=%i --workers=1 --config-reload=0 --luna-config=http://127.0.0.1:5070/1 --log_suffix=%i
# Run from config file
# ExecStart=/var/lib/luna/current/luna-image-store/venv/bin/python ./run.py --port=%i --workers=1 --config-reload=0 --log_suffix=%i --config=./configs/config.conf

[Install]
WantedBy=multi-user.target

Start 2 instances on ports 5021, 5022:

sudo systemctl start luna-image-store\@{5021..5022}.service