-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (67 loc) · 1.61 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
package main
import (
"embed"
"go-blog/src/handlers"
"html/template"
"log"
"net/http"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/template/html"
)
// Embed the static/ directory to the app to serve favicons, custom css
// and all other `static` stuff.
//go:embed static
var static embed.FS
// Embed the Template Library
//go:embed templates/*
var t embed.FS
//Embed Our Actual Content aka The Blogs.
//go:embed posts/*.md
var posts embed.FS
func main() {
PORT := "4000"
OSPORT := os.Getenv("PORT")
if OSPORT != "" {
PORT = OSPORT
} else {
log.Printf("PORT Env Variable not set, Using the default port: %s Instead", PORT)
}
// Setup a Template Engine
var engine *html.Engine
if os.Getenv("GO_ENV") == "production" {
engine = html.NewFileSystem(http.FS(t), ".html")
} else {
// For Dev Reasons
engine = html.New(".", ".html")
engine.Reload(true)
}
// We will be using this function to unescape the html which is coming from
// goldmark markdown parser.
engine.AddFunc(
"unescape", func(s string) template.HTML {
return template.HTML(s)
},
)
// Create a New Fiber App.
// TODO: Use the `http` package instead.
app := fiber.New(
fiber.Config{
Views: engine,
})
// Setup handlers for the blogs
handlers.SetupRoutes(app, posts)
// Mount the static directory
if os.Getenv("GO_ENV") == "production" {
app.Use("/", filesystem.New(
filesystem.Config{
Root: http.FS(static),
Browse: false,
}))
} else {
app.Static("/static", "static/")
}
// Start the fiber app woo-hoo.
log.Panic(app.Listen(":" + PORT))
}