-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
119 lines (107 loc) · 3.1 KB
/
main.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
// Copyright (C) Cyrill@Schumacher.fm @SchumacherFM Twitter/GitHub
// Wanderlust - a cache warmer for your web app with priorities
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package picnic
import (
log "github.com/SchumacherFM/wanderlust/github.com/segmentio/go-log"
"github.com/SchumacherFM/wanderlust/helpers"
. "github.com/SchumacherFM/wanderlust/picnic/api"
"github.com/SchumacherFM/wanderlust/rucksack/rucksackdb"
"net/http"
"sync"
)
const (
PEM_CERT string = "cert.pem"
PEM_KEY string = "key.pem"
RD_DIST_DIR string = "responsive-dashboard/dist/"
DEFAULT_TLS_SESSION_CACHE_CAPACITY int = 128
)
var (
rsdb rucksackdb.RDBIF
logger *log.Logger
)
type PicnicApp struct {
ListenAddress string
PemDir string
session SessionManagerIf
certFile string
keyFile string
httpRunning sync.Once
}
// la = listen address, pd = pemDir, lo = logger
func NewPicnicApp(la, pd string, lo *log.Logger, theDb rucksackdb.RDBIF) (*PicnicApp, error) {
var err error
rsdb = theDb
logger = lo
pa := &PicnicApp{
ListenAddress: la,
PemDir: pd,
}
pa.certFile, pa.keyFile, err = pa.generatePems()
if nil != err {
return nil, err
}
pa.session, err = newSessionManager(pa.certFile, pa.keyFile)
if nil != err {
return nil, err
}
err = initUsers(rsdb)
if nil != err {
return nil, err
}
return pa, nil
}
func (p *PicnicApp) GetSessionManager() SessionManagerIf {
return p.session
}
func (p *PicnicApp) getServer() *http.Server {
s := &http.Server{
Addr: p.GetListenAddress(),
Handler: p.getHandler(),
TLSConfig: helpers.GetTlsConfig(),
}
return s
}
func (p *PicnicApp) getPemDir() string {
return p.PemDir
}
func (p *PicnicApp) generatePems() (certFile, keyFile string, err error) {
// PemDir can be empty then it will generate a random one
dir, err := helpers.GeneratePems(p.GetListenAddress(), p.getPemDir(), PEM_CERT, PEM_KEY)
if nil != err {
return "", "", err
}
if "" != dir {
logger.Notice("PEM certificate temp directory is %s", dir)
}
p.PemDir = dir
certFile = dir + PEM_CERT
keyFile = dir + PEM_KEY
return
}
// make sure to execute only once
func (p *PicnicApp) Execute() error {
p.httpRunning.Do(func() {
logger.Check(p.getServer().ListenAndServeTLS(p.certFile, p.keyFile))
})
return nil
}
func (p *PicnicApp) GetListenAddress() string {
address, port, err := helpers.ValidateListenAddress(p.ListenAddress)
if nil != err {
logger.Check(err)
}
return address + ":" + port
}