我已经部署到铁路和mongodb集合命名为联系人添加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]](https://stackoverflow.com/cdn-cgi/l/email-protection) :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
,但也没有找到?我如何修复它?
1条答案
按热度按时间qlfbtfca1#
使用load_unsafe对我来说很有用。
字符串
}