-
Notifications
You must be signed in to change notification settings - Fork 1
/
essen.go
87 lines (72 loc) · 2.04 KB
/
essen.go
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
/*
Package essen is a micro web framework for golang, It resembles express.js (node.js web framework) functions with nearly one to one mapping.
*/
package essen
import (
"log"
"net/http"
"strconv"
)
var essenGlobal Essen
//Essen is the root struct, It's instance provide the main entrypoint to access all other functions.
//
//Expressjs similarity
//
//let app = express();
type Essen struct {
//To share values between every request handler, Accessible in request handler.
//Checkout Request type for more.
//Lifetime: Until app is running
Locals map[string]interface{}
}
//Response type is a wrapper around native http.ResponseWriter.
//
//This type is used to access response functions built in essen.
type Response struct {
//Copy of native http.ResponseWriter instance.
Res http.ResponseWriter
ReqMethod string
}
//Request type is a wrapper around native http.Request.
//
//This type is used to access request functions built in essen.
//Essen Request Type
type Request struct {
//Reference to native http.Request instance.
Req *http.Request
//Copy of Essen.Locals.
//Properties set in Essen.Locals are accessed in request handlers through req.App.
App map[string]interface{}
//Map to pass values within middlewares.
//Lifetime: Until request is responded.
Locals map[string]interface{}
//Field to access request body parameters.
//
// val, err := req.Body.Params("key")
//
// if err.IsNil(){
// fmt.Pritln(val)
// }
Body param
//Unique id per request. (Used for request data cleanup)
Uid string
}
//App is an entrypoint to essen functions.
//
// app := essen.App()
func App() Essen {
e := Essen{Locals: make(map[string]interface{})}
essenGlobal = e
return e
}
//Listen function lets you lift HTTP server.
//
// app := essen.App()
// app.Listen(8080) //Will listen for requests on port 8080.
func (e Essen) Listen(port int) {
sport := strconv.Itoa(port)
fport := ":" + sport
println("Lifting On Port: " + sport)
//Register Request/Response Root Handler
log.Fatal(http.ListenAndServe(fport, http.HandlerFunc(rootHandler)))
}