-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (58 loc) · 1.47 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
package main
import (
"fmt"
"log"
"os"
"github.com/handikacatur/go-blog/database"
"github.com/handikacatur/go-blog/models"
"github.com/handikacatur/go-blog/router"
"github.com/joho/godotenv"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func initDB() {
var err error
database.DBConn, err = gorm.Open("sqlite3", "go_blog.db")
if err != nil {
panic("failed to connect database")
}
fmt.Println("Connection Opened to Database")
database.DBConn.AutoMigrate(&models.Post{})
database.DBConn.AutoMigrate(&models.User{})
fmt.Println("Database Migrated")
}
func main() {
// Create covers directory if not exist
_, err := os.Stat("./public/assets/img/covers")
if os.IsNotExist(err) {
os.Mkdir("./public/assets/img/covers", 0775)
}
// Instantiate html template engine
engine := html.New("./views", ".html")
// Load env vars
err = godotenv.Load()
if err != nil {
log.Fatalf("Can not load env variables")
}
app := fiber.New(fiber.Config{
Views: engine,
})
initDB()
app.Use(func(c *fiber.Ctx) error {
token := "Bearer " + c.Cookies("token")
c.Request().Header.Add("Authorization", token)
return c.Next()
})
// Serve static
app.Static("/", "./public")
// Instantiate routers
router.SetHome(app)
router.SetAuth(app)
router.SetPost(app)
app.Use(func(c *fiber.Ctx) error {
return c.Render("404", fiber.Map{})
})
log.Fatal(app.Listen(":" + os.Getenv("PORT")))
}