@@ -2,7 +2,7 @@ use journal_sdk::{Config, JOURNAL};
22use log:: info;
33use rocket:: config:: Config as RocketConfig ;
44use rocket:: data:: { Limits , ToByteUnit } ;
5- use rocket:: response:: content:: RawHtml ;
5+ use rocket:: response:: content:: { RawHtml , RawText } ;
66use rocket:: serde:: json:: Json ;
77use rocket:: { get, post, routes} ;
88use serde_json:: Value ;
@@ -20,6 +20,8 @@ const INDEX_HTML: &str = r#"<!DOCTYPE html>
2020 <ul>
2121 <li><a href="/interface">LISP Interface</a></li>
2222 <li><a href="/interface/json">JSON Interface</a></li>
23+ <li><a href="/interface/lisp-to-json">LISP to JSON</a></li>
24+ <li><a href="/interface/json-to-lisp">JSON to LISP</a></li>
2325 </ul>
2426 </body>
2527</html>
@@ -28,7 +30,7 @@ const INDEX_HTML: &str = r#"<!DOCTYPE html>
2830const INTERFACE_HTML : & str = r#"<!DOCTYPE html>
2931<html>
3032 <head>
31- <h2>{} </h2>
33+ <h2>__TITLE__ </h2>
3234 </head>
3335 <body style="padding: 0 20px; font-family: 'Consolas'">
3436 <textarea id="query" rows="8" cols="128" spellcheck="false"></textarea>
@@ -43,9 +45,7 @@ const INTERFACE_HTML: &str = r#"<!DOCTYPE html>
4345 let query = document.getElementById('query').value;
4446 fetch('', {
4547 method: 'POST',
46- headers: {
47- 'Content-Type': 'application/json',
48- },
48+ __HEADERS__
4949 body: query,
5050 }).then(response => {
5151 return response.text();
@@ -76,17 +76,43 @@ async fn index() -> RawHtml<String> {
7676
7777#[ get( "/interface" , format = "text/html" ) ]
7878async fn inform_lisp ( ) -> RawHtml < String > {
79- RawHtml ( INTERFACE_HTML . replace ( "{}" , "LISP Interface" ) )
79+ RawHtml (
80+ INTERFACE_HTML
81+ . replace ( "__TITLE__" , "LISP Interface" )
82+ . replace ( "__HEADERS__" , "" ) ,
83+ )
8084}
8185
8286#[ post( "/interface" , data = "<query>" , rank = 1 ) ]
8387async fn evaluate_lisp ( query : & str ) -> String {
8488 JOURNAL . evaluate ( query)
8589}
8690
91+ #[ get( "/interface/lisp-to-json" , format = "text/html" ) ]
92+ async fn inform_lisp_to_json ( ) -> RawHtml < String > {
93+ RawHtml (
94+ INTERFACE_HTML
95+ . replace ( "__TITLE__" , "LISP to JSON" )
96+ . replace ( "__HEADERS__" , "" ) ,
97+ )
98+ }
99+
100+ #[ post( "/interface/lisp-to-json" , data = "<query>" , rank = 1 ) ]
101+ async fn lisp_to_json ( query : & str ) -> Json < Value > {
102+ let result = JOURNAL . lisp_to_json ( query) ;
103+ Json ( result)
104+ }
105+
87106#[ get( "/interface/json" , format = "text/html" ) ]
88107async fn inform_json ( ) -> RawHtml < String > {
89- RawHtml ( INTERFACE_HTML . replace ( "{}" , "JSON Interface" ) )
108+ RawHtml (
109+ INTERFACE_HTML
110+ . replace ( "__TITLE__" , "JSON Interface" )
111+ . replace (
112+ "__HEADERS__" ,
113+ "headers: { 'Content-Type': 'application/json' }," ,
114+ ) ,
115+ )
90116}
91117
92118#[ post( "/interface/json" , data = "<query>" , format = "json" , rank = 1 ) ]
@@ -95,6 +121,19 @@ async fn evaluate_json(query: Json<Value>) -> Json<Value> {
95121 Json ( result)
96122}
97123
124+ #[ get( "/interface/json-to-lisp" , format = "text/html" ) ]
125+ async fn inform_json_to_lisp ( ) -> RawHtml < String > {
126+ RawHtml ( INTERFACE_HTML . replace ( "__TITLE__" , "JSON to LISP" ) . replace (
127+ "__HEADERS__" ,
128+ "headers: { 'Content-Type': 'application/json' }," ,
129+ ) )
130+ }
131+
132+ #[ post( "/interface/json-to-lisp" , data = "<query>" , format = "json" , rank = 1 ) ]
133+ async fn json_to_lisp ( query : Json < Value > ) -> RawText < String > {
134+ RawText ( JOURNAL . json_to_lisp ( query. into_inner ( ) ) )
135+ }
136+
98137#[ rocket:: main]
99138async fn main ( ) {
100139 let config = Config :: new ( ) ;
@@ -156,8 +195,12 @@ async fn main() {
156195 index,
157196 inform_lisp,
158197 evaluate_lisp,
198+ inform_lisp_to_json,
199+ lisp_to_json,
159200 inform_json,
160- evaluate_json
201+ evaluate_json,
202+ inform_json_to_lisp,
203+ json_to_lisp
161204 ] ,
162205 )
163206 . configure ( rocket_config)
0 commit comments