Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http support #281

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
PKG_CONFIG = PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 pkg-config
COMPILER_DIR = src/compiler/
RUNTIME_DIR = src/runtime/
SHARED_DIR = src/shared/
UTILS_DIR = src/utils/
OPT_DIR = src/optionals/
GRAVITY_SRC = src/cli/gravity.c

DOCKER_DIR = docker
DOCKERFILE_DIRS = $(notdir $(wildcard $(DOCKER_DIR)/*))

CC ?= gcc
SRC = $(wildcard $(COMPILER_DIR)*.c) \
$(wildcard $(RUNTIME_DIR)/*.c) \
$(wildcard $(SHARED_DIR)/*.c) \
$(wildcard $(UTILS_DIR)/*.c) \
$(wildcard $(OPT_DIR)/*.c)

OPENSSL_ENABLED := true
OPENSSL_INSTALLED = $(shell $(PKG_CONFIG) --exists openssl && echo true || echo false)
$(info OPENSSL_ENABLED: $(OPENSSL_ENABLED))
$(info OPENSSL_INSTALLED: $(OPENSSL_INSTALLED))
ifeq ($(OPENSSL_ENABLED),true)
ifeq ($(OPENSSL_INSTALLED),false)
$(error OpenSSL is enabled, but not installed)
endif
endif
INCLUDE = -I$(COMPILER_DIR) -I$(RUNTIME_DIR) -I$(SHARED_DIR) -I$(UTILS_DIR) -I$(OPT_DIR)
CFLAGS = $(INCLUDE) -std=gnu99 -fgnu89-inline -fPIC -DBUILD_GRAVITY_API
OBJ = $(SRC:.c=.o)
Expand All @@ -26,6 +39,10 @@ else
# MacOS
LIBTARGET = libgravity.dylib
LDFLAGS = -lm
ifeq ($(OPENSSL_ENABLED),true)
CFLAGS += $(shell $(PKG_CONFIG) --cflags openssl) -DGRAVITY_OPENSSL_ENABLED
LDFLAGS += $(shell $(PKG_CONFIG) --libs openssl)
endif
else ifeq ($(UNAME_S),OpenBSD)
# OpenBSD
CFLAGS += -D_WITH_GETLINE
Expand All @@ -46,6 +63,10 @@ else
# Linux
LIBTARGET = libgravity.so
LDFLAGS = -lm -lrt
ifeq ($(OPENSSL_ENABLED),true)
CFLAGS += $(shell $(PKG_CONFIG) --cflags openssl) -DGRAVITY_OPENSSL_ENABLED
LDFLAGS += $(shell $(PKG_CONFIG) --libs openssl)
endif
endif
endif

Expand All @@ -67,3 +88,18 @@ lib: gravity

clean:
rm -f $(OBJ) gravity libgravity.so gravity.dll

docker.%.base:
docker build --no-cache -t gravity-$*-base -f $(DOCKER_DIR)/$*/base/Dockerfile .

docker.build:
docker-compose build

docker.up: docker.$(DOCKERFILE_DIRS).base docker.build
$(info building gravity on: $(DOCKERFILE_DIRS))
docker-compose up

docker.down:
docker-compose down
docker-compose stop
docker-compose rm
6 changes: 6 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
ubuntu_openssl_enabled:
build: docker/ubuntu/openssl_enabled
ubuntu_openssl_disabled:
build: docker/ubuntu/openssl_disabled
8 changes: 8 additions & 0 deletions docker/ubuntu/base/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ubuntu:latest

RUN apt update
RUN apt install -y make pkg-config

COPY . /app
WORKDIR /app
ENV PATH="/app:${PATH}"
6 changes: 6 additions & 0 deletions docker/ubuntu/openssl_disabled/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM gravity-ubuntu-base

ENV OPENSSL_ENABLED=false

ENTRYPOINT [ "/bin/bash" ]
CMD [ "-c", "./examples/http/run.sh" ]
8 changes: 8 additions & 0 deletions docker/ubuntu/openssl_enabled/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM gravity-ubuntu-base

RUN apt install -y libssl-dev

ENV OPENSSL_ENABLED=true

ENTRYPOINT [ "/bin/bash" ]
CMD [ "-c", "./examples/http/run.sh" ]
40 changes: 40 additions & 0 deletions examples/http/main.gravity
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
func main() {
var get_tests = [
[
"host": "github.com"
],
[
"host": "https://reddit.com"
],
[
"host": "https://api.github.com",
"path": "/users/jamesalbert"
],
[
"host": "https://httpbin.org",
"path": "/get"
]
]

get_tests.loop(func (request) {
var resp = Http.get(request)
System.print("=== " + resp.Hostname)
System.print(resp)
})

var post_tests = [
[
"host": "https://httpbin.org/post",
"path": "/post",
"data": [
"pet": "cat"
]
]
]

post_tests.loop(func (request) {
var resp = Http.post(request)
System.print("=== " + resp.Hostname)
System.print(resp)
})
}
9 changes: 9 additions & 0 deletions examples/http/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

# make gravity
make clean gravity OPENSSL_ENABLED=${OPENSSL_ENABLED}

# run example
rm -f examples/http/main.json
gravity -c examples/http/main.gravity -o examples/http/main.json
gravity -x examples/http/main.json
1 change: 1 addition & 0 deletions gravity_visualstudio/exports.def
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ EXPORTS
gravity_list_from_array
gravity_list_free
gravity_list_append_list
gravity_list_append_value
gravity_list_blacken
gravity_list_size

Expand Down
2 changes: 2 additions & 0 deletions gravity_visualstudio/gravity.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ClInclude Include="..\src\compiler\gravity_token.h" />
<ClInclude Include="..\src\compiler\gravity_visitor.h" />
<ClInclude Include="..\src\optionals\gravity_math.h" />
<ClInclude Include="..\src\optionals\gravity_http.h" />
<ClInclude Include="..\src\optionals\gravity_optionals.h" />
<ClInclude Include="..\src\runtime\gravity_core.h" />
<ClInclude Include="..\src\runtime\gravity_vm.h" />
Expand Down Expand Up @@ -63,6 +64,7 @@
<ClCompile Include="..\src\compiler\gravity_token.c" />
<ClCompile Include="..\src\compiler\gravity_visitor.c" />
<ClCompile Include="..\src\optionals\gravity_math.c" />
<ClCompile Include="..\src\optionals\gravity_http.c" />
<ClCompile Include="..\src\runtime\gravity_core.c" />
<ClCompile Include="..\src\runtime\gravity_vm.c" />
<ClCompile Include="..\src\shared\gravity_hash.c" />
Expand Down
6 changes: 6 additions & 0 deletions gravity_visualstudio/gravity.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<ClInclude Include="..\src\optionals\gravity_math.h">
<Filter>src\opt</Filter>
</ClInclude>
<ClInclude Include="..\src\optionals\gravity_http.h">
<Filter>src\opt</Filter>
</ClInclude>
<ClInclude Include="..\src\optionals\gravity_optionals.h">
<Filter>src\opt</Filter>
</ClInclude>
Expand Down Expand Up @@ -185,6 +188,9 @@
<ClCompile Include="..\src\optionals\gravity_math.c">
<Filter>src\opt</Filter>
</ClCompile>
<ClCompile Include="..\src\optionals\gravity_http.c">
<Filter>src\opt</Filter>
</ClCompile>
<ClCompile Include="..\src\compiler\gravity_ast.c">
<Filter>src\compiler</Filter>
</ClCompile>
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/gravity_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,11 @@ static void parser_register_optional_classes (gravity_parser_t *parser) {
gnode_array_push(decls, decl2);
#endif

#ifdef GRAVITY_INCLUDE_HTTP
gnode_t *decl3 = gnode_variable_create(NO_TOKEN, string_dup(GRAVITY_HTTP_NAME()), NULL, NULL, LAST_DECLARATION(), NULL);
gnode_array_push(decls, decl3);
#endif

// check if optional classes callback is registered
if (parser->delegate && parser->delegate->optional_classes) {
const char **list = parser->delegate->optional_classes();
Expand Down
Loading