-
Notifications
You must be signed in to change notification settings - Fork 0
/
varname.go
126 lines (89 loc) · 1.99 KB
/
varname.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path"
"runtime/debug"
"sync"
"varname/app/models"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
_ "github.com/mattn/go-sqlite3"
)
type Config struct {
Theme string
PortalType string
PortalAddr string
ResultsPerPage int
}
type VarName struct {
root string
appName string
config Config
db *sql.DB
}
func (self *VarName) Init(root, appName string) (err error) {
self.root = root
self.appName = appName
err = self.initConfig()
if err != nil {
return err
}
err = self.initDB()
return
}
func (self *VarName) initConfig() (err error) {
configFile := path.Join(self.root, self.appName+".conf")
configData, err := ioutil.ReadFile(configFile)
if err != nil {
return
}
err = json.Unmarshal(configData, &self.config)
return
}
func (self *VarName) initDB() (err error) {
err = models.InitDB("sqlite3", path.Join(self.root, "app/data", self.appName+".db"))
return
}
func (self *VarName) Run() {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
self.runPortal()
}()
go func() {
defer wg.Done()
self.timeWork()
}()
wg.Wait()
}
func (self *VarName) timeWork() {
}
func check(err error) {
if err != nil {
log.Println("[ERRO]", err)
log.Println("[DEBU]", string(debug.Stack()[:]))
panic(err)
}
}
type handlerFunc func(*http.Request, martini.Params, map[string]interface{})
type handlerWrapFunc func(*http.Request, martini.Params, render.Render)
func martiniSafeHandler(layout string, hf handlerFunc) handlerWrapFunc {
return func(req *http.Request, params martini.Params, r render.Render) {
data := make(map[string]interface{})
defer func() {
if err, ok := recover().(error); ok {
data["Status"] = "500"
data["ErrMsg"] = err.Error()
r.HTML(500, layout, data)
}
}()
log.Println("[INFO]", "request from", req.RemoteAddr, req.URL)
hf(req, params, data)
r.HTML(200, layout, data)
}
}