-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (65 loc) · 2.05 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/michaelcclary/blogsite/controller"
"github.com/michaelcclary/blogsite/middleware"
"github.com/michaelcclary/blogsite/model"
"github.com/michaelcclary/blogsite/model/database"
"log"
"os"
)
func main() {
loadEnv()
loadDatabase()
serveApplication()
}
func loadDatabase() {
database.Connect()
err := database.Database.AutoMigrate(&model.User{})
if err != nil {
log.Panicf("Auto Migrate User Failed:%v", err)
}
err = database.Database.AutoMigrate(&model.Entry{})
if err != nil {
log.Panicf("Auto Migrate Entry Failed:%v", err)
}
err = database.Database.AutoMigrate(&model.EntryType{})
if err != nil {
log.Panicf("Auto Migrate Entry Type Failed:%v", err)
}
}
func loadEnv() {
err := godotenv.Load(".env.local")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
}
func serveApplication() {
router := gin.Default()
router.LoadHTMLGlob("templates/**/*")
router.Static("/static", "./static/")
publicRoutes := router.Group("/auth")
publicRoutes.POST("/register", controller.Register)
publicRoutes.POST("/login", controller.Login)
router.POST("/blogEntry", controller.AddBlogEntry)
router.GET("/blogEntry", controller.ManageBlogEntry)
router.GET("/blogEntry/:blogId", controller.ManageBlogEntry)
router.GET("/", controller.HomePage)
protectedRoutes := router.Group("/api")
protectedRoutes.Use(middleware.JWTAuthMiddleware())
protectedRoutes.POST("/entry", controller.AddEntry)
protectedRoutes.GET("/entries", controller.GetAllEntries)
protectedRoutes.POST("/entryType", controller.CreateEntryType)
protectedRoutes.PATCH("/entryType/:id", controller.UpdateEntryType)
protectedRoutes.DELETE("/entryType/:id", controller.DeleteEntrytype)
protectedRoutes.GET("/entryType/:id", controller.GetEntryTypeByID)
protectedRoutes.GET("/entryType", controller.GetAllEntryTypes)
port := os.Getenv("PORT")
err := router.Run(":" + port)
if err != nil {
log.Panicf("Auto Migrate Entry Failed:%v", err)
}
fmt.Printf("Server running on port %v", port)
}