Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the need for #define CROW_MAIN #280

Merged
merged 3 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Available [here](https://crowcpp.org).

#### Hello World
```cpp
#define CROW_MAIN
#include "crow.h"

int main()
Expand Down
7 changes: 1 addition & 6 deletions docs/getting_started/your_first_application.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
This page shows how you can get started with a simple hello world application.

## 1. Include
Starting with an empty `main.cpp` file, first add `#!cpp #define CROW_MAIN` then `#!cpp #include "crow.h"` or `#!cpp #include "crow_all.h"` if you're using the single header file.

!!! note

If you're using multiple C++ source files make sure to have `CROW_MAIN` defined only in your main source file.
Starting with an empty `main.cpp` file, first add `#!cpp #include "crow.h"` or `#!cpp #include "crow_all.h"` if you're using the single header file.

## 2. App declaration
Next Create a `main()` and declare a `#!cpp crow::SimpleApp` inside, your code should look like this
Expand Down Expand Up @@ -39,7 +35,6 @@ Please note that the `port()` and `multithreaded()` methods aren't needed, Thoug
Once you've followed all the steps above, your code should look similar to this

``` cpp linenums="1"
#define CROW_MAIN //let the compiler know this is your main cpp file
#include "crow.h"
//#include "crow_all.h"

Expand Down
2 changes: 1 addition & 1 deletion docs/overrides/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ <h1 style="text-align:center;">A Fast and Easy to use microframework for the web
<h2 style="text-align: center;">Easy to get started</h3>
</div>
<div class="scontent">
<div class="highlight"><pre id="__code_0"><span></span><button class="md-clipboard md-icon" title="Copy to clipboard" data-clipboard-target="#__code_0 > code"></button><code><span class="cp">#define CROW_MAIN</span>
<div class="highlight"><pre id="__code_0"><span></span><button class="md-clipboard md-icon" title="Copy to clipboard" data-clipboard-target="#__code_0 > code"></button><code>
<span class="cp">#include</span> <span class="cpf">"crow.h"</span><span class="cp"></span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
Expand Down
1 change: 0 additions & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"

#include <sstream>
Expand Down
1 change: 0 additions & 1 deletion examples/example_blueprint.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"

int main()
Expand Down
1 change: 0 additions & 1 deletion examples/example_catchall.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include <crow.h>


Expand Down
1 change: 0 additions & 1 deletion examples/example_chat.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"
#include <string>
#include <vector>
Expand Down
1 change: 0 additions & 1 deletion examples/example_compression.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"
#include "crow/compression.h"

Expand Down
1 change: 0 additions & 1 deletion examples/example_json_map.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#define CROW_JSON_USE_MAP
#include "crow.h"

Expand Down
7 changes: 3 additions & 4 deletions examples/example_static_file.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//#define CROW_STATIC_DRIECTORY "alternative_directory/"
//#define CROW_STATIC_ENDPOINT "/alternative_endpoint/<path>"
//#define CROW_DISABLE_STATIC_DIR
#define CROW_MAIN
#include "crow.h"

int main()
Expand All @@ -18,10 +17,10 @@ CROW_ROUTE(app, "/")
res.end();
});


app.port(18080).run();


return 0;
}

Expand Down
7 changes: 3 additions & 4 deletions examples/example_vs.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"

#include <sstream>
Expand All @@ -10,7 +9,7 @@ class ExampleLogHandler : public crow::ILogHandler {
}
};

struct ExampleMiddleware
struct ExampleMiddleware
{
std::string message;

Expand Down Expand Up @@ -108,7 +107,7 @@ int main()
app.route_dynamic("/params")
([](const crow::request& req){
std::ostringstream os;
os << "Params: " << req.url_params << "\n\n";
os << "Params: " << req.url_params << "\n\n";
os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
if(req.url_params.get("pew") != nullptr) {
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
Expand All @@ -120,7 +119,7 @@ int main()
os << " - " << countVal << '\n';
}
return crow::response{os.str()};
});
});

// ignore all log
crow::logger::setLogLevel(crow::LogLevel::Debug);
Expand Down
5 changes: 2 additions & 3 deletions examples/example_with_all.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow_all.h"

#include <sstream>
Expand Down Expand Up @@ -96,7 +95,7 @@ int main()
CROW_ROUTE(app, "/params")
([](const crow::request& req){
std::ostringstream os;
os << "Params: " << req.url_params << "\n\n";
os << "Params: " << req.url_params << "\n\n";
os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
if(req.url_params.get("pew") != nullptr) {
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
Expand All @@ -108,7 +107,7 @@ int main()
os << " - " << countVal << '\n';
}
return crow::response{os.str()};
});
});

// ignore all log
crow::logger::setLogLevel(crow::LogLevel::Debug);
Expand Down
1 change: 0 additions & 1 deletion examples/helloworld.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"

int main()
Expand Down
3 changes: 1 addition & 2 deletions examples/ssl/example_ssl.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"

int main()
Expand All @@ -14,7 +13,7 @@ int main()

// Use .pem file
//app.port(18080).ssl_file("test.pem").run();

// Use custom context; see boost::asio::ssl::context
/*
* crow::ssl_context_t ctx;
Expand Down
3 changes: 1 addition & 2 deletions examples/websocket/example_ws.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define CROW_MAIN
#include "crow.h"
#include <unordered_set>
#include <mutex>
Expand Down Expand Up @@ -38,7 +37,7 @@ int main()
gethostname(name, 256);
crow::mustache::context x;
x["servername"] = name;

auto page = crow::mustache::load("ws.html");
return page.render(x);
});
Expand Down
8 changes: 4 additions & 4 deletions include/crow/http_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ namespace crow
{
*this = std::move(r);
}

response(std::string contentType, std::string body) : body(std::move(body))
{
set_header("Content-Type",mime_types[contentType]);
set_header("Content-Type", mime_types.at(contentType));
}

response(int code, std::string contentType, std::string body): code(code),body(std::move(body))
{
set_header("Content-Type",mime_types[contentType]);
set_header("Content-Type", mime_types.at(contentType));
}

response& operator = (const response& r) = delete;
Expand Down Expand Up @@ -221,7 +221,7 @@ namespace crow
this->add_header("Content-length", std::to_string(file_info.statbuf.st_size));

if (extension != ""){
mimeType = mime_types[extension];
mimeType = mime_types.at(extension);
if (mimeType != "")
this-> add_header("Content-Type", mimeType);
else
Expand Down
7 changes: 1 addition & 6 deletions include/crow/mime_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <string>

namespace crow {

#ifdef CROW_MAIN
std::unordered_map<std::string, std::string> mime_types {
const std::unordered_map<std::string, std::string> mime_types {
{"shtml", "text/html"},
{"htm", "text/html"},
{"html", "text/html"},
Expand Down Expand Up @@ -115,7 +113,4 @@ namespace crow {
{"wmv", "video/x-ms-wmv"},
{"avi", "video/x-msvideo"}
};
#else
extern std::unordered_map<std::string, std::string> mime_types;
#endif
}
7 changes: 2 additions & 5 deletions include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
namespace crow
{

#ifdef CROW_MAIN
uint16_t INVALID_BP_ID{0xFFFF};
#else
extern uint16_t INVALID_BP_ID;
#endif
constexpr const uint16_t INVALID_BP_ID{0xFFFF};

/// A base class for all rules.

/// Used to provide a common interface for code dealing with different types of rules.
Expand Down
7 changes: 1 addition & 6 deletions include/crow/version.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#pragma once

namespace crow {

#ifdef CROW_MAIN
char VERSION[] = "master";
#else
extern char VERSION[];
#endif
constexpr const char VERSION[] = "master";
}
7 changes: 1 addition & 6 deletions scripts/nginx_mime2cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def main():
"#include <string>",
"",
"namespace crow {",
"",
"#ifdef CROW_MAIN"
tabspace + "std::unordered_map<std::string, std::string> mime_types {"])
tabspace + "const std::unordered_map<std::string, std::string> mime_types {"])

with open(file_path, "r") as mtfile:
incomplete = ""
Expand All @@ -44,9 +42,6 @@ def main():
outLines[-1] = outLines[-1][:-1]
outLines.extend([
tabspace + "};",
"#else",
"extern std::unordered_map<std::string, std::string> mime_types;",
"#endif",
"}"
])

Expand Down
1 change: 0 additions & 1 deletion tests/ssl/ssltest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#define CATCH_CONFIG_MAIN
#define CROW_LOG_LEVEL 0
#define CROW_MAIN

#include <thread>

Expand Down
1 change: 0 additions & 1 deletion tests/unittest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#define CATCH_CONFIG_MAIN
#define CROW_ENABLE_DEBUG
#define CROW_LOG_LEVEL 0
#define CROW_MAIN
#include <sys/stat.h>

#include <iostream>
Expand Down