forked from m-barthelemy/vpn-webauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (60 loc) · 1.98 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
//go:generate pkger
package main
import (
"log"
"strings"
"github.com/kelseyhightower/envconfig"
"github.com/m-barthelemy/vpn-webauth/models"
"github.com/m-barthelemy/vpn-webauth/routes"
services "github.com/m-barthelemy/vpn-webauth/services"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
var config models.Config
config = config.New()
err := envconfig.Process("", &config)
if err != nil {
log.Fatal(err.Error())
}
config.Verify()
var db *gorm.DB
var dbErr error
switch strings.ToLower(config.DbType) {
case "sqlite":
db, dbErr = gorm.Open(sqlite.Open(config.DbDSN), &gorm.Config{})
case "postgres":
db, dbErr = gorm.Open(postgres.Open(config.DbDSN), &gorm.Config{})
case "mysql":
db, dbErr = gorm.Open(mysql.Open(config.DbDSN), &gorm.Config{})
default:
log.Fatalf("Unknown DbType '%s'", config.DbType)
}
if dbErr != nil {
log.Fatalf("Failed to connect to database: %s", dbErr)
}
// Migrate the schema
if err := db.AutoMigrate(&models.User{}); err != nil {
log.Fatalf("Failed to run database migrations for User model: %s", err)
}
if err := db.AutoMigrate(&models.VpnSession{}); err != nil {
log.Fatalf("Failed to run database migrations for VpnSession model: %s", err)
}
if err := db.AutoMigrate(&models.UserMFA{}); err != nil {
log.Fatalf("Failed to run database migrations for UserMFA model: %s", err)
}
if err := db.AutoMigrate(&models.VPNConnection{}); err != nil {
log.Fatalf("Failed to run database migrations for VPNConnection model: %s", err)
}
if err := db.AutoMigrate(&models.UserSubscription{}); err != nil {
log.Fatalf("Failed to run database migrations for UserSubscription model: %s", err)
}
// Delete old VPN connections log entries
userManager := services.NewUserManager(db, &config)
if err := userManager.CleanupConnectionsLog(); err != nil {
log.Printf("Could not delete old VPN connections log entries: %s", err.Error())
}
startServer(&config, routes.New(&config, db))
}