Skip to content

Commit

Permalink
🎉 (codon/) Added a experiment of server to use codon
Browse files Browse the repository at this point in the history
  • Loading branch information
Comamoca committed Apr 7, 2023
1 parent 0d65e37 commit 4e328c3
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions codon/greet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* greet(char* name) {
char* result = malloc(strlen(name) + 5);
sprintf(result, "Hi! %s!", name);
return result;
}
2 changes: 2 additions & 0 deletions codon/lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def greet(name: str) -> str:
return f"Hi {name}"
7 changes: 7 additions & 0 deletions codon/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import http.server
import socketserver

with socketserver.TCPServer(
("127.0.0.1", 8000), http.server.SimpleHTTPRequestHandler
) as httpd:
httpd.serve_forever()
14 changes: 14 additions & 0 deletions codon/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tool.poetry]
name = "codon"
version = "0.1.0"
description = ""
authors = ["Comamoca <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
13 changes: 13 additions & 0 deletions codon/server/server.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Package

version = "0.1.0"
author = "Comamoca"
description = "A new awesome nimble package"
license = "MIT"
srcDir = "src"
bin = @["server"]


# Dependencies

requires "nim >= 1.6.10"
Binary file added codon/server/src/greet
Binary file not shown.
2 changes: 2 additions & 0 deletions codon/server/src/greet.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
proc greet*(name: cstring): cstring {.exportc.} =
return "Hi! " & "{name}!"
Binary file added codon/server/src/server
Binary file not shown.
24 changes: 24 additions & 0 deletions codon/server/src/server.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asynchttpserver, asyncdispatch, strutils

const HTML = {"Content-type": "text/html; charset=utf-8"}
const JSON = {"Content-type": "application/json; charset=utf-8"}

proc responce(req: Request, code: HttpCode, content: string,
header: auto = HTML) {.async.} =
await req.respond(code, content, header.newHttpHeaders())

proc run*(port: int, content: string) {.async dynlib exportc.} =
var server = newAsyncHttpServer()

proc cb(req: Request) {.async.} =
if req.reqMethod == HttpGet:
let path = req.url.path

case path
of "/":
await req.responce(Http200, content, HTML)

else:
await req.responce(Http404, "Not Found")

waitFor server.serve(Port(port), cb)
Binary file added codon/tmp
Binary file not shown.
10 changes: 10 additions & 0 deletions codon/tmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@python
def run():
import http.server
import socketserver

with socketserver.TCPServer(
("127.0.0.1", 8000), http.server.SimpleHTTPRequestHandler
) as httpd:
httpd.serve_forever()
print("Server Launched🚀")

0 comments on commit 4e328c3

Please sign in to comment.