switch server to rust based since it's much easier
This commit is contained in:
		
							parent
							
								
									465b2de7f2
								
							
						
					
					
						commit
						09f3ea62f2
					
				
					 7 changed files with 1974 additions and 18 deletions
				
			
		
							
								
								
									
										5
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -2,3 +2,8 @@ public/
 | 
			
		|||
_vendor
 | 
			
		||||
/.direnv/
 | 
			
		||||
/.hugo_build.lock
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Added by cargo
 | 
			
		||||
 | 
			
		||||
/target
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1861
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										1861
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										15
									
								
								Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Cargo.toml
									
										
									
									
									
										Normal 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"
 | 
			
		||||
| 
						 | 
				
			
			@ -68,9 +68,9 @@
 | 
			
		|||
     fetch("http://localhost:4242/health-form", {
 | 
			
		||||
         method: "POST",
 | 
			
		||||
         headers: {
 | 
			
		||||
             "Content-Type": "application/json",
 | 
			
		||||
             "Content-Type": "multipart/form-data",
 | 
			
		||||
         },
 | 
			
		||||
         body: JSON.stringify(Object.fromEntries(data))
 | 
			
		||||
         body: data
 | 
			
		||||
     }).then((res) => {
 | 
			
		||||
         console.log(res);
 | 
			
		||||
     });
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										13
									
								
								shell.nix
									
										
									
									
									
								
							
							
						
						
									
										13
									
								
								shell.nix
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -4,6 +4,12 @@ mkShell rec {
 | 
			
		|||
  name = "tfc-env";
 | 
			
		||||
 | 
			
		||||
  nativeBuildInputs = [
 | 
			
		||||
    gcc
 | 
			
		||||
    stdenv
 | 
			
		||||
    gnumake
 | 
			
		||||
    gdb
 | 
			
		||||
    pkg-config
 | 
			
		||||
    makeWrapper
 | 
			
		||||
  ];
 | 
			
		||||
 | 
			
		||||
  buildInputs = [
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +21,13 @@ mkShell rec {
 | 
			
		|||
    clj-kondo
 | 
			
		||||
    sbcl
 | 
			
		||||
    openssl
 | 
			
		||||
 | 
			
		||||
    clippy
 | 
			
		||||
    rustc
 | 
			
		||||
    cargo
 | 
			
		||||
    rustfmt
 | 
			
		||||
    rust-analyzer
 | 
			
		||||
    corrosion
 | 
			
		||||
  ];
 | 
			
		||||
  
 | 
			
		||||
  shellHook = ''
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										25
									
								
								src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/main.rs
									
										
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,40 +1,77 @@
 | 
			
		|||
(load "~/quicklisp/setup.lisp")
 | 
			
		||||
(ql:quickload :hunchentoot)
 | 
			
		||||
(ql:quickload :clack)
 | 
			
		||||
(ql:quickload :drakma)
 | 
			
		||||
(ql:quickload :com.inuoe.jzon)
 | 
			
		||||
(ql:quickload :rfc2388)
 | 
			
		||||
 | 
			
		||||
(uiop:define-package tfc-server
 | 
			
		||||
  (:use :cl :hunchentoot :com.inuoe.jzon))
 | 
			
		||||
  (:use :cl :hunchentoot :com.inuoe.jzon :clack))
 | 
			
		||||
 | 
			
		||||
(in-package :tfc-server)
 | 
			
		||||
 | 
			
		||||
(defun handle-post-request (request)
 | 
			
		||||
  (let ((form-data (hunchentoot:req request)))
 | 
			
		||||
    (with-response-buffer (stream :content-type "text/plain")
 | 
			
		||||
      (format stream "Received form data: ~A" form-data))))
 | 
			
		||||
(defun handler (env) '(200 nil ("Hello World, bleh!")))
 | 
			
		||||
 | 
			
		||||
(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)
 | 
			
		||||
  (setf (hunchentoot:content-type*) "text/plain")
 | 
			
		||||
  (format nil "Hey~@[ ~A~]!" name))
 | 
			
		||||
 | 
			
		||||
(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*)))
 | 
			
		||||
    (cond ((eq request-type :get) nil)
 | 
			
		||||
          ((eq request-type :post)
 | 
			
		||||
           (let* ((data (hunchentoot:raw-post-data :force-text t))
 | 
			
		||||
                  (json-obj (com.inuoe.jzon:parse data))
 | 
			
		||||
           (uiop:println hunchentoot:*request*)
 | 
			
		||||
           (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 "Received Health Form")
 | 
			
		||||
             (render-json json-obj)
 | 
			
		||||
             (setq last json-obj)
 | 
			
		||||
             (process-form json-obj)
 | 
			
		||||
             data)))))
 | 
			
		||||
             (uiop:println (sb-sys:fd-stream-fd stream))
 | 
			
		||||
             (uiop:println (open-stream-p stream))
 | 
			
		||||
             (uiop:println (write-byte 8 stream))
 | 
			
		||||
             ;; (uiop:println (flex:octets-to-string 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")
 | 
			
		||||
 | 
			
		||||
(defun print-hash-entry (key value)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue