-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (79 loc) · 2.25 KB
/
main.go
File metadata and controls
88 lines (79 loc) · 2.25 KB
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
package main
import (
"embed"
"github.com/Penetration-Testing-Toolkit/ptt/internal/database"
"github.com/Penetration-Testing-Toolkit/ptt/internal/server"
"github.com/Penetration-Testing-Toolkit/ptt/internal/session"
"github.com/Penetration-Testing-Toolkit/ptt/shared"
"github.com/hashicorp/go-hclog"
"io/fs"
"os"
"strconv"
"strings"
"time"
)
//go:embed static
var embeddedFS embed.FS
func main() {
// Read environment variables to configure logging
json := strings.ToUpper(os.Getenv("JSON")) != "FALSE"
logLevel := shared.LoggerOptions.Level
levelStr := os.Getenv("LOG_LEVEL")
level, err := strconv.Atoi(levelStr)
if err == nil {
logLevel = hclog.Level(level)
}
// Create a hclog.Logger
// TODO: split log output to Stdout AND a log file. Bonus: hash the log file during server shutdown for integrity
l := hclog.New(&hclog.LoggerOptions{
Name: "ptt",
Level: logLevel,
Output: os.Stdout,
JSONFormat: json,
})
l.Debug("logger created", "level", logLevel, "json", json)
// Determine if files will be hosted from embedded in the binary or from the OS file system
var f fs.FS
env := os.Getenv("ENV")
devMode := strings.ToUpper(env) == "DEV"
if devMode {
// If dev environment, host static assets from the OS file system
f = os.DirFS("static")
l.Debug("hosting static files from file system")
} else {
// Else, host static assets embedded in Go binary
f, err = fs.Sub(embeddedFS, "static")
if err != nil {
l.Error("error finding 'static' directory in embedded filesystem", "error", err.Error())
panic(err)
}
l.Debug("hosting static files from embedded file system")
}
// Setup database
db, err := database.SetupDB(l)
if err != nil {
l.Error("error setting up database", "error", err.Error())
panic(err)
}
// Setup session manager
sessManager := session.NewSessionManager(
time.Minute, // GC every minute
3*time.Hour, // Expire idle after 3 hours
12*time.Hour, // Expire absolute after 12 hours
"session", // Cookie name
l,
)
// Read address from environment variable
address := os.Getenv("PTT_ADDR")
if address == "" {
address = ":8080"
}
server.Start(&server.Config{
Logger: l,
Static: f,
Address: address,
DB: db,
Sessions: sessManager,
DevMode: devMode,
})
}