I have deployed to railway and mongodb collections named contacts is added with josn file. I want contacts' data display on webpage. Then I add a mongodb c++ drivers by create a dockerfile in hello_crow/bbox. I use the commanddocker build --rm --no-cache --squash -t bbox:latest .
to build a image. It display a warning message:
WARNING: experimental flag squash is removed with BuildKit. You should squash inside build using a multi-stage Dockerfile for efficiency.
In hello_crow directory, 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})
In hello_crow/, I create a dockerfile as below.
FROM bbox:latest
WORKDIR /usr/src/cppweb/hello_crow
COPY . .
WORKDIR /usr/src/cppweb/hello_crow/build
RUN cmake .
RUN make
CMD ["./hello_crow"]
I use the commanddocker build --rm --no-cache --squash -t hello_crow:latest .
to build a image. Also have last warning.
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
When I run command docker run -p 8080:8080 -e PORT=8080 -e MONGO_URL="mongodb://myusername:mypassword@containers-us-west-175.railway.app:7978" hello_crow:latest
. The webpage localhost:8080
and localhost:8080/about
displayed fine. But localhost:8080/contacts
display blank.
Why mongdb collections can't display. How can I fix it?
Answer: change getenv()
and conn[database name][contacts].The page localhost:8080/contacts
diplays data fine.
Another issue:When use crow::mustacle to render page /contacts. I run docker build
and docker run
again.The page become blank, can't find contacts.html. How can I fix it?
Answer:Replce load
to load_unsafe
and add set_base(".")
in contacts route. It can display data with table.
But when I add new contact.html
in public,qerry email with localhost:8080/contact/<email>
. Error message is "Template "../public/contact.html" not found.". Why I set it same to contacts route
, but also not found? How can I fix it?