Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit dc37ef4

Browse files
committed
First commit
0 parents  commit dc37ef4

10 files changed

+423
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.pub/*
2+
pubspec.lock
3+
.packages

lib/src/config.dart

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
library web.config;
2+
import 'dart:async' show Future;
3+
import "dart:convert" show JSON;
4+
import "package:yaml/yaml.dart" show loadYaml;
5+
import "dart:io" show File;
6+
7+
class Config {
8+
Map<String, dynamic> rawMap;
9+
10+
Config(this.rawMap);
11+
12+
factory Config.json(String s){
13+
var conf = new Config({});
14+
conf.getFromJson(s);
15+
return conf;
16+
}
17+
18+
factory Config.yaml(String s){
19+
var conf = new Config({});
20+
conf.getFromYaml(s);
21+
return conf;
22+
}
23+
24+
factory Config.file(File f){
25+
var conf = new Config({});
26+
if (f.uri.toFilePath().endsWith(".json")) {
27+
conf.getFromJsonFile(f);
28+
} else {
29+
conf.getFromYamlFile(f);
30+
}
31+
return conf;
32+
}
33+
34+
dynamic operator [](String key) => rawMap[key];
35+
operator []=(String key, var value) => rawMap[key] = value;
36+
37+
void getFromYaml(String s){
38+
var r = loadYaml(s);
39+
rawMap.addAll(r);
40+
}
41+
42+
void getFromJson(String s){
43+
var r = JSON.decode(s);
44+
rawMap.addAll(r);
45+
}
46+
47+
Future getFromJsonFile(File f) async{
48+
getFromJson(await f.readAsString());
49+
}
50+
51+
Future getFromYamlFile(File f) async{
52+
getFromYaml(await f.readAsString());
53+
}
54+
}

lib/src/context.dart

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
library web.context;
2+
import "./request.dart" show Request;
3+
4+
5+
dynamic value2type(String value){
6+
switch (value) {
7+
case "true":
8+
return true;
9+
break;
10+
case "false":
11+
return value;
12+
break;
13+
}
14+
15+
var number;
16+
try{
17+
number = num.parse(value);
18+
} on FormatException {}
19+
20+
if (number){
21+
return number;
22+
}
23+
24+
return value;
25+
}
26+
27+
28+
class Context{
29+
Request request;
30+
Map<String, Function> contextCreators;
31+
32+
Context(this.request);
33+
34+
dynamic call(String s){
35+
return contextCreators[s](request);
36+
}
37+
38+
void register(String s, Function f) => contextCreators[s] = f;
39+
}

lib/src/layer.dart

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
library web.layer;
2+
import "dart:async" show Future;
3+
4+
abstract class Layer {
5+
/// This class is base of all of Layers
6+
/// [Layer.apply] must be async
7+
void apply(List args, [Map<Symbol, dynamic> namedArgs]);
8+
9+
factory Layer(Function f){
10+
/// Return a [FunctionalLayer]
11+
return new FunctionalLayer(f);
12+
}
13+
}
14+
15+
16+
class FunctionalLayer implements Layer {
17+
Function function;
18+
19+
FunctionalLayer(this.function);
20+
21+
Future run(List args, [Map<Symbol, dynamic> namedArgs]) async{
22+
if (args == null) {
23+
args = [];
24+
}
25+
if (namedArgs != null) {
26+
await Function.apply(function, args, namedArgs);
27+
} else {
28+
await Function.apply(function, args);
29+
}
30+
}
31+
32+
Future apply(List args, [Map<Symbol, dynamic> namedArgs]) async{
33+
await this.run(args,namedArgs);
34+
}
35+
36+
}
37+
38+
39+
class LayerManager {
40+
LayerChain chain;
41+
42+
LayerManager(){
43+
chain = new LayerChain.empty();
44+
}
45+
46+
LayerState createState() => chain.newState;
47+
LayerState get newState => createState();
48+
}
49+
50+
51+
class LayerChain {
52+
List<Layer> list;
53+
Map<String, dynamic> global;
54+
55+
LayerChain(this.list,[Map<String, dynamic> map]){
56+
if (map == null) {
57+
global = <String, dynamic>{};
58+
}
59+
}
60+
61+
factory LayerChain.empty(){
62+
return new LayerChain(<Layer>[]);
63+
}
64+
65+
void add(Layer layer) => list.add(layer);
66+
67+
void addAll(Iterable<Layer> iterable) => list.addAll(iterable);
68+
69+
void insert(int index, Layer layer) => list.insert(index, layer);
70+
71+
void insertAll(int startIndex, Iterable<Layer> iter) => list.insertAll(
72+
startIndex, iter
73+
);
74+
75+
LayerState createState() => new LayerState(this);
76+
LayerState get newState => createState();
77+
78+
}
79+
80+
81+
typedef void LayerForEachHandler(Layer layer);
82+
83+
84+
class LayerState {
85+
LayerChain pchain;
86+
int _rawPointer;
87+
Map<String, dynamic> memories;
88+
89+
LayerState(this.pchain){
90+
this.memories.addAll(this.pchain.global);
91+
}
92+
93+
Future start(List args, [Map<Symbol, dynamic> namedArgs]) async{
94+
forEach((Layer layer) async{
95+
await layer.apply(args,namedArgs);
96+
});
97+
}
98+
99+
Layer next(){
100+
_rawPointer ++;
101+
if ((rawPointer + 1) > pchain.list.length){
102+
return null;
103+
}
104+
return pointer;
105+
}
106+
107+
Future forEach(LayerForEachHandler h) async{
108+
while (true) {
109+
var layer = next();
110+
if (layer){
111+
await h(layer);
112+
} else {
113+
break;
114+
}
115+
}
116+
}
117+
118+
Layer get pointer => pchain.list[rawPointer];
119+
120+
int get rawPointer => _rawPointer;
121+
}

lib/src/logging.dart

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
library web.logging;
2+
import "package:logging/logging.dart";
3+
import "package:bwu_log/bwu_log.dart";
4+
import "./layer.dart" show Layer;
5+
import "./request.dart" show Request;
6+
7+
class SimpleStringFormatter implements FormatterBase<String> {
8+
String call(LogRecord r) => """[${r.loggerName}][${r.time.toIso8601String()}][${r.level.toString()}]{
9+
${r.message}
10+
${r.error}
11+
${r.stackTrace}
12+
}
13+
""";
14+
}
15+
16+
17+
final InMemoryListAppender appender = new InMemoryListAppender(new SimpleStringFormatter());
18+
19+
20+
Logger getLogger(String name){
21+
var logger = new Logger(name);
22+
appender.attachLogger(logger);
23+
return logger;
24+
}
25+
26+
27+
final HandlerLogger = getLogger("handler");
28+
29+
30+
final Layer LoggingLayer = new Layer((Request req){
31+
HandlerLogger.info("${req.method} ${req.path}");
32+
});

lib/src/plugin.dart

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library web.plugin;
2+
import "./web.dart" show Application;
3+
4+
abstract class Plugin{
5+
void init(Application app);
6+
}

lib/src/request.dart

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
library web.request;
2+
import "dart:convert" show JSON;
3+
import "package:shelf/shelf.dart" as shelf;
4+
import "./layer.dart" show LayerState;
5+
import "./context.dart" show Context;
6+
import "./web.dart" show Application;
7+
import "./config.dart" show Config;
8+
import "dart:async" show Future;
9+
10+
class Request{
11+
shelf.Request _raw;
12+
Response _res;
13+
LayerState _state;
14+
Application _app;
15+
Context _context;
16+
17+
Request(this._raw,this._state,this._app){
18+
_res = new Response(this);
19+
_context = new Context(this);
20+
}
21+
22+
shelf.Request get raw => _raw;
23+
24+
LayerState get state => _state;
25+
26+
Application get app => _app;
27+
28+
Context get context => _context;
29+
30+
Map<String, String> get headers => raw.headers;
31+
32+
String get method => raw.method;
33+
34+
String get mimeType => raw.mimeType;
35+
36+
Uri get url => raw.url;
37+
38+
Future<String> get body => raw.readAsString();
39+
40+
dynamic asJson() async => JSON.decode(await body);
41+
42+
Response get response => _res;
43+
Response get res => response;
44+
45+
Config get C => app.C;
46+
47+
get path => raw.handlerPath;
48+
}
49+
50+
51+
class Response{
52+
String body;
53+
Request request;
54+
int statusCode;
55+
Map<String, String> headers;
56+
57+
Response(this.request);
58+
59+
shelf.Response done(){
60+
return new shelf.Response(statusCode, body: body,headers: headers);
61+
}
62+
63+
void ok(var body){
64+
statusCode = 200;
65+
this.body = preprocessBody(body);
66+
}
67+
68+
void forbidden([var body]) => error(403,body);
69+
70+
void notFound([var body]) => error(404,body);
71+
72+
void error(int code, [var body]){
73+
statusCode = code;
74+
getTargetPage(body);
75+
}
76+
77+
Future getTargetPage([var body]) async{
78+
if (body == null){
79+
this.body = await request.app.getErrorPage(statusCode);
80+
} else {
81+
this.body = preprocessBody(body);
82+
}
83+
}
84+
85+
static dynamic preprocessBody(var body){
86+
if (body is String){
87+
return body;
88+
} else {
89+
return JSON.encode(body);
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)