-
Notifications
You must be signed in to change notification settings - Fork 7
/
effe.go
53 lines (49 loc) · 1.17 KB
/
effe.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
package main
import (
"flag"
"fmt"
"github.com/siscia/effe/logic"
"log/syslog"
"net/http"
"sync"
)
type complexContext struct {
ctx logic.Context
err error
}
func generateHandler(pool *sync.Pool, logger *syslog.Writer) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := pool.Get().(complexContext)
defer func() {
if r := recover(); r != nil {
w.WriteHeader(http.StatusInternalServerError)
logger.Crit("Logic Panicked")
}
}()
err := logic.Run(ctx.ctx, ctx.err, w, r)
if err != nil {
logger.Debug(err.Error())
}
if ctx.err == nil {
pool.Put(ctx)
}
}
}
func main() {
port := flag.Int("port", 8080, "Port where serve the effe.")
info := flag.Bool("info", false, "Print the effe information, then exit.")
flag.Parse()
if *info {
fmt.Println(logic.Info)
return
}
url := fmt.Sprintf(":%d", *port)
logic.Init()
logger, _ := syslog.New(syslog.LOG_ERR|syslog.LOG_USER, "Logs From Effe ")
var ctxPool = &sync.Pool{New: func() interface{} {
ctx, err := logic.Start()
return complexContext{ctx, err}
}}
http.HandleFunc("/", generateHandler(ctxPool, logger))
http.ListenAndServe(url, nil)
}