# Generated by gramex init 1.62.0 (don't delete this line)
# on 2020-10-27 by bhanu.k <bhanu.kamapantula@gramener.com>

variables:
  FORMS_URL: 'sqlite:///$GRAMEXDATA/forms/forms.db'
  FORMS_TABLE: forms
  FORMS_ID: id
  FORMS_PATH: $GRAMEXDATA/forms

# Gramex init configurations for app: form_builder
# ----------------------------------------------------------------------------
import:
  apps/forms/ui:
    path: $GRAMEXAPPS/ui/gramex.yaml
    YAMLURL: $YAMLURL/ui/
  apps/forms/admin:
    path: $GRAMEXAPPS/admin2/gramex.yaml
    YAMLURL: $YAMLURL/admin/

# Configurations for app: form_builder
# ----------------------------------------------------------------------------
url:
  forms/home-$*:
    pattern: /$YAMLURL/
    handler: FileHandler
    kwargs:
      path: $YAMLPATH/index.html
      template: true
      headers:
        # Templates may have user-specific content. Cache privately.
        # Keep max-age small (in case we log out & log in as another user.)
        Cache-Control: private, max-age=1
    cache: {expiry: {duration: 1}}

  forms/create-$*:
    pattern: /$YAMLURL/create
    handler: FileHandler
    kwargs:
      path: $YAMLPATH/create.html
      template: true
      headers:
        # Templates may have user-specific content. Cache privately.
        # Keep max-age small (in case we log out & log in as another user.)
        Cache-Control: private, max-age=1
    cache: {expiry: {duration: 1}}

  forms/static-$*:
    pattern: /$YAMLURL/(.*)
    handler: FileHandler
    priority: -100
    kwargs:
      path: $YAMLPATH
      default_filename: index.html    #   default filename
      index: true                     #   display the index
      sass: "*.scss"
      headers:
          Cache-Control: max-age=60   # By default, cache for a minute
          # Standard libraries won't ever change. Cache for 10 years
          'node_modules/**':
            Cache-Control: public, max-age=315360000

  forms/publish-$*:
    pattern: /$YAMLURL/publish
    handler: FormHandler
    kwargs:
      url: $FORMS_URL
      table: $FORMS_TABLE
      id: $FORMS_ID
      modify: form_builder.after_publish(handler, data)
      columns:
        metadata: TEXT
        config: TEXT
        thumbnail: TEXT
        html: TEXT
        user: TEXT
        id:
          type: INTEGER
          primary_key: true
          autoincrement: true
  forms/thumbnail-$*:
    pattern: /$YAMLURL/thumbnail/(.*)
    handler: FileHandler
    kwargs:
      path: $FORMS_PATH/thumbnail/
      index: true
      # filehandler disables access to . directories, allow explicitly
      allow: "*/.config/*"
  forms/form-view-$*:
    pattern: /$YAMLURL/(form|view)/(.*)
    handler: FileHandler
    kwargs:
      path: $YAMLPATH/viewform.html
      template: true
  forms/embed-$*:
    pattern: /$YAMLURL/embed/(\d+)\.(json|html|js)
    handler: FunctionHandler
    kwargs:
      function: form_builder.endpoint(handler)
      headers:
        Access-Control-Allow-Origin: '*'        # Allow CORS from any domain
  forms/analytics-$*:
    pattern: /$YAMLURL/analytics/(.*)
    handler: FormHandler
    kwargs:
      each_form:
        url: 'sqlite:///$GRAMEXDATA/forms/form_{db}.db'    # Pick any database
        table: analytics                                   # Pick any table name to create
        id: id                      # The "id" column is primary key
        columns:
          response: TEXT            # Use any SQL type allowed by DB
          id:
            type: INTEGER           # Define an integer ID column
            primary_key: true       # as a primary key
            autoincrement: true     # that auto-increments
          form_id: INTEGER
        modify: form_builder.modify_columns(handler, data)
      analytics:
        url: 'sqlite:///$GRAMEXDATA/forms/forms.db'    # Pick any database
        table: analytics                                   # Pick any table name to create
        id: id                      # The "id" column is primary key
        columns:
          response: TEXT            # Use any SQL type allowed by DB
          id:
            type: INTEGER           # Define an integer ID column
            primary_key: true       # as a primary key
            autoincrement: true     # that auto-increments

  forms/snippets/all:
    pattern: /$YAMLURL/snippets/
    handler: FileHandler
    kwargs:
      path:
        '': $YAMLPATH/snippets/{type}/{file}.{ext}
      headers:
        Content-Type: text/plain
        Content-Disposition: none
        Cache-Control: "public, max-age=86400"
