-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
109 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,58 @@ | ||
#include "gudov/http/http_server.h" | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
#include <fstream> | ||
#include <string> | ||
|
||
#include "gudov/config.h" | ||
#include "gudov/log.h" | ||
|
||
gudov::Logger::ptr g_logger = LOG_ROOT(); | ||
|
||
bool exist_file(const std::string& filename) { | ||
std::ifstream f(filename); | ||
return !f.bad(); | ||
} | ||
|
||
std::string read_file(const std::string& filename) { | ||
std::ifstream f(filename); | ||
return std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); | ||
} | ||
#include "gudov/gudov.h" | ||
#include "gudov/http/http.h" | ||
|
||
using gudov::Address; | ||
using gudov::Config; | ||
using gudov::EnvMgr; | ||
using gudov::FSUtil; | ||
using gudov::IOManager; | ||
using gudov::Logger; | ||
using gudov::LogLevel; | ||
using gudov::http::HttpRequest; | ||
using gudov::http::HttpResponse; | ||
using gudov::http::HttpServer; | ||
using gudov::http::HttpSession; | ||
|
||
Logger::ptr g_logger = LOG_ROOT(); | ||
|
||
void run() { | ||
if (exist_file("conf/http_server.yml")) { | ||
YAML::Node root = YAML::LoadFile("conf/http_server.yml"); | ||
gudov::Config::LoadFromYaml(root); | ||
LOG_INFO(g_logger) << "Successfully load config file 'conf/http_server.yml'"; | ||
} else { | ||
LOG_INFO(g_logger) << "config file 'conf/http_server.yml' doesn't exists, " | ||
"use default configurations"; | ||
} | ||
gudov::Address::ptr addr = gudov::Address::LookupAnyIPAddress("0.0.0.0:8020"); | ||
if (!addr) { | ||
LOG_ERROR(g_logger) << "get address error"; | ||
return; | ||
} | ||
g_logger->SetLevel(LogLevel::INFO); | ||
|
||
gudov::http::HttpServer::ptr http_server(new gudov::http::HttpServer(true)); | ||
while (!http_server->Bind(addr)) { | ||
HttpServer::ptr server(new HttpServer(true)); | ||
Address::ptr addr = Address::LookupAnyIPAddress("0.0.0.0:8020"); | ||
|
||
while (!server->Bind(addr)) { | ||
LOG_ERROR(g_logger) << "Bind " << *addr << " fail"; | ||
sleep(1); | ||
sleep(2); | ||
} | ||
|
||
auto servlet_dispatcher = http_server->GetServletDispatch(); | ||
auto sd = server->GetServletDispatch(); | ||
|
||
servlet_dispatcher->addServlet("/", [](gudov::http::HttpRequest::ptr req, gudov::http::HttpResponse::ptr rsp, | ||
gudov::http::HttpSession::ptr session) { | ||
rsp->SetBody(read_file("html/index.html")); | ||
sd->AddServlet("/", [](HttpRequest::ptr req, HttpResponse::ptr rsp, HttpSession::ptr session) { | ||
rsp->SetBody(FSUtil::ReadFile("html/index.html")); | ||
return 0; | ||
}); | ||
|
||
servlet_dispatcher->addServlet( | ||
"/index.html", | ||
[](gudov::http::HttpRequest::ptr req, gudov::http::HttpResponse::ptr rsp, gudov::http::HttpSession::ptr session) { | ||
rsp->SetBody(read_file("html/index.html")); | ||
return 0; | ||
}); | ||
sd->AddServlet("/index.html", [](HttpRequest::ptr req, HttpResponse::ptr rsp, HttpSession::ptr session) { | ||
rsp->SetBody(FSUtil::ReadFile("html/index.html")); | ||
return 0; | ||
}); | ||
|
||
servlet_dispatcher->addGlobServlet("/*", [](gudov::http::HttpRequest::ptr req, gudov::http::HttpResponse::ptr rsp, | ||
gudov::http::HttpSession::ptr session) { | ||
rsp->SetBody(read_file("html/404.html")); | ||
sd->AddGlobServlet("/*", [](HttpRequest::ptr req, HttpResponse::ptr rsp, HttpSession::ptr session) { | ||
rsp->SetBody(FSUtil::ReadFile("html/404.html")); | ||
return 0; | ||
}); | ||
|
||
http_server->Start(); | ||
server->Start(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
gudov::IOManager iom(5); | ||
EnvMgr::GetInstance()->Init(argc, argv); | ||
Config::LoadFromConfDir(EnvMgr::GetInstance()->GetConfigPath()); | ||
|
||
IOManager iom(1, true, "http_server"); | ||
|
||
iom.Schedule(run); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
#pragma once | ||
|
||
#include "address.h" | ||
#include "bytearray.h" | ||
#include "config.h" | ||
#include "env.h" | ||
#include "fiber.h" | ||
#include "http/http.h" | ||
#include "http/http_connection.h" | ||
#include "http/http_parser.h" | ||
#include "http/http_server.h" | ||
#include "http/http_session.h" | ||
#include "http/servlet.h" | ||
#include "iomanager.h" | ||
#include "log.h" | ||
#include "macro.h" | ||
#include "scheduler.h" | ||
#include "singleton.h" | ||
#include "socket.h" | ||
#include "socket_stream.h" | ||
#include "tcp_server.h" | ||
#include "thread.h" | ||
#include "util.h" | ||
#include "util.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.