Erste Schritte für Server
This commit is contained in:
parent
6bb99d58b6
commit
271c75c2e8
0
Client/client/main.ts
Normal file
0
Client/client/main.ts
Normal file
13
Client/tsconfig.json
Normal file
13
Client/tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noImplicitAny": false,
|
||||
"noEmitOnError": true,
|
||||
"removeComments": false,
|
||||
"sourceMap": false,
|
||||
"target": "ES5",
|
||||
"outDir": "../html/scripts"
|
||||
},
|
||||
"include": [
|
||||
"client/**/*"
|
||||
]
|
||||
}
|
61
Server/scripts/FileHandler.js
Normal file
61
Server/scripts/FileHandler.js
Normal file
@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.file_get = exports.file_exists = exports.file_allowed = exports.File = void 0;
|
||||
var node_fs_1 = require("node:fs");
|
||||
var Webserver_1 = require("./Webserver");
|
||||
var Mapping = /** @class */ (function () {
|
||||
function Mapping() {
|
||||
}
|
||||
return Mapping;
|
||||
}());
|
||||
var mappings = [
|
||||
{ ending: 'html', encoding: 'text/html' },
|
||||
{ ending: 'js', encoding: 'text/javascript' },
|
||||
{ ending: 'css', encoding: 'text/css' },
|
||||
{ ending: 'json', encoding: 'application/json' },
|
||||
{ ending: 'ico', encoding: 'image/x-icon' },
|
||||
{ ending: 'svg', encoding: 'image/svg+xml' },
|
||||
{ ending: 'txt', encoding: 'text/plain' },
|
||||
];
|
||||
var File = /** @class */ (function () {
|
||||
function File(type, content) {
|
||||
this.content = content;
|
||||
this.type = type;
|
||||
}
|
||||
return File;
|
||||
}());
|
||||
exports.File = File;
|
||||
function file_allowed(url) {
|
||||
var allowed = false;
|
||||
var m = url.match('\\.\\.');
|
||||
if (m == null) {
|
||||
allowed = true;
|
||||
}
|
||||
return allowed;
|
||||
}
|
||||
exports.file_allowed = file_allowed;
|
||||
function file_exists(url) {
|
||||
if (url == '/') {
|
||||
url = Webserver_1.index_alias;
|
||||
}
|
||||
return (0, node_fs_1.existsSync)(Webserver_1.www_folder + url);
|
||||
}
|
||||
exports.file_exists = file_exists;
|
||||
function file_get(url) {
|
||||
if (url == '/') {
|
||||
url = Webserver_1.index_alias;
|
||||
}
|
||||
return new File(get_type(url), (0, node_fs_1.readFileSync)(Webserver_1.www_folder + url));
|
||||
}
|
||||
exports.file_get = file_get;
|
||||
function get_type(url) {
|
||||
if (url == '/') {
|
||||
url = Webserver_1.index_alias;
|
||||
}
|
||||
var type = 'application/octet-stream';
|
||||
var m = mappings.find(function (e) { return e.ending == url.split('.').pop(); });
|
||||
if (m != undefined) {
|
||||
type = m.encoding;
|
||||
}
|
||||
return type;
|
||||
}
|
1
Server/scripts/FileHandler.js.map
Normal file
1
Server/scripts/FileHandler.js.map
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"FileHandler.js","sourceRoot":"","sources":["../server/FileHandler.ts"],"names":[],"mappings":";;;AAAA,mCAAoC;AAEpC,IAAM,UAAU,GAAW,iDAAiD,CAAA;AAC5E,IAAM,WAAW,GAAW,YAAY,CAAA;AAExC,SAAgB,YAAY,CAAC,GAAW;IACpC,IAAI,OAAO,GAAY,KAAK,CAAC;IAC7B,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;QAC1B,OAAO,GAAG,IAAI,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,CAAC;AACnB,CAAC;AAND,oCAMC;AAED,SAAgB,WAAW,CAAC,GAAW;IACnC,OAAO,IAAA,oBAAU,EAAC,UAAU,GAAG,GAAG,CAAC,CAAC;AACxC,CAAC;AAFD,kCAEC"}
|
23
Server/scripts/RequestHandler.js
Normal file
23
Server/scripts/RequestHandler.js
Normal file
@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.new_request = void 0;
|
||||
var FileHandler_1 = require("./FileHandler");
|
||||
function new_request(req, res) {
|
||||
if ((0, FileHandler_1.file_allowed)(req.url)) {
|
||||
if ((0, FileHandler_1.file_exists)(req.url)) {
|
||||
res.statusCode = 200;
|
||||
var f = (0, FileHandler_1.file_get)(req.url);
|
||||
res.setHeader('Content-Type', f.type);
|
||||
res.end(f.content);
|
||||
}
|
||||
else {
|
||||
res.statusCode = 404;
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.statusCode = 403;
|
||||
}
|
||||
console.log(req.url, res.statusCode);
|
||||
res.end();
|
||||
}
|
||||
exports.new_request = new_request;
|
16
Server/scripts/Webserver.js
Normal file
16
Server/scripts/Webserver.js
Normal file
@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.start_server = exports.index_alias = exports.www_folder = void 0;
|
||||
var node_http_1 = require("node:http");
|
||||
var RequestHandler_1 = require("./RequestHandler");
|
||||
exports.www_folder = '/home/carsten/Dokumente/SmartHomeDashboard/html/';
|
||||
exports.index_alias = 'index.html';
|
||||
var port = 8080;
|
||||
var hostname = '127.0.0.1';
|
||||
function start_server() {
|
||||
var server = (0, node_http_1.createServer)(RequestHandler_1.new_request);
|
||||
server.listen(port, hostname, function () {
|
||||
console.log('Server running at http://' + hostname + ':' + port);
|
||||
});
|
||||
}
|
||||
exports.start_server = start_server;
|
1
Server/scripts/Webserver.js.map
Normal file
1
Server/scripts/Webserver.js.map
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"Webserver.js","sourceRoot":"","sources":["../server/Webserver.ts"],"names":[],"mappings":""}
|
5
Server/scripts/main.js
Normal file
5
Server/scripts/main.js
Normal file
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Webserver_1 = require("./Webserver");
|
||||
(0, Webserver_1.start_server)();
|
||||
console.log("Server started");
|
1
Server/scripts/main.js.map
Normal file
1
Server/scripts/main.js.map
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"main.js","sourceRoot":"","sources":["../server/main.ts"],"names":[],"mappings":""}
|
62
Server/server/FileHandler.ts
Normal file
62
Server/server/FileHandler.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { www_folder, index_alias } from './Webserver';
|
||||
|
||||
class Mapping {
|
||||
ending: string;
|
||||
encoding: string;
|
||||
}
|
||||
|
||||
const mappings: Mapping[] = [
|
||||
{ ending: 'html', encoding: 'text/html' },
|
||||
{ ending: 'js', encoding: 'text/javascript' },
|
||||
{ ending: 'css', encoding: 'text/css' },
|
||||
{ ending: 'json', encoding: 'application/json' },
|
||||
{ ending: 'ico', encoding: 'image/x-icon' },
|
||||
{ ending: 'svg', encoding: 'image/svg+xml' },
|
||||
{ ending: 'txt', encoding: 'text/plain' },
|
||||
]
|
||||
|
||||
export class File {
|
||||
type: string;
|
||||
content: Buffer;
|
||||
|
||||
constructor(type: string, content: Buffer) {
|
||||
this.content = content;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
export function file_allowed(url: string): boolean {
|
||||
let allowed: boolean = false;
|
||||
let m = url.match('\\.\\.');
|
||||
if (m == null) {
|
||||
allowed = true;
|
||||
}
|
||||
return allowed;
|
||||
}
|
||||
|
||||
export function file_exists(url: string): boolean {
|
||||
if(url == '/') {
|
||||
url = index_alias;
|
||||
}
|
||||
return existsSync(www_folder + url);
|
||||
}
|
||||
|
||||
export function file_get(url: string): File {
|
||||
if(url == '/') {
|
||||
url = index_alias;
|
||||
}
|
||||
return new File(get_type(url), readFileSync(www_folder + url));
|
||||
}
|
||||
|
||||
function get_type(url: string): string {
|
||||
if(url == '/') {
|
||||
url = index_alias;
|
||||
}
|
||||
let type: string = 'application/octet-stream';
|
||||
let m: Mapping = mappings.find((e) => e.ending == url.split('.').pop());
|
||||
if(m != undefined) {
|
||||
type = m.encoding;
|
||||
}
|
||||
return type;
|
||||
}
|
19
Server/server/RequestHandler.ts
Normal file
19
Server/server/RequestHandler.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { IncomingMessage, ServerResponse } from "node:http";
|
||||
import { file_allowed, file_exists, file_get, File } from "./FileHandler";
|
||||
|
||||
export function new_request(req: IncomingMessage, res: ServerResponse): void {
|
||||
if(file_allowed(req.url)) {
|
||||
if(file_exists(req.url)) {
|
||||
res.statusCode = 200;
|
||||
let f: File = file_get(req.url)
|
||||
res.setHeader('Content-Type', f.type);
|
||||
res.end(f.content);
|
||||
} else {
|
||||
res.statusCode = 404;
|
||||
}
|
||||
} else {
|
||||
res.statusCode = 403;
|
||||
}
|
||||
console.log(req.url, res.statusCode);
|
||||
res.end();
|
||||
}
|
18
Server/server/Webserver.ts
Normal file
18
Server/server/Webserver.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { createServer } from 'node:http'
|
||||
import { new_request } from './RequestHandler';
|
||||
|
||||
|
||||
export const www_folder: string = '/home/carsten/Dokumente/SmartHomeDashboard/html/';
|
||||
export const index_alias: string = 'index.html';
|
||||
const port: number = 8080;
|
||||
const hostname: string = '127.0.0.1';
|
||||
|
||||
|
||||
export function start_server(): void {
|
||||
const server = createServer(new_request);
|
||||
|
||||
server.listen(port, hostname, () => {
|
||||
console.log('Server running at http://' + hostname + ':' + port);
|
||||
});
|
||||
}
|
||||
|
4
Server/server/main.ts
Normal file
4
Server/server/main.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { start_server } from "./Webserver";
|
||||
|
||||
start_server();
|
||||
console.log("Server started");
|
13
Server/tsconfig.json
Normal file
13
Server/tsconfig.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noImplicitAny": false,
|
||||
"noEmitOnError": true,
|
||||
"removeComments": false,
|
||||
"sourceMap": false,
|
||||
"target": "ES5",
|
||||
"outDir": "scripts"
|
||||
},
|
||||
"include": [
|
||||
"server/**/*"
|
||||
]
|
||||
}
|
5
html/index.html
Normal file
5
html/index.html
Normal file
@ -0,0 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<script src="/scripts/main.js"></script>
|
||||
Hat geklappt
|
||||
</html>
|
0
html/scripts/main.js
Normal file
0
html/scripts/main.js
Normal file
Loading…
x
Reference in New Issue
Block a user