moonnginx

    nginx.conf directive-tree codec for MoonBit

    nginx
    nginx.conf
    crossplane
    config
    parser
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    18 hours ago
    Downloads
    2

    #MoonNginx

    MoonBit codec for nginx.conf: lex, parse to a directive tree, dump, build, and check OSS core-module context/arity.

    Behavioral reference: nginxinc/nginx-go-crossplane (Apache-2.0). Go sources are not copied.

    #Install

    moon add LuoYunxin1/moonnginx

    Until the package is published, depend on this repository as a local module.

    #Parse and dump

    let cfg = unwrap_ngx(parse_conf("worker_processes 4;\n"))
    let text = dump_conf(cfg)

    #Three runnable scenarios

    moon run cmd/main --target wasm-gc

    1. PARSE — upstream + location + proxy_pass
    2. BUILD — listen 8080 and root /var/www
    3. VALIDATE — listen inside location is rejected

    #Verify

    moon check --target wasm-gc --deny-warn moon test --target wasm-gc

    #Boundary

    Does not start nginx, terminate TLS, execute Lua/OpenResty, follow include on disk, or speak HAProxy PROXY protocol.

    #License

    Apache-2.0

    Config

    pub(all) struct Config {
    file : String
    parsed : Array[Directive]
    status : String
    } derive(Eq,
    Debug
    )

    Config::find

    fn Config::find(self : Config, name : String) -> Array[Directive]

    Config::find_all

    fn Config::find_all(self : Config, name : String) -> Array[Directive]

    Directive

    pub(all) struct Directive {
    name : String
    line : Int
    args : Array[String]
    quoted : Array[Bool]
    children : Array[Directive]
    has_block : Bool
    comment : String?
    } derive(Eq,
    Debug
    )

    One nginx statement: simple, block, or comment.

    Directive::arg0

    fn Directive::arg0(self : Directive) -> String?

    Directive::child

    fn Directive::child(self : Directive, name : String) -> Array[Directive]

    Directive::is_block

    fn Directive::is_block(self : Directive) -> Bool

    Directive::is_comment

    fn Directive::is_comment(self : Directive) -> Bool

    Directive::is_include

    fn Directive::is_include(self : Directive) -> Bool

    NgxError

    pub(all) enum NgxError {
    Syntax(String)
    Unexpected(String)
    Unterminated(String)
    Constraint(String)
    NotImplemented(String)
    } derive(Eq,
    Debug
    )

    nginx.conf codec errors.

    NgxError::message

    fn NgxError::message(self : NgxError) -> String

    Tok

    pub(all) struct Tok {
    kind : TokKind
    value : String
    line : Int
    } derive(Eq,
    Debug
    )

    TokKind

    pub(all) enum TokKind {
    Word
    Quoted
    LBrace
    RBrace
    Semi
    Comment
    } derive(Eq,
    Debug
    )

    build_config

    fn build_config(dirs : Array[Directive]) -> Config

    collect_names

    fn collect_names(cfg : Config) -> Array[String]

    config_json

    fn config_json(cfg : Config) -> Json

    config_ok

    fn config_ok(file : String, parsed : Array[Directive]) -> Config

    count_directives

    fn count_directives(cfg : Config) -> Int

    dir_block

    fn dir_block(name : String, args : Array[String], children : Array[Directive], line : Int) -> Directive

    dir_comment

    fn dir_comment(text : String, line : Int) -> Directive

    dir_simple

    fn dir_simple(name : String, args : Array[String], line : Int) -> Directive

    dump_conf

    fn dump_conf(cfg : Config) -> String

    empty_config

    fn empty_config(file : String) -> Config

    first_server_name

    fn first_server_name(server : Directive) -> String?

    has_ssl

    fn has_ssl(server : Directive) -> Bool

    includes_of

    fn includes_of(cfg : Config) -> Array[String]

    known_core_directive

    fn known_core_directive(name : String) -> Bool

    library_name

    fn library_name() -> String

    line_msg

    fn line_msg(line : Int, msg : String) -> String

    listen_addrs

    fn listen_addrs(server : Directive) -> Array[String]

    location_paths

    fn location_paths(server : Directive) -> Array[String]

    locations_of

    fn locations_of(server : Directive) -> Array[Directive]

    ngx_access_log

    fn ngx_access_log(path : String) -> Directive

    ngx_add_header

    fn ngx_add_header(name : String, value : String) -> Directive

    ngx_allow

    fn ngx_allow(spec : String) -> Directive

    ngx_client_max_body_size

    fn ngx_client_max_body_size(value : String) -> Directive

    ngx_deny

    fn ngx_deny(spec : String) -> Directive

    ngx_error_log

    fn ngx_error_log(path : String) -> Directive

    ngx_events

    fn ngx_events(children : Array[Directive]) -> Directive

    ngx_gzip

    fn ngx_gzip(onoff : String) -> Directive

    ngx_http

    fn ngx_http(children : Array[Directive]) -> Directive

    ngx_if

    fn ngx_if(cond : Array[String], children : Array[Directive]) -> Directive

    ngx_include

    fn ngx_include(path : String) -> Directive

    ngx_index

    fn ngx_index(names : Array[String]) -> Directive

    ngx_keepalive_timeout

    fn ngx_keepalive_timeout(value : String) -> Directive

    ngx_listen

    fn ngx_listen(addr : String) -> Directive

    ngx_location

    fn ngx_location(path : String, children : Array[Directive]) -> Directive

    ngx_location_mod

    fn ngx_location_mod(modifier : String, path : String, children : Array[Directive]) -> Directive

    ngx_pid

    fn ngx_pid(path : String) -> Directive

    ngx_proxy_pass

    fn ngx_proxy_pass(url : String) -> Directive

    ngx_return_code

    fn ngx_return_code(code : String) -> Directive

    ngx_return_url

    fn ngx_return_url(code : String, url : String) -> Directive

    ngx_rewrite

    fn ngx_rewrite(regex : String, replacement : String) -> Directive

    ngx_root

    fn ngx_root(path : String) -> Directive

    ngx_sendfile

    fn ngx_sendfile(onoff : String) -> Directive

    ngx_server

    fn ngx_server(children : Array[Directive]) -> Directive

    ngx_server_name

    fn ngx_server_name(name : String) -> Directive

    ngx_set

    fn ngx_set(variable : String, value : String) -> Directive

    ngx_try_files

    fn ngx_try_files(args : Array[String]) -> Directive

    ngx_upstream

    fn ngx_upstream(name : String, children : Array[Directive]) -> Directive

    ngx_upstream_server

    fn ngx_upstream_server(addr : String) -> Directive

    ngx_worker_connections

    fn ngx_worker_connections(n : String) -> Directive

    ngx_worker_processes

    fn ngx_worker_processes(n : String) -> Directive

    parse_and_validate

    fn parse_and_validate(text : String) -> Result[Config, NgxError]

    parse_conf

    fn parse_conf(text : String) -> Result[Config, NgxError]

    parse_conf_named

    fn parse_conf_named(file : String, text : String) -> Result[Config, NgxError]

    parse_dump

    fn parse_dump(text : String) -> Result[String, NgxError]

    payload_json

    fn payload_json(cfg : Config) -> Json

    payload_text

    fn payload_text(cfg : Config) -> String

    proxy_backends

    fn proxy_backends(cfg : Config) -> Array[String]

    rewrite_rules

    fn rewrite_rules(cfg : Config) -> Array[(String, String)]

    roundtrip

    fn roundtrip(text : String) -> Result[String, NgxError]

    servers_of

    fn servers_of(cfg : Config) -> Array[Directive]

    supported_blocks

    fn supported_blocks() -> Array[String]

    syntax_profile

    fn syntax_profile() -> String

    unwrap_ngx

    fn[T] unwrap_ngx(r : Result[T, NgxError]) -> T

    upstream_peers

    fn upstream_peers(cfg : Config, name : String) -> Array[String]

    validate_core

    fn validate_core(cfg : Config) -> Result[Unit, NgxError]