From 85d5e342c1020a3e225cd454f75f60c3660fd508 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 19 May 2021 10:40:28 +0200 Subject: [PATCH] Added current project files --- api/build.hxml | 8 +++ api/res/html/index.html | 0 api/res/html/index.js | 0 api/res/json/projects.json | 20 ++++++++ api/src/Server.hx | 78 +++++++++++++++++++++++++++++ api/src/Utils.hx | 24 +++++++++ api/src/VDom.hx | 0 api/src/html/BaseRenderable.hx | 8 +++ api/src/html/Document.hx | 18 +++++++ api/src/html/RenderableContainer.hx | 7 +++ api/src/html/Tag.hx | 44 ++++++++++++++++ api/src/res/a | 0 api/src/res/json/projects.json | 20 ++++++++ 13 files changed, 227 insertions(+) create mode 100644 api/build.hxml create mode 100644 api/res/html/index.html create mode 100644 api/res/html/index.js create mode 100644 api/res/json/projects.json create mode 100644 api/src/Server.hx create mode 100644 api/src/Utils.hx create mode 100644 api/src/VDom.hx create mode 100644 api/src/html/BaseRenderable.hx create mode 100644 api/src/html/Document.hx create mode 100644 api/src/html/RenderableContainer.hx create mode 100644 api/src/html/Tag.hx create mode 100644 api/src/res/a create mode 100644 api/src/res/json/projects.json diff --git a/api/build.hxml b/api/build.hxml new file mode 100644 index 0000000..afbb064 --- /dev/null +++ b/api/build.hxml @@ -0,0 +1,8 @@ +-lib tink_http +-lib tink_web +-lib tink_hxx +-lib tink_json +-lib hxnodejs +-cp src +--main Server +-js build/out.js diff --git a/api/res/html/index.html b/api/res/html/index.html new file mode 100644 index 0000000..e69de29 diff --git a/api/res/html/index.js b/api/res/html/index.js new file mode 100644 index 0000000..e69de29 diff --git a/api/res/json/projects.json b/api/res/json/projects.json new file mode 100644 index 0000000..e0d5f09 --- /dev/null +++ b/api/res/json/projects.json @@ -0,0 +1,20 @@ +{ + "version" : 1, + "projects" : [ + { + "name": "subsonicsnl", + "description": "subsonics.nl website", + "url": "https://subsonics.nl" + }, + { + "name": "drivebycool", + "description": "driveby.cool website", + "url": "https://driveby.cool" + }, + { + "name": "VaV Arena", + "description": "Q3 CPMA inspired arena shooter", + "url": "https://vav.driveby.cool" + } + ] +} \ No newline at end of file diff --git a/api/src/Server.hx b/api/src/Server.hx new file mode 100644 index 0000000..92f3b4b --- /dev/null +++ b/api/src/Server.hx @@ -0,0 +1,78 @@ +import html.Document; +import tink.http.containers.*; +import tink.http.Response; +import tink.web.routing.*; +import haxe.Json; + + + +typedef T_project = { + name : String, + description : String, + url : String +} +typedef T_projects = { + version : Int, + projects : Array +} + +class Server { + static function main() { + var container = new NodeContainer(8080); + //var container = PhpContainer.inst; //use PhpContainer instead of NodeContainer when targeting PHP + var router = new Router(new Root()); + container.run(function(req) { + return router.route(Context.ofRequest(req)) + .recover(OutgoingResponse.reportError); + }); + } +} + +class Root { + public function new() {} + + + @:get('/') + @:produces('text/html') + public function root(){ + return Document.render(); + } + + @:sub('/projects') + public function projects() + return new Projects(); + + @:get('/hello') + @:produces('application/json') + public function hello(name = 'world'){ + var greeting = { hello: name, foo: 42 }; + var strout:String = tink.Json.stringify(greeting); + return strout; + } +} + +class Projects { + public function new() {} + + public var path:String = "./res/json/projects.json"; + + @:get('/') + @:produces('application/json') + public function projects(){ + var json:String = Utils.getJson(path); + return json; + } + @:get('/project/by_name/$name') + @:produces('application/json') + public function project(name:String){ + var json:String = Utils.getJson(path); + var _projects:T_projects = Json.parse(json); + json = "{}"; + for(_project in _projects.projects){ + if(_project.name == name){ + json = tink.Json.stringify(_project); + } + } + return json; + } +} diff --git a/api/src/Utils.hx b/api/src/Utils.hx new file mode 100644 index 0000000..714fdb5 --- /dev/null +++ b/api/src/Utils.hx @@ -0,0 +1,24 @@ +package; + +import sys.io.File; +import sys.FileSystem; + + +class Utils{ + public static function getHTML(path:String):String + { + var strout:String = ""; + if(FileSystem.exists(path)){ + strout = File.getContent(path); + } + return strout; + } + public static function getJson(path:String):String + { + var strout:String = "{}"; + if(FileSystem.exists(path)){ + strout = File.getContent(path); + } + return strout; + } +} \ No newline at end of file diff --git a/api/src/VDom.hx b/api/src/VDom.hx new file mode 100644 index 0000000..e69de29 diff --git a/api/src/html/BaseRenderable.hx b/api/src/html/BaseRenderable.hx new file mode 100644 index 0000000..7d3178e --- /dev/null +++ b/api/src/html/BaseRenderable.hx @@ -0,0 +1,8 @@ +package html; + +class BaseRenderable{ + public var bIsContainer:Bool = true; + public function render(){ + + } +} \ No newline at end of file diff --git a/api/src/html/Document.hx b/api/src/html/Document.hx new file mode 100644 index 0000000..9cbb7a9 --- /dev/null +++ b/api/src/html/Document.hx @@ -0,0 +1,18 @@ +package html; + +class Document{ + public static function render():String + { + var docroot:Tag = new Tag("html"); + var head:Tag = new Tag("head"); + var body:Tag = new Tag("body"); + var h1:Tag = new Tag("h1",['class="yeet"'],"Api endpoints:"); + var a_yeet:Tag = new Tag("a",['href="projects"'],"projects"); + var a_yeet:Tag = new Tag("a",['href="projects/project/by_name/"'],"projects"); + docroot.children.push(head); + docroot.children.push(body); + body.children.push(h1); + body.children.push(a_yeet); + return docroot.render(); + } +} \ No newline at end of file diff --git a/api/src/html/RenderableContainer.hx b/api/src/html/RenderableContainer.hx new file mode 100644 index 0000000..4ff10b8 --- /dev/null +++ b/api/src/html/RenderableContainer.hx @@ -0,0 +1,7 @@ +package html; + +class RenderableContainer extends BaseRenderable{ + override function render() { + super.render(); + } +} \ No newline at end of file diff --git a/api/src/html/Tag.hx b/api/src/html/Tag.hx new file mode 100644 index 0000000..ce01c60 --- /dev/null +++ b/api/src/html/Tag.hx @@ -0,0 +1,44 @@ +package html; + +class Tag{ + public var children:Array = []; + public var name:String = ""; + public var props:Array; + public var textonly:Bool; + public var text:String = ""; + public static var dochead:Tag; + public function new(_name:String, _props:Array=null, _text = "", _textonly=false){ + name = _name; + textonly = _textonly; + if(_props != null) props = _props; + text = _text; + } + public static function createTextElement(_text:String):Tag + { + var tag = new Tag("t",null,_text,true); + tag.text = _text; + return tag; + } + public function render():String + { + var strout:String = ""; + if(!textonly){ + var strProps:String = ""; + if(props != null){ + for(prop in props) strProps+=prop+" "; + } + strout+="<"+name+" "+strProps+" "+">"; + strout+=text; + trace(text); + for(tag in children){ + strout+=tag.render(); + } + strout+=""; + } + else{ + strout = text; + } + trace(strout); + return strout; + } +} \ No newline at end of file diff --git a/api/src/res/a b/api/src/res/a new file mode 100644 index 0000000..e69de29 diff --git a/api/src/res/json/projects.json b/api/src/res/json/projects.json new file mode 100644 index 0000000..e0d5f09 --- /dev/null +++ b/api/src/res/json/projects.json @@ -0,0 +1,20 @@ +{ + "version" : 1, + "projects" : [ + { + "name": "subsonicsnl", + "description": "subsonics.nl website", + "url": "https://subsonics.nl" + }, + { + "name": "drivebycool", + "description": "driveby.cool website", + "url": "https://driveby.cool" + }, + { + "name": "VaV Arena", + "description": "Q3 CPMA inspired arena shooter", + "url": "https://vav.driveby.cool" + } + ] +} \ No newline at end of file