janet-lang / circlet

HTTP server library for janet
Other
81 stars 10 forks source link

How would you use this in a different app? #3

Closed ahungry closed 5 years ago

ahungry commented 5 years ago

Lets say that I want to write an API in Janet/C.

I have janet and jpm. I built circlet and run the test. I now have a build/circlet.so file - would that (and the appropriate janet files) be all I need to pull into my own codebase? Where I could then initialize a server similar to the sample in readme? just an (import circlet)?

bakpakin commented 5 years ago

For circlet, all you should need is the build/circlet.so file. This can be put in your project directory and imported like (import ./path/to/circlet :as circlet), but without the so extension.

If you are working on a derivative project that depends on circlet, you can specify circlet as a dependency and run jpm deps to install it and all other dependencies. This is all pretty new so you would have to be in the latest master for Janet (might work in 1.1.0). To add a dependency, specify it in your own project.janet file like so.

(declare-project
  :name "my-project"
   ...
  :dependencies ["https://github.com/janet-lang/circlet.git"])

Otherwise, you can clone circlet and run jpm install to put circlet.so in a globally accessible place (using sudo as needed - a default install will probably need it).

If you want to install dependencies to a directory that is not global, but say a project tree (like node_modules), you can add the flag --modpath=./janet_modules to your jpm command (install or deps), and then set the env variable JANET_PATH (or the -m flag) to ./janet_modules when you run your application. Something like:

mkdir janet_modules
jpm --modpath=./janet_modules install https://github.com/janet-lang/circlet.git # will pull circlet from git and install it into the janet_modules tree.
janet -m ./janet_modules myserver.janet

All of the janet_module stuff is completely optional if you don’t mind a single shared dependency directory like go and GOPATH. (By default, jpm will install things to Janet’s default value of (dyn :syspath)). jpm works like this because I was frustrated with tools like npm that love downloading multiple copies of everything.

I am working on code for jpm to automatically build zero dependency executables for projects. This involves building circlet.a as well as circlet.so, as well as a generated C file with a main function and serialized bytecode and linking them together. This is not yet public but would be the way to distribute an application so you don’t have to drag around .so files, Janet source code, and a Janet interpreter.

I plan to document this better when I get standalone executables working and released.