Code Monkey home page Code Monkey logo

ngxcmod.nim's Introduction

ngxcmod

This module contains high level API definations to build module for Nginx with nginx-link-function

For low level API, please import ngxcmod/raw

Requirements

Usage

Nim module

import ngxcmod, strutils

proc init(ctx: ContextCycle) {.init.} =
  ctx.cyc_log(INFO, "hello from Nim")

proc exit(ctx: ContextCycle) {.exit.} =
  ctx.cyc_log(WARN, "goodbye, from Nim w/ <3")

proc hello(ctx: Context) {.exportc.} =
  ctx.log(INFO, "Calling back and log from hello")
  let
    name = ctx.getQueryParam("name")
    message = "hello $#, greeting from Nim" % name
  ctx.response(200, "200 OK", CONTENT_TYPE_PLAINTEXT, message)

proc post(ctx: Context) {.exportc.} =
  ctx.response(200, "200 OK", CONTENT_TYPE_PLAINTEXT, ctx.getBodyAsStr())

Nginx config

http {
...
    server {
...
        ngx_link_func_lib "./your-module.so";
        location = /hello {
            ngx_link_func_call "hello";
        }
...
}

ngxcmod.nim's People

Contributors

ba0f3 avatar baysao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

ryefccd baysao

ngxcmod.nim's Issues

ngxcmod high level API unable to find ngx functions

Hello,

When compiling example code, compiler cannot resolve references to the exported definitions

/usr/bin/ld: /home/rhenvar/.cache/nim/test_d/@mtest.nim.c.o: in function `ngx_link_func_init_cycle':
@mtest.nim.c:(.text+0xad): undefined reference to `ngx_link_func_cyc_log_info'
/usr/bin/ld: /home/rhenvar/.cache/nim/test_d/@mtest.nim.c.o: in function `ngx_link_func_exit_cycle':
@mtest.nim.c:(.text+0x1ad): undefined reference to `ngx_link_func_cyc_log_warn'
/usr/bin/ld: /home/rhenvar/.cache/nim/test_d/@mtest.nim.c.o: in function `hello':
@mtest.nim.c:(.text+0x2b8): undefined reference to `ngx_link_func_log_info'
/usr/bin/ld: @mtest.nim.c:(.text+0x2d6): undefined reference to `ngx_link_func_get_query_param'
/usr/bin/ld: @mtest.nim.c:(.text+0x343): undefined reference to `ngx_link_func_write_resp'
/usr/bin/ld: /home/rhenvar/.cache/nim/test_d/@mtest.nim.c.o: in function `post':
@mtest.nim.c:(.text+0x495): undefined reference to `ngx_link_func_write_resp'
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/rhenvar/ParadigmInteractive/dataAPIServer/src/pkg/test  /home/rhenvar/.cache/nim/test_d/stdlib_system.nim.c.o /home/rhenvar/.cache/nim/test_d/stdlib_strutils.nim.c.o /home/rhenvar/.cache/nim/test_d/@mtest.nim.c.o  -lm   -ldl -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

shell returned 1

Above results were generated from following file:

import ngxcmod, strutils

proc init(ctx: ContextCycle) {.init.} =
  ctx.cyc_log(INFO, "hello from Nim")

proc exit(ctx: ContextCycle) {.exit.} =
  ctx.cyc_log(WARN, "goodbye, from Nim w/ <3")

proc hello(ctx: Context) {.exportc.} =
  ctx.log(INFO, "Calling back and log from hello")
  let
    name = ctx.getQueryParam("name")
    message = "hello $#, greeting from Nim" % name
  ctx.response(200, "200 OK", CONTENT_TYPE_PLAINTEXT, message)

proc post(ctx: Context) {.exportc.} =
  ctx.response(200, "200 OK", CONTENT_TYPE_PLAINTEXT, ctx.getBodyAsStr())

Nim compiler info:

[rhenvar@manjaro pkg]$ nim --version
Nim Compiler Version 1.0.4 [Linux: amd64]
Compiled at 2019-11-27
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release -d:nativeStackTrace

SIGSEGV with Example Code in README

Hello,

I have just discovered this and the possibility of building apps in Nim and deploying them as Nginx modules is so exciting. Thank you so much for the great work!

I have successfully compiled the module using the example code, but when I start nginx, it keeps crashing with the logs below:

019/10/29 21:26:13 [notice] 8#8:  enabled aio threads for link-function module  in /etc/nginx/nginx.conf :124
2019/10/29 21:26:13 [info] 8#8: Init Default Share memory with 1M in /etc/nginx/nginx.conf:124
2019/10/29 21:26:13 [notice] 8#8: using the "epoll" event method
2019/10/29 21:26:13 [notice] 8#8: start worker processes
2019/10/29 21:26:13 [notice] 8#8: start worker process 45
2019/10/29 21:26:13 [notice] 45#45:  enabled aio threads for link-function module
2019/10/29 21:26:13 [info] 45#45: Application /opt/app/hello.so loaded successfully
2019/10/29 21:26:13 [info] 45#45: application initializing
Traceback (most recent call last)
/home/user/.nimble/pkgs/ngxcmod-0.1.2/ngxcmod.nim(41) onInit
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
2019/10/29 21:26:13 [notice] 8#8: signal 17 (SIGCHLD) received from 45
2019/10/29 21:26:13 [notice] 8#8: worker process 45 exited with code 1
2019/10/29 21:26:13 [notice] 8#8: start worker process 51
2019/10/29 21:26:13 [notice] 8#8: signal 29 (SIGIO) received
2019/10/29 21:26:13 [notice] 9#9: gracefully shutting down
2019/10/29 21:26:13 [notice] 9#9: exiting
2019/10/29 21:26:13 [notice] 9#9: exit

To prevent the crash, I needed to remove ctx.log calls from both onInit and onExit functions. After commenting out those two lines, it works.

Is it possible that ctx is not initialized or somehow not ready when these two functions are called?

Thank you for the library again!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.