Skip to content

Commit

Permalink
Old Architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
INotfound authored and INotfound committed Dec 17, 2020
0 parents commit 804d8b3
Show file tree
Hide file tree
Showing 344 changed files with 105,291 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
/.vs
/.vscode
/out
/build
tags
CMakeSettings.json
.ycm_extra_conf.py
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "ThirdParty/Asio"]
path = ThirdParty/Asio
url = https://github.com/chriskohlhoff/asio.git
[submodule "ThirdParty/RapidJSON"]
path = ThirdParty/RapidJSON
url = https://github.com/Tencent/rapidjson
46 changes: 46 additions & 0 deletions Build.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* @File: Build.cpp
* @Author: INotFound
* @Date: 2020-04-04 20:08:11
* @LastEditTime: 2020-04-04 20:11:12
*/
// Util
#include "Source/Magic.cpp"
#include "Source/Magic/Log.cpp"
#include "Source/Magic/Gzip.cpp"
#include "Source/Magic/Timer.cpp"
#include "Source/Magic/Crypto.cpp"
#include "Source/Magic/Thread.cpp"
#include "Source/Magic/Config.cpp"
#include "Source/Magic/Plugin.cpp"
#include "Source/Magic/IoPool.cpp"
#include "Source/Magic/Socket.cpp"
#include "Source/Magic/TcpServer.cpp"
#include "Source/Magic/TimingWheel.cpp"
// DataBase
#include "Source/Magic/DB/MySql.cpp"
// Http
#include "Source/Magic/Http/Http.cpp"
#include "Source/Magic/Http/Session.cpp"
#include "Source/Magic/Http/HttpFile.cpp"
#include "Source/Magic/Http/MultiPart.cpp"
#include "Source/Magic/Http/HttpCache.cpp"
#include "Source/Magic/Http/HttpSocket.cpp"
#include "Source/Magic/Http/HttpServer.cpp"
#include "Source/Magic/Http/HttpParser.cpp"
#include "Source/Magic/Http/HttpServlet.cpp"
// WebSocket
#include "Source/Magic/Http/WebSocket.cpp"
#include "Source/Magic/Http/WebSocketMessage.cpp"
#include "Source/Magic/Http/WebSocketServer.cpp"
// Adapter
#include "Source/Magic/Adapter/WinUtil.cpp"
#include "Source/Magic/Adapter/WinMutex.cpp"
#include "Source/Magic/Adapter/WinPlugin.cpp"
#include "Source/Magic/Adapter/LinuxUtil.cpp"
#include "Source/Magic/Adapter/LinuxMutex.cpp"
#include "Source/Magic/Adapter/LinuxPlugin.cpp"
// Ragel
#include "Source/Magic/Http/Util/Uri.rl.cpp"
#include "Source/Magic/Http/Util/http11_parser.rl.cpp"
#include "Source/Magic/Http/Util/httpclient_parser.rl.cpp"
79 changes: 79 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
cmake_minimum_required(VERSION 3.0)
project(Magic)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(Library
Include
Include/Magic
ThirdParty/Gzip #Gzip
ThirdParty/RapidJSON/include #RapidJSON
ThirdParty/Asio/asio/include #ASIO
)
include(Library.cmake)
include_directories(${Library})

#GZip
add_subdirectory(ThirdParty/Gzip)

set(SRC
# Util
Source/Magic.cpp
Source/Magic/Log.cpp
Source/Magic/Gzip.cpp
Source/Magic/Timer.cpp
Source/Magic/Crypto.cpp
Source/Magic/Thread.cpp
Source/Magic/Config.cpp
Source/Magic/Plugin.cpp
Source/Magic/IoPool.cpp
Source/Magic/Socket.cpp
Source/Magic/TcpServer.cpp
Source/Magic/TimingWheel.cpp
# DataBase
Source/Magic/DB/MySql.cpp
# Http
Source/Magic/Http/Http.cpp
Source/Magic/Http/Session.cpp
Source/Magic/Http/HttpFile.cpp
Source/Magic/Http/MultiPart.cpp
Source/Magic/Http/HttpCache.cpp
Source/Magic/Http/HttpSocket.cpp
Source/Magic/Http/HttpServer.cpp
Source/Magic/Http/HttpParser.cpp
Source/Magic/Http/HttpServlet.cpp
Source/Magic/Http/WebSocketMessage.cpp
Source/Magic/Http/WebSocket.cpp
Source/Magic/Http/WebSocketServer.cpp
# Adapter
Source/Magic/Adapter/WinUtil.cpp
Source/Magic/Adapter/WinMutex.cpp
Source/Magic/Adapter/WinPlugin.cpp
Source/Magic/Adapter/LinuxUtil.cpp
Source/Magic/Adapter/LinuxMutex.cpp
Source/Magic/Adapter/LinuxPlugin.cpp
# Ragel
Source/Magic/Http/Util/Uri.rl.cpp
Source/Magic/Http/Util/http11_parser.rl.cpp
Source/Magic/Http/Util/httpclient_parser.rl.cpp
)

set(SRCS
Build.cpp
)

add_library(Magic ${SRC})
target_link_libraries(Magic zlib)

#Test
add_executable(Test Test/Test.cc)
target_link_libraries(Test Magic)

#Examples-Plugin
add_library(Test_PluginLib MODULE Examples/Test_PluginLib.cpp)
add_executable(Test_Plugin Examples/Test_Plugin.cpp)
target_link_libraries(Test_Plugin Magic)

#Examples-Server
add_executable(Test_Server Examples/Test_Server.cpp)
target_link_libraries(Test_Server Magic)
32 changes: 32 additions & 0 deletions Examples/Test_Plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* @File: Test_Plugin.cpp
* @Author: INotFound
* @Date: 2020-03-16 00:15:10
* @LastEditTime: 2020-03-16 00:25:49
*/
#include <iostream>
#include "Magic.h"
class IPluginModule {
public:
virtual int arg() = 0;
virtual ~IPluginModule() {};
};

void Plugin(){
auto config = Magic::Config::GetInstance();
Safe<Magic::Plugin> plugin{
// so库要使用绝对路径
new Magic::Plugin("TestLib", config->at<std::string>("Library","/home/magic/WorkSpace/Magic/build/libTest_PluginLib.so"))
};
auto& pluginMgr = Magic::PluginMgr::GetInstance();
pluginMgr->addPlugin(plugin);
Safe<IPluginModule> pp(pluginMgr->at("TestLib")->getInstance<IPluginModule>());
MAGIC_INFO() << pp->arg();
}

int main(){
Magic::Init("Test_Plugin");
Plugin();
getchar();
return 0;
}
40 changes: 40 additions & 0 deletions Examples/Test_PluginLib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @File: Test_PluginLib.cpp
* @Author: INotFound
* @Date: 2020-03-16 00:16:18
* @LastEditTime: 2020-03-16 00:16:31
*/
#include <memory>

class IPluginModule {
public:
virtual int arg() =0;
virtual ~IPluginModule() {}
};


class PluginModule:public IPluginModule {
public:
~PluginModule() {}
PluginModule() {}
int arg() override{
return 6666666;
}
};


#if defined(_WIN32) || defined(_WIN64)
//Win
# define DLL_API extern "C" __declspec(dllexport)
DLL_API void* create(){
return new PluginModule;
}
#endif

#if defined(linux) || defined(__linux__)
# define DLL_API extern "C"
//Linux
DLL_API void* create(){
return new PluginModule;
}
#endif
120 changes: 120 additions & 0 deletions Examples/Test_Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* @File: Test_Server.cpp
* @Author: INotFound
* @Date: 2020-03-16 00:11:04
* @LastEditTime: 2020-03-16 00:14:18
*/
#include <iostream>
#include "Magic.h"

class DeafultServlet :public Magic::Http::HttpServlet{
public:
DeafultServlet()
:HttpServlet("DeafultServlet"){
}
bool handle (const Safe<Magic::Http::HttpRequest>& request,const Safe<Magic::Http::HttpResponse>& response) override{
response->setStatus(Magic::Http::HttpStatus::NOT_FOUND);
std::string notfound{R"Template(<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>Magic/0.0.1</center>
</body>
</html>)Template"};
response->setBody(notfound);
return true;
}
};


class LogServlet :public Magic::Http::HttpServlet{
public:
LogServlet()
:HttpServlet("LogServlet"){
}
bool handle (const Safe<Magic::Http::HttpRequest>& request,const Safe<Magic::Http::HttpResponse>& response) override{
response->setStatus(Magic::Http::HttpStatus::OK);
std::fstream stream;
response->setHeader("Content-type","text/html");
stream.open("Test_Server.html",std::ios::in);
if(stream.is_open()){
std::stringstream sstream;
sstream << stream.rdbuf();
response->setBody(sstream.str());
}
return true;
}
};

class FileServlet :public Magic::Http::HttpServlet{
public:
FileServlet()
:HttpServlet("FileServlet"){
}
bool handle (const Safe<Magic::Http::HttpRequest>& request,const Safe<Magic::Http::HttpResponse>& response) override{
Magic::Http::MultiPart multiPart;
multiPart.parse(request);
MAGIC_DEBUG() << multiPart.getParams().count("xxxx");
auto fileIter = multiPart.getFiles().begin();
auto fileEnd = multiPart.getFiles().end();
for(;fileIter!=fileEnd; fileIter++){
(*fileIter)->save("www/Image/" + (*fileIter)->getName());
}
response->setStatus(Magic::Http::HttpStatus::OK);
response->setBody("OK!!!");
return true;
}
};

class MainServlet :public Magic::Http::HttpServlet{
public:
MainServlet()
:HttpServlet("MainServlet"){
}
bool handle (const Safe<Magic::Http::HttpRequest>& request,const Safe<Magic::Http::HttpResponse>& response) override{
response->setStatus(Magic::Http::HttpStatus::OK);
std::ifstream stream;
std::string res = "www";
std::string path = request->getPath();
if(path == "/"){
path = "/index.html";
}
stream.open(res + path,std::ios::in);
if(stream.is_open()){
std::ostringstream staticRes;
staticRes << stream.rdbuf();
response->setContentType(Magic::Http::FileTypeToHttpContentType(path));
response->setBody(staticRes.str());
return true;
}
return false;
}
};


void Server(){
std::string ip = Magic::Config::GetInstance()->at<std::string>("ServerIp","0.0.0.0");
uint16_t port = Magic::Config::GetInstance()->at<uint16_t>("ServerPort",8080);
uint16_t processorsNumber = Magic::Config::GetInstance()->at<uint16_t>("ServerProcessorsNumber",Magic::GetProcessorsNumber());
try{
Magic::Http::HttpServer server(ip,port,processorsNumber);
Safe<Magic::Http::HttpServlet> log(new LogServlet);
Safe<Magic::Http::HttpServlet> deafult(new DeafultServlet);
Safe<Magic::Http::HttpServlet> file(new FileServlet);
Safe<Magic::Http::HttpServlet> main(new MainServlet);
server.getHttpServletDispatch()->setDeafultServlet(deafult);
server.getHttpServletDispatch()->addHttpServlet("/log",log);
server.getHttpServletDispatch()->addHttpServlet("/file",file);
server.getHttpServletDispatch()->addGlobHttpServlet("^/?(.*)$",main);
Magic::Config::GetInstance()->update();
server.run();
}catch(std::system_error ec){
std::cout << ec.what() << std::endl;
}
}

int main(){
Magic::Init("Test_Server");
Server();
return 0;
}
Loading

0 comments on commit 804d8b3

Please sign in to comment.