Thread Class Routing C++ WebFramework (2 answers)
Opened by rosti at 2023-12-17 13:30

rosti
 2023-12-17 13:30
#195694 #195694
User since
2011-03-19
3219 Artikel
BenutzerIn
[Homepage]
user image
Moin;

vorab:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstring>
#include <map>
#include <stdlib.h>
#include <sstream>

using namespace std;

// Routing Table
// Wird später zur Laufzeit aus der Konfiguration geladen
map <string, string> routes;
map <string, string> ::iterator el;

// Basis Responseklasse
class Response{
private: stringstream body_stream;
public:string body(){
body_stream.str("Das ist der Body der Baisklasse");
return body_stream.str();
};

public: stringstream header_stream;
public:void set_header(const string header){
header_stream << header << "\n";
};
public: string get_header(){
header_stream << "\n\n";
return header_stream.str();
}
};

class Foo : public Response{
private: stringstream body_stream;
public:string body(){
body_stream.str("Das ist der Body der Klasse Foo");
return body_stream.str();
};
public: string header(){
this->set_header("Content-Type: text/plain");
return this->get_header();
};
};

class Bar : public Response{
private: stringstream body_stream;
public:string body(){
body_stream.str("Das ist der Body der Klasse Bar");
return body_stream.str();
};
public: string header(){
this->set_header("Content-Type: text/plain; charset=UTF-8");
return this->get_header();
};
};

class NotFound : public Response{
private: stringstream body_stream;
public:string body(){
body_stream.str("Die Seite wurde nicht gefunden");
return body_stream.str();
};
public: string header(){
this->set_header("Status: 404");
this->set_header("Content-Type: text/html; charset=utf-8");
return this->get_header();
}
};


int main(){
string ns = "Foo";

string url = getenv("REDIRECT_URL") ? getenv("REDIRECT_URL") : "/qwertz.chtml";
// Diese Konfiguration wird später zur Laufzeit geladen
routes["/foo.chtml"] = "Foo";
routes["/bar.chtml"] = "Bar";
string classname = routes[url];

if( ! classname.compare("Foo") ){
Foo *response = new Foo;
cout << response->header();
cout << response->body();
}
else if( ! classname.compare("Bar") ){
Bar *response = new Bar;
cout << response->header();
cout << response->body();
}
else{
NotFound *response = new NotFound;
cout << response->header();
cout << response->body();
}

return 0;
}


Von Interesse ist die Kontrollstruktur in der main. Sie ist unschön weil viele Zeilen redundant sind. Besser wäre sowas

Code: (dl )
1
2
3
     classname *response = new classname; // classname variabel
cout << response->header();
cout << response->body();


womit man die entsprechenden Methoden nur noch einmal aufrufen muss. Nur c++ will classname als Barword. Gehts vielleicht doch ?

MFG und schönen Sonntag ;)

View full thread Class Routing C++ WebFramework