-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (109 loc) · 3.12 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
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/NYTimes/gziphandler"
"github.com/aleksrutins/litelytics/api"
"github.com/aleksrutins/litelytics/auth"
"github.com/aleksrutins/litelytics/dbutil"
"github.com/aleksrutins/litelytics/util"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/encryptcookie"
"github.com/gofiber/template/html"
"github.com/profclems/go-dotenv"
vueglue "github.com/torenware/vite-go"
)
//go:embed "frontend"
var frontend embed.FS
var vueGlue *vueglue.VueGlue
func main() {
err := dotenv.LoadConfig()
if err != nil {
log.Printf(".env could not be loaded: %v\n", err)
}
var viteConfig *vueglue.ViteConfig
if util.IsProduction() {
fmt.Println("Running in production.")
frontendFS, err := fs.Sub(frontend, "frontend")
if err != nil {
log.Fatalf("Error opening embedded frontend FS: %v", err)
}
viteConfig = &vueglue.ViteConfig{
Environment: "production",
AssetsPath: "dist",
EntryPoint: "src/main.ts",
Platform: "svelte",
FS: frontendFS,
}
} else {
fmt.Println("Running in development.")
viteConfig = &vueglue.ViteConfig{
Environment: "development",
AssetsPath: "frontend",
EntryPoint: "src/main.ts",
Platform: "svelte",
FS: os.DirFS("frontend"),
}
}
vueGlue, err = vueglue.NewVueGlue(viteConfig)
if err != nil {
log.Fatalf("error initializing Vite: %v", err)
}
dbutil.Connect()
defer dbutil.Client.Close()
templates := html.New("./templates", ".html")
templates.AddFunc("headMeta", func(title string) map[string]interface{} {
return fiber.Map{"Title": title}
})
app := fiber.New(fiber.Config{
Views: templates,
EnableTrustedProxyCheck: util.IsProduction(),
})
app.Use(encryptcookie.New(encryptcookie.Config{
Key: dotenv.GetString("SECRET_KEY"),
Except: []string{"userEmail"},
}))
fileServer, err := vueGlue.FileServer()
if err != nil {
log.Fatalf("error creating static file server: %v", err)
}
app.Mount("/auth", auth.Routes)
app.Mount("/api", api.Routes)
app.Use("/api/track", cors.New(cors.Config{AllowMethods: "POST"}))
app.Use("/simpleclient.js", cors.New(cors.Config{AllowMethods: "GET"}))
app.Use(viteConfig.URLPrefix, util.WrapHandler(gziphandler.GzipHandler(http.HandlerFunc(fileServer.ServeHTTP))))
if !util.IsProduction() {
app.Get("/simpleclient.js", func(c *fiber.Ctx) error {
c.Response().Header.Set("Content-Type", "text/javascript")
c.SendFile("frontend/public/simpleclient.js")
return nil
})
} else {
app.Get("/simpleclient.js", func(c *fiber.Ctx) error {
c.Response().Header.Set("Content-Type", "text/javascript")
content, err := frontend.ReadFile("frontend/public/simpleclient.js")
if err != nil {
return err
}
c.Send(content)
return nil
})
}
app.Get("/*", func(c *fiber.Ctx) error {
if strings.HasPrefix(c.Path(), "/auth") || strings.HasPrefix(c.Path(), "/api") {
return c.Next()
}
return c.Render("embedVue", vueGlue)
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
app.Listen("0.0.0.0:" + port)
}