Skip to content

Commit

Permalink
First Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpakin committed Sep 10, 2018
0 parents commit adb79e8
Show file tree
Hide file tree
Showing 10 changed files with 23,324 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .gitignore
@@ -0,0 +1,59 @@

# Created by https://www.gitignore.io/api/c

### C ###
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf


# End of https://www.gitignore.io/api/c
17 changes: 17 additions & 0 deletions LICENSE
@@ -0,0 +1,17 @@
Copyright (c) 2018 Calvin Rose
Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
Copyright (c) 2013-2018 Cesanta Software Limited
All rights reserved

This software is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation. For the terms of this
license, see <http://www.gnu.org/licenses/>.

You are free to use this software under the terms of the GNU General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

Alternatively, you can license this software under a commercial
license, as set out in <https://www.cesanta.com/license>.
25 changes: 25 additions & 0 deletions Makefile
@@ -0,0 +1,25 @@
CFLAGS:=-std=c99 -Wall -Wextra -O2 -shared -fpic
LDFLAGS:=
SOURCES:=circletc.c mongoose.c
HEADERS:=mongoose.h
OBJECTS:=$(patsubst %.c,%.o,${SOURCES})
TARGET:=circletc.so

all: ${TARGET}

%.o: %.c ${HEADERS}
${CC} ${CFLAGS} -o $@ -c $< ${LDFLAGS}

${TARGET}: ${OBJECTS}
${CC} ${CFLAGS} -o $@ $^ ${LDFLAGS}

clean:
rm ${OBJECTS}
rm ${TARGET}

install: ${TARGET}
cp ${TARGET} ${JANET_PATH}
cp circlet.janet ${JANET_PATH}

.PHONY: all clean install

38 changes: 38 additions & 0 deletions README.md
@@ -0,0 +1,38 @@
# circlet

Circlet is an HTTP and networking library for the [janet](https://github.com/bakpakin/janet) language.
It provides an abstraction out of the box like Clojure's [ring](https://github.com/ring-clojure/ring), which
is a server abstraction that makes it easy to build composable web applications.

Circlet uses [mongoose](https://cesanta.com/) as the underlying HTTP server engine. Mongoose
is a portable, low footprint, event based server library. The flexible build system requirements
of mongoose make it very easy to embed in other C programs and libraries.

# Building

Building requires [janet](https://github.com/bakpakin/janet) to be installed on the system.

On Linux and macos systems, just run `make` to build, and `make install` to install to
the configured janet path (`JANET_PATH`).

# Example

The below example starts a very simple web server on port 8000.

```lisp
(import circlet)
(defn myserver
"A simple HTTP server"
[req]
{:status 200
:headers {"Content-Type" "text/html"}
:body "<!doctype html><html><body><h1>Hello.</h1></body></html>"})
(circlet.server 8000 myserver)
```

# License

Unlike [janet](https://github.com/bakpakin/janet), circlet is licensed under
the GPL license in accordance with mongoose.
29 changes: 29 additions & 0 deletions build_win.bat
@@ -0,0 +1,29 @@
@rem batch script, run in 'Visual Studio Developer Prompt'

@rem

@echo off

cl /nologo /c /O2 /W3 circlet.c
@if errorlevel 1 goto :BUILDFAIL

cl /nologo /c /O2 /W3 mongoose.c
@if errorlevel 1 goto :BUILDFAIL

@rem TODO find the janet lib location on the system
link /nologo /dll ..\..\janet.lib /out:circletc.dll *.obj
if errorlevel 1 goto :BUILDFAIL

@echo .
@echo ======
@echo Build Succeeded.
@echo =====
exit /b 0

:BUILDFAIL
@echo .
@echo =====
@echo BUILD FAILED. See Output For Details.
@echo =====
@echo .
exit /b 1
32 changes: 32 additions & 0 deletions circlet.janet
@@ -0,0 +1,32 @@
# Generates code that can be serialized

(import circletc :as cc)

(defn middleware
"Coerce any type to http middleware"
[x]
(case (type x)
:function x
(fn @[] x)))

(defn router
"Creates a router middleware"
[routes]
(fn [req]
(def r (or
(get routes (get req :uri))
(get routes :default)))
(if r ((middleware r) req) 404)))

(defn server
"Creates a simple http server"
[port handler]
(def mgr (cc.manager))
(def mw (middleware handler))
(defn evloop [conn]
(print "Circlet server listening on port " port "...")
(var req (yield nil))
(while true
(:= req (yield (mw req)))))
(cc.bind-http mgr (string port) evloop)
(while true (cc.poll mgr 2000)))

0 comments on commit adb79e8

Please sign in to comment.