-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
118 lines (101 loc) · 3.48 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
package main
import (
"html/template"
"net/http"
"os"
"github.com/Devansh3712/tsuki-go/internal"
socials "github.com/Devansh3712/tsuki-go/internal/auth"
"github.com/Devansh3712/tsuki-go/middleware"
"github.com/Devansh3712/tsuki-go/routes"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func index(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl.html", nil)
}
func notFound(c *gin.Context) {
c.HTML(http.StatusNotFound, "error.tmpl.html", gin.H{
"error": "404 Not Found",
"message": "The requested page was not found.",
})
}
func main() {
godotenv.Load(".env")
gin.SetMode(gin.ReleaseMode)
app := gin.Default()
app.RedirectTrailingSlash = true
app.HandleMethodNotAllowed = true
app.NoRoute(notFound)
app.Static("/static", "./static")
app.SetFuncMap(template.FuncMap{
"formatAsTitle": internal.FormatAsTitle,
"formatAsDate": internal.FormatAsDate,
})
app.LoadHTMLGlob("templates/*")
store := cookie.NewStore([]byte(os.Getenv("SECRET_KEY")))
app.Use(sessions.Sessions("tsuki", store))
app.Use(middleware.RecoveryMiddleware())
app.GET("/", index)
app.GET("/signup", routes.SignUp)
app.GET("/login", routes.Login)
app.GET("/logout", routes.Logout)
app.GET("/feed", middleware.AuthMiddleware(), routes.UserFeed)
app.GET("/feed/more", middleware.AuthMiddleware(), routes.LoadMoreFeed)
auth := app.Group("/auth")
{
auth.GET("/signup/discord", socials.DiscordSignUp)
auth.GET("/signup/github", socials.GitHubSignUp)
auth.GET("/signup/google", socials.GoogleSignUp)
auth.GET("/login/discord", socials.DiscordLogin)
auth.GET("/login/github", socials.GitHubLogin)
auth.GET("/login/google", socials.GoogleLogin)
auth.GET("/discord", socials.DiscordAuth)
auth.GET("/github", socials.GitHubAuth)
auth.GET("/google", socials.GoogleAuth)
auth.GET("/verify", middleware.AuthMiddleware(), routes.SendVerificationMail)
auth.GET("/verify/:id", routes.Verify)
auth.POST("/signup", routes.SignUp)
auth.POST("/login", routes.Login)
}
user := app.Group("/user")
user.GET("/:username", routes.GetUserByName)
user.GET("/:username/posts", routes.GetUserPosts)
user.GET("/:username/posts/more", routes.LoadMorePosts)
user.Use(middleware.AuthMiddleware())
{
user.GET("/", routes.GetUser)
user.GET("/settings/avatar", routes.UpdateAvatar)
user.GET("/settings/username", routes.UpdateUsername)
user.GET("/settings/password", routes.UpdatePassword)
user.GET("/settings/delete", routes.DeleteUser)
user.POST("/:username/toggle-follow", routes.ToggleFollow)
user.POST("/settings/avatar", routes.UpdateAvatar)
user.POST("/settings/username", routes.UpdateUsername)
user.POST("/settings/password", routes.UpdatePassword)
user.POST("/settings/delete", routes.DeleteUser)
}
search := app.Group("/search")
{
search.GET("/", routes.SearchUser)
search.GET("/more", routes.LoadMoreUsers)
search.POST("/", routes.SearchUser)
search.POST("/:username/toggle-follow", middleware.AuthMiddleware(), routes.ToggleSearchFollow)
}
post := app.Group("/post")
post.GET("/:id", routes.GetPost)
post.Use(middleware.AuthMiddleware())
{
post.GET("/", routes.NewPost)
post.GET("/:id/toggle-vote", routes.ToggleVote)
post.GET("/:id/delete", routes.DeletePost)
post.GET("/:id/comments", routes.LoadMoreComments)
post.GET("/:id/comment/delete", routes.DeleteComment)
post.POST("/", routes.NewPost)
post.POST("/:id/comment", routes.Comment)
}
if err := app.Run(); err != nil {
panic(err)
}
}