我有如下代碼:訪問示例以外的類方法實例的正確方法是什麼?
import std.stdio;
import database;
import router;
import config;
import vibe.d;
void main()
{
Config config = new Config();
auto settings = new HTTPServerSettings;
settings.port = 8081;
settings.bindAddresses = ["::1", "127.0.0.1"];
auto router = new URLRouter();
router.get("/*", serveStaticFiles("./html"));
Database database = new Database(config);
database.MySQLConnect(); // all DB methods are declared here
router.registerRestInterface(new MyRouter(database));
router.get("*", &myStuff); // all other request
listenHTTP(settings, router);
logInfo("Please open http://127.0.0.1:8081/ in your browser.");
runApplication();
}
void myStuff(HTTPServerRequest req, HTTPServerResponse res) // I need this to handle any accessed URLs
{
writeln(req.path); // getting URL that was request on server
// here I need access to DB methods to do processing and return some DB data
}
我需要創建router.get("*", &myStuff);
處理任何網址,不涉及任何REST實例。
,我不知道如何從myStuff()
使'數據庫''共享'並將其移動到模塊範圍? –