Configuration file and options for Popcorn apps

pop_config provide a configuration framework for Popcorn apps based on ini files.

By default AppConfig provides app.host and app.port keys, it's all we need to start an app:

import popcorn
import popcorn::pop_config

# Build config from options
var config = new AppConfig
config.parse_options(args)

# Use options
var app = new App
app.listen(config.app_host, config.app_port)

For more advanced uses, AppConfig and AppOptions can be specialized to offer additional config options:

import popcorn
import popcorn::pop_config

class MyConfig
    super AppConfig

    # My secret code I don't want to share in my source repository
    fun secret: String do return opt_secret.value or else ini["secret"] or else "my-secret"

    # opt --secret
    var opt_secret = new OptionString("My secret string", "--secret")

    redef init do
        super
        add_option opt_secret
    end
end

class SecretHandler
    super Handler

    # Config to use to access `secret`
    var config: MyConfig

    redef fun get(req, res) do
        res.send config.secret
    end
end

var config = new MyConfig
config.parse_options(args)

var app = new App
app.use("/secret", new SecretHandler(config))
app.listen(config.app_host, config.app_port)

Introduced classes

class AppConfig

popcorn :: AppConfig

Configuration file for Popcorn apps

All class definitions

class AppConfig

popcorn $ AppConfig

Configuration file for Popcorn apps
package_diagram popcorn::pop_config pop_config config config popcorn::pop_config->config ini ini config->ini opts opts config->opts ...ini ... ...ini->ini ...opts ... ...opts->opts popcorn::pop_repos pop_repos popcorn::pop_repos->popcorn::pop_config github::loader loader github::loader->popcorn::pop_repos popcorn::pop_tracker pop_tracker popcorn::pop_tracker->popcorn::pop_repos github::loader... ... github::loader...->github::loader popcorn::pop_tracker... ... popcorn::pop_tracker...->popcorn::pop_tracker

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module config

config :: config

Configuration options for nit tools and apps

Children

module pop_repos

popcorn :: pop_repos

Repositories for data management.

Descendants

# Configuration file and options for Popcorn apps
#
# `pop_config` provide a configuration framework for Popcorn apps based on ini
# files.
#
# By default `AppConfig` provides `app.host` and `app.port` keys, it's all we
# need to start an app:
#
# ~~~
# import popcorn
# import popcorn::pop_config
#
# # Build config from options
# var config = new AppConfig
# config.parse_options(args)
#
# # Use options
# var app = new App
# app.listen(config.app_host, config.app_port)
# ~~~
#
# For more advanced uses, `AppConfig` and `AppOptions` can be specialized to
# offer additional config options:
#
# ~~~
# import popcorn
# import popcorn::pop_config
#
# class MyConfig
#	super AppConfig
#
#	# My secret code I don't want to share in my source repository
#	fun secret: String do return opt_secret.value or else ini["secret"] or else "my-secret"
#
#	# opt --secret
#	var opt_secret = new OptionString("My secret string", "--secret")
#
#	redef init do
#		super
#		add_option opt_secret
#	end
# end
#
# class SecretHandler
#	super Handler
#
#	# Config to use to access `secret`
#	var config: MyConfig
#
#	redef fun get(req, res) do
#		res.send config.secret
#	end
# end
#
# var config = new MyConfig
# config.parse_options(args)
#
# var app = new App
# app.use("/secret", new SecretHandler(config))
# app.listen(config.app_host, config.app_port)
# ~~~
module pop_config

import config

# Configuration file for Popcorn apps
#
# ~~~
# import popcorn
# import popcorn::pop_config
#
# # Build config from default values
# var config = new AppConfig
# config.parse_options(args)
#
# # Change config values
# config.ini["app.port"] = 3001.to_s
#
# # Use options
# var app = new App
# app.listen(config.app_host, config.app_port)
# ~~~
class AppConfig
	super IniConfig

	redef var default_config_file: String = "app.ini"

	# Host name to bind on (will overwrite the config one).
	var opt_host = new OptionString("Host to bind the server on", "--host")

	# Web app host name
	#
	# * key: `app.host`
	# * default: `localhost`
	fun app_host: String do return opt_host.value or else ini["app.host"] or else "localhost"

	# Port number to bind on (will overwrite the config one).
	var opt_port = new OptionInt("Port number to use", -1, "--port")

	# Web app port
	#
	# * key: `app.port`
	# * default: `3000`
	fun app_port: Int do
		var opt = opt_port.value
		if opt > -1 then return opt
		var val = ini["app.port"]
		if val != null then return val.to_i
		return 3000
	end

	# Displayed host name
	#
	# Specify this option if you need a qualified hostname to use within your handlers
	# and `app_host` requires something else like an IP to bind.
	# Really useful if the popcorn app runs behind a proxy.
	#
	# Default is `"{app_host}:{app_port}"`
	var opt_hostname = new OptionString("Displayed host name", "--hostname")

	# Displayed host name config
	#
	# * key: `app.hostname`
	# * default: `"{app_host}:{app_port}"`
	fun app_hostname: String do
		var opt = opt_hostname.value
		if opt != null then return opt
		var cfg = ini["app.hostname"]
		if cfg != null then return cfg
		var res = app_host
		if app_port != 80 then res = "{res}:{app_port}"
		return res
	end

	init do
		super
		add_option(opt_host, opt_port, opt_hostname)
	end
end
lib/popcorn/pop_config.nit:17,1--155,3