Use Static Middleware with Tink Web
Asked Answered
C

2

5

I want to declare 2 routes. The first one, "/api" will provide some REST stuff, and the other one, "/static" should serve static content.

I tried to start from the quick start samples, but I don't know how to do this.

import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;

class Server {
    static function main() {
        var container = new NodeContainer(8080);
        var router = new Router<Root>(new Root());
        container.run(function(req) {
            return router.route(Context.ofRequest(req))
                .recover(OutgoingResponse.reportError);
        });
    }
}

class Root {
    public function new() {}

    @:get('/')
    @:get('/$name')
    public function hello(name = 'World')
        return 'Hello, $name!';
}
Contract answered 22/5, 2019 at 12:38 Comment(0)
C
1

First of all, i had to use git version of tink_http_middleware and asys

-lib tink_web
-lib hxnodejs
-lib tink_http_middleware:git:https://github.com/haxetink/tink_http_middleware.git
-lib asys:git:https://github.com/benmerckx/asys.git
-main server.Api
-js www/api/api.js

Next, @KevinResoL's answer was very helpful, i changed only one thing. staticMiddleware.apply(handler)

package server;

import tink.http.Handler;
import tink.http.middleware.Static;
import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;

class Api {
    public static function main() {

        var container = new NodeContainer(8080);
        var router = new Router<ApiRoute>(new ApiRoute());
        var staticMiddleware = new Static("..","/");
        var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
        container.run(staticMiddleware.apply(handler));
    }
}

class ApiRoute {
    public function new() { }

    @:sub public var api:Root = new Root();
}

class Root {

    public function new() { }

    @:get('/')
    @:get('/$name')
    public function serve(name = 'index.html')
        return 'Hello, $name!';
}
Contract answered 24/5, 2019 at 8:25 Comment(0)
A
5

For /static you can use Static from tink_http_middleware.
For /api you can use a @:sub route.

import tink.http.containers.*;
import tink.http.Handler;
import tink.http.Response;
import tink.http.middleware.Static;
import tink.web.routing.*;

class Server {
    static function main() {
        var container = new NodeContainer(8080);
        var router = new Router<Api>(new Api());
        var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
        container.run(handler.applyMiddleware(new Static('public_html', '/static')));
    }
}

class Api {
    @:sub public var api:Root = new Root();
}

class Root {
    public function new() {}

    @:get('/')
    @:get('/$name')
    public function hello(name = 'World')
        return 'Hello, $name!';
}
Animism answered 22/5, 2019 at 14:45 Comment(1)
I've got now a compile error : C:\HaxeToolkit\haxe\lib\tink_http_middleware/0,1,0/src/tink/http/middleware/Static.hx:79: characters 12-27 : Not supported. while i use -lib hxnodejsContract
C
1

First of all, i had to use git version of tink_http_middleware and asys

-lib tink_web
-lib hxnodejs
-lib tink_http_middleware:git:https://github.com/haxetink/tink_http_middleware.git
-lib asys:git:https://github.com/benmerckx/asys.git
-main server.Api
-js www/api/api.js

Next, @KevinResoL's answer was very helpful, i changed only one thing. staticMiddleware.apply(handler)

package server;

import tink.http.Handler;
import tink.http.middleware.Static;
import tink.http.containers.*;
import tink.http.Response;
import tink.web.routing.*;

class Api {
    public static function main() {

        var container = new NodeContainer(8080);
        var router = new Router<ApiRoute>(new ApiRoute());
        var staticMiddleware = new Static("..","/");
        var handler:Handler = req -> router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError);
        container.run(staticMiddleware.apply(handler));
    }
}

class ApiRoute {
    public function new() { }

    @:sub public var api:Root = new Root();
}

class Root {

    public function new() { }

    @:get('/')
    @:get('/$name')
    public function serve(name = 'index.html')
        return 'Hello, $name!';
}
Contract answered 24/5, 2019 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.