switch server to rust based since it's much easier

This commit is contained in:
Chris Cochrun 2023-05-26 16:21:21 -05:00
parent 465b2de7f2
commit 09f3ea62f2
7 changed files with 1974 additions and 18 deletions

5
.gitignore vendored
View file

@ -2,3 +2,8 @@ public/
_vendor _vendor
/.direnv/ /.direnv/
/.hugo_build.lock /.hugo_build.lock
# Added by cargo
/target

1861
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

15
Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "tfcconnection"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix = "0.13.0"
actix-rt = "2.8.0"
actix-web = "4.3.1"
env_logger = "0.10.0"
reqwest = { version = "0.11.18", features = ["json", "multipart"] }
serde = "1.0.163"
serde_json = "1.0.96"

View file

@ -68,9 +68,9 @@
fetch("http://localhost:4242/health-form", { fetch("http://localhost:4242/health-form", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "multipart/form-data",
}, },
body: JSON.stringify(Object.fromEntries(data)) body: data
}).then((res) => { }).then((res) => {
console.log(res); console.log(res);
}); });

View file

@ -4,6 +4,12 @@ mkShell rec {
name = "tfc-env"; name = "tfc-env";
nativeBuildInputs = [ nativeBuildInputs = [
gcc
stdenv
gnumake
gdb
pkg-config
makeWrapper
]; ];
buildInputs = [ buildInputs = [
@ -15,6 +21,13 @@ mkShell rec {
clj-kondo clj-kondo
sbcl sbcl
openssl openssl
clippy
rustc
cargo
rustfmt
rust-analyzer
corrosion
]; ];
shellHook = '' shellHook = ''

25
src/main.rs Normal file
View file

@ -0,0 +1,25 @@
use std::{env, io};
use actix_web::{middleware, web, App, HttpServer, get, post, Responder, http::header::ContentType, HttpResponse, HttpRequest, http::{StatusCode, Method}};
#[actix_web::main]
async fn main() -> io::Result<()> {
env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
env_logger::init();
HttpServer::new(|| {
App::new()
// enable logger - always register actix-web Logger middleware last
.wrap(middleware::Logger::default())
// register HTTP requests handlers
.service(form)
})
.bind("0.0.0.0:4242")?
.run()
.await
}
#[post("/health-form")]
async fn form(req: HttpRequest) -> HttpResponse {
println!("{req:?}");
HttpResponse::Ok().respond_to(&req)
}

View file

@ -1,40 +1,77 @@
(load "~/quicklisp/setup.lisp") (load "~/quicklisp/setup.lisp")
(ql:quickload :hunchentoot) (ql:quickload :hunchentoot)
(ql:quickload :clack)
(ql:quickload :drakma) (ql:quickload :drakma)
(ql:quickload :com.inuoe.jzon) (ql:quickload :com.inuoe.jzon)
(ql:quickload :rfc2388)
(uiop:define-package tfc-server (uiop:define-package tfc-server
(:use :cl :hunchentoot :com.inuoe.jzon)) (:use :cl :hunchentoot :com.inuoe.jzon :clack))
(in-package :tfc-server) (in-package :tfc-server)
(defun handle-post-request (request) (defun handler (env) '(200 nil ("Hello World, bleh!")))
(let ((form-data (hunchentoot:req request)))
(with-response-buffer (stream :content-type "text/plain")
(format stream "Received form data: ~A" form-data))))
(push (create-prefix-dispatcher "/data" 'handle-post-request) hunchentoot:*dispatch-table*) (clack:stop (clack:clackup (lambda (env)
'(200 nil ("Hello, World!")))
(clack.handler:stop *clack-server*)
(defparameter *clack-server* (clack:clackup (lambda (env)
(funcall 'handler env))))
(hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242))
(hunchentoot:define-easy-handler (say-yo :uri "/yo") (name) (hunchentoot:define-easy-handler (say-yo :uri "/yo") (name)
(setf (hunchentoot:content-type*) "text/plain") (setf (hunchentoot:content-type*) "text/plain")
(format nil "Hey~@[ ~A~]!" name)) (format nil "Hey~@[ ~A~]!" name))
(hunchentoot:define-easy-handler (respond :uri "/health-form") () (hunchentoot:define-easy-handler (respond :uri "/health-form") ()
(setf (hunchentoot:content-type*) "multipart/form-data") (setf (hunchentoot:content-type*) "application/json")
(let ((request-type (hunchentoot:request-method hunchentoot:*request*))) (let ((request-type (hunchentoot:request-method hunchentoot:*request*)))
(cond ((eq request-type :get) nil) (cond ((eq request-type :get) nil)
((eq request-type :post) ((eq request-type :post)
(let* ((data (hunchentoot:raw-post-data :force-text t)) (uiop:println hunchentoot:*request*)
(json-obj (com.inuoe.jzon:parse data)) (setq *last* hunchentoot:*request*)
(setq *stream* (hunchentoot::content-stream hunchentoot:*request*))
(let* ((stream (hunchentoot::content-stream hunchentoot:*request*))
(data (hunchentoot::get-post-data :request *last*))
) )
;; (print (format nil "Json: ~A" (com.inuoe.jzon:stringify json-obj))) (uiop:println (sb-sys:fd-stream-fd stream))
(uiop:println "Received Health Form") (uiop:println (open-stream-p stream))
(render-json json-obj) (uiop:println (write-byte 8 stream))
(setq last json-obj) ;; (uiop:println (flex:octets-to-string data))
(process-form json-obj)
data)))))
(defvar last) ;; (let* ((header (rfc2388:parse-header "multipart/form-data" :value))
;; (boundary (or (assoc "boundary" (rfc2388:header-parameters header)
;; :test #'string-equal)
;; (error "Form data is missing a boundary: ~S"
;; "multipart/form-data"))))
;; (uiop:println (rfc2388:parse-mime data boundary)))
;; )
(uiop:println "OTHER")
(uiop:println (hunchentoot::parse-multipart-form-data *last* *hunchentoot-default-external-format*))
;; (uiop:println (hunchentoot::headers-in *last*))
;; (uiop:println (hunchentoot::get-request-data stream))
;; (uiop:println (rfc2388:parse-mime data boundary))
;; (let* ((data (hunchentoot:raw-post-data :force-text t))
;; (json-obj (com.inuoe.jzon:parse data))
;; )
;; ;; (print (format nil "Json: ~A" (com.inuoe.jzon:stringify json-obj)))
;; (uiop:println "Received Health Form")
;; (render-json json-obj)
;; (setq last json-obj)
;; (process-form json-obj)
;; data)
)))))
(defun display-stream (stream)
(loop for line = (read-line stream nil)
while line do (format t "~A~%" line)))
(defvar *last*)
(defvar *stream*)
(defvar *token* "boFFRM68vvXbO-DO7S9YDg86lHX027-hd07mn0dh") (defvar *token* "boFFRM68vvXbO-DO7S9YDg86lHX027-hd07mn0dh")
(defun print-hash-entry (key value) (defun print-hash-entry (key value)