Mongodb 集合数据在使用 Crow 的网页上显示为空白

问题描述 投票:0回答:1

我已经部署到铁路和 mongodb 集合,名为 contact 并添加了 josn 文件。我希望在网页上显示联系人的数据。然后我通过在 hello_crow/bbox 中创建 dockerfile 来添加 mongodb c++ 驱动程序。我使用命令

docker build --rm --no-cache --squash -t bbox:latest .
来构建图像。它显示一条警告消息:

WARNING: experimental flag squash is removed with BuildKit. You should squash inside build using a multi-stage Dockerfile for efficiency.

hello_crow目录下,main.cpp:

#include "crow_all.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <boost/filesystem.hpp>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/oid.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::basic::kvp;
using mongocxx::cursor;
using namespace std;
using namespace crow;

crow::mustache::rendered_template getView(const string& filename, context& x)
{
    auto page=load("../public/" + filename + ".html");
    return page.render(x);
}

int main(int argc, char* argv[]) 
{
    crow::SimpleApp app;
    set_base(".");

    mongocxx::instance inst{};
    string mongoConnect=std::string(getenv("MONGO_URL"));
    mongocxx::client conn{mongocxx::uri{mongoConnect}};
    auto collection=conn["containers-us-west-175.railway.app:7978"]["contacts"];
       
    CROW_ROUTE(app, "/contact/<string>")
    ([&collection](string email)
    {
        set_base(".");
        auto doc=collection.find_one(make_document(kvp("email",email)));
        crow::json::wvalue dto;
        dto["contact"]=json::load(bsoncxx::to_json(doc.value().view())) ;
        return getView("contact", dto);;
    }); 
    
    CROW_ROUTE(app, "/contacts")
    ([&collection]()
    {
        set_base(".");
        mongocxx::options::find opts;
        opts.skip(9);
        opts.limit(10);
        auto docs=collection.find({}, opts);
        crow::json::wvalue dto;
        vector<crow::json::rvalue> contacts;
        contacts.reserve(10);

        for (auto doc : docs)
        {
            contacts.push_back(json::load(bsoncxx::to_json(doc)));
        }
        dto["contacts"] = contacts;
        return getView("contacts", dto);
    });

    CROW_ROUTE(app, "/")
    ([](const crow::request& req, crow::response& res)
    {
        sendHtml(res, "index");
    });

    char* port = getenv("PORT");
    uint16_t iPort = static_cast<uint16_t>(port != NULL? stoi(port): 18080);
    cout << "PORT = " << iPort << endl;
    app.port(iPort).multithreaded().run();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(hello_crow)

set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package(Boost COMPONENTS system filesystem REQUIRED)
find_package(Threads REQUIRED)
find_package(libmongocxx REQUIRED)

add_executable(hello_crow main.cpp)
target_include_directories(hello_crow PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS})
target_link_libraries(hello_crow ${Boost_LIBRARIES} Threads::Threads ${LIBMONGOCXX_LIBRARIES})

在 hello_crow/ 中,我创建了一个 dockerfile,如下所示。

FROM bbox:latest

WORKDIR /usr/src/cppweb/hello_crow
COPY . .

WORKDIR /usr/src/cppweb/hello_crow/build
RUN cmake .
RUN make
CMD ["./hello_crow"]

我使用命令

docker build --rm --no-cache --squash -t hello_crow:latest .
来构建图像。还有最后的警告。

hello_crow:
│  CMakeLists.txt
│  crow_all.h
│  Dockerfile
│  main.cpp
├─bbox
│      Dockerfile
├─build
│
└─public
    │  contact.html
    │  contacts.html
       index.html    
    ├─images
    │      cat.jfif
    │      jerry.png    
    ├─scripts
    │      test.js    
    └─styles
            style.css

当我运行命令

docker run -p 8080:8080 -e PORT=8080 -e MONGO_URL="mongodb://myusername:[email protected]:7978" hello_crow:latest
时。网页
localhost:8080
localhost:8080/about
显示良好。但
localhost:8080/contacts
显示空白。 为什么mongdb集合无法显示。我该如何解决它?
答:更改
getenv()
和conn[数据库名称][联系人]。页面
localhost:8080/contacts
可以正常显示数据了。
另一个问题:当使用 crow::mustacle 渲染页面/联系人时。我再次运行
docker build
docker run
。页面变成空白,找不到contacts.html。我该如何解决它?
答:将
load
替换为
load_unsafe
,并在联系人路由中添加
set_base(".")
。可以用表格显示数据。
但是当我在公共场合添加新的
contact.html
时,qerry电子邮件中包含
localhost:8080/contact/<email>
。错误消息是“未找到模板“../public/contact.html”。”。为什么我把它设置为
contacts route
,但还是找不到?我该如何解决它?

c++ mongodb railway crow
1个回答
0
投票

使用 load_unsafe 对我有用。

crow::mustache::rendered_template getView(const string &filename, context &x) {
auto page=load_unsafe("../public/" + filename + ".html");
return page.render(x);

}

© www.soinside.com 2019 - 2024. All rights reserved.