-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (84 loc) · 2.11 KB
/
main.go
File metadata and controls
105 lines (84 loc) · 2.11 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"auth/database"
"auth/middleware"
"auth/routes"
"auth/utils"
"auth/utils/config"
"auth/utils/env"
"auth/utils/tokens"
"crypto/tls"
"embed"
"flag"
"net/http"
"os"
"codeberg.org/coldwire/cwhydra"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/proxy"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
//go:embed views/public/*
var views embed.FS
func init() {
// Configure logs
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
fileConf := flag.String("config", "", "Path to the config file")
flag.Parse()
// Init configuration
config.Init(env.Get("CONFIG_FILE", *fileConf))
// Connect to database
database.Connect()
// Connect to hydra admin api
cwhydra.Init(cwhydra.Config{
Url: config.Conf.Hydra.Admin,
Http: http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 0,
},
})
log.Print("load jwt secret")
// generate key for signing jwt tokens
tokens.Init(env.Get("JWT_KEY", ""))
}
func main() {
log.Print("init fiber")
// Create fiber instance
app := fiber.New()
log.Print("migrate tables")
// migrate database
utils.MigrateTables()
// Include cors
app.Use(cors.New())
log.Print("load routes")
// Setup routes
routes.Api(app)
if config.Conf.Hydra.Proxy == "true" {
routes.Hydra(app)
}
if env.Get("DEV_FRONT_URL", "") == "" {
// Load view as static website
app.Use("/", middleware.OnAuth, filesystem.New(filesystem.Config{
Root: http.FS(views),
PathPrefix: "views/public",
Browse: true,
}))
} else {
app.Get("/*", middleware.OnAuth, func(c *fiber.Ctx) error {
url := env.Get("DEV_FRONT_URL", "") + c.Params("*")
err := proxy.Do(c, url)
if err != nil {
return err
}
c.Response().Header.Del(fiber.HeaderServer)
return nil
})
}
log.Print(config.Conf.Server.Address + ":" + config.Conf.Server.Port)
//utils.InitClients()
log.Info().Err(app.Listen(config.Conf.Server.Address + ":" + config.Conf.Server.Port))
}