github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/web/web.go (about) 1 package web 2 3 import ( 4 "path" 5 6 "github.com/ngocphuongnb/tetua/app/asset" 7 "github.com/ngocphuongnb/tetua/app/auth" 8 "github.com/ngocphuongnb/tetua/app/config" 9 "github.com/ngocphuongnb/tetua/app/entities" 10 "github.com/ngocphuongnb/tetua/app/middlewares" 11 "github.com/ngocphuongnb/tetua/app/server" 12 webcomment "github.com/ngocphuongnb/tetua/app/web/comment" 13 "github.com/ngocphuongnb/tetua/app/web/manage" 14 webpost "github.com/ngocphuongnb/tetua/app/web/post" 15 websetting "github.com/ngocphuongnb/tetua/app/web/setting" 16 websitemap "github.com/ngocphuongnb/tetua/app/web/sitemap" 17 webuser "github.com/ngocphuongnb/tetua/app/web/user" 18 fiber "github.com/ngocphuongnb/tetua/packages/fiberserver" 19 ) 20 21 type Config struct { 22 JwtSigningKey string 23 Theme string 24 } 25 26 var ( 27 authPostCompose = auth.Config(&server.AuthConfig{ 28 Action: "post.compose", 29 DefaultValue: entities.PERM_OWN, 30 Prepare: auth.GetPost, 31 OwnCheckFN: auth.PostOwnerCheck, 32 }) 33 34 authPostSave = auth.Config(&server.AuthConfig{ 35 Action: "post.save", 36 DefaultValue: entities.PERM_OWN, 37 Prepare: auth.GetPost, 38 OwnCheckFN: auth.PostOwnerCheck, 39 }) 40 41 authPostDelete = auth.Config(&server.AuthConfig{ 42 Action: "post.delete", 43 DefaultValue: entities.PERM_OWN, 44 Prepare: auth.GetPost, 45 OwnCheckFN: auth.PostOwnerCheck, 46 }) 47 48 authPostList = auth.Config(&server.AuthConfig{ 49 Action: "post.list", 50 DefaultValue: entities.PERM_OWN, 51 OwnCheckFN: auth.AllowLoggedInUser, 52 }) 53 54 authPostView = auth.Config(&server.AuthConfig{ 55 Action: "post.view", 56 DefaultValue: entities.PERM_ALL, 57 }) 58 59 authCommentList = auth.Config(&server.AuthConfig{ 60 Action: "comment.list", 61 DefaultValue: entities.PERM_OWN, 62 OwnCheckFN: auth.AllowLoggedInUser, 63 }) 64 65 authCommentSave = auth.Config(&server.AuthConfig{ 66 Action: "comment.save", 67 DefaultValue: entities.PERM_OWN, 68 OwnCheckFN: auth.CommentOwnerCheck, 69 }) 70 71 authCommentDelete = auth.Config(&server.AuthConfig{ 72 Action: "comment.delete", 73 DefaultValue: entities.PERM_OWN, 74 OwnCheckFN: auth.CommentOwnerCheck, 75 }) 76 77 authFileUpload = auth.Config(&server.AuthConfig{ 78 Action: "file.upload", 79 DefaultValue: entities.PERM_OWN, 80 OwnCheckFN: auth.AllowLoggedInUser, 81 }) 82 83 authFileList = auth.Config(&server.AuthConfig{ 84 Action: "file.list", 85 DefaultValue: entities.PERM_OWN, 86 OwnCheckFN: auth.AllowLoggedInUser, 87 }) 88 89 authFileDelete = auth.Config(&server.AuthConfig{ 90 Action: "file.delete", 91 DefaultValue: entities.PERM_OWN, 92 Prepare: auth.GetFile, 93 OwnCheckFN: auth.FileOwnerCheck, 94 }) 95 96 authUserProfile = auth.Config(&server.AuthConfig{ 97 Action: "user.profile", 98 DefaultValue: entities.PERM_ALL, 99 }) 100 101 authUserSettingCompose = auth.Config(&server.AuthConfig{ 102 Action: "user.setting.compose", 103 DefaultValue: entities.PERM_OWN, 104 OwnCheckFN: auth.AllowLoggedInUser, 105 }) 106 107 authUserSettingSave = auth.Config(&server.AuthConfig{ 108 Action: "user.setting.save", 109 DefaultValue: entities.PERM_OWN, 110 OwnCheckFN: auth.AllowLoggedInUser, 111 }) 112 113 authTopicView = auth.Config(&server.AuthConfig{ 114 Action: "topic.view", 115 DefaultValue: entities.PERM_ALL, 116 }) 117 118 authTopicFeed = auth.Config(&server.AuthConfig{ 119 Action: "topic.feed", 120 DefaultValue: entities.PERM_ALL, 121 }) 122 ) 123 124 func NewServer(cfg Config) server.Server { 125 s := fiber.New(fiber.Config{ 126 JwtSigningKey: cfg.JwtSigningKey, 127 AppName: config.Setting("app_name"), 128 }) 129 s.Register(auth.Routes) 130 s.Static("/", path.Join(config.WD, "public")) 131 132 for _, assetFile := range asset.All() { 133 assetPath := path.Join("assets", assetFile.Name) 134 if config.DEVELOPMENT { 135 s.Static(assetPath, assetFile.Path) 136 } else { 137 if assetFile.DisableInline { 138 func(assetPath string, assetFile *asset.StaticAsset) { 139 s.Get(assetPath, func(c server.Context) error { 140 contentType := "text/plain; charset=utf-8" 141 142 if assetFile.Type == "css" { 143 contentType = "text/css; charset=utf-8" 144 } 145 146 if assetFile.Type == "js" { 147 contentType = "text/javascript; charset=utf-8" 148 } 149 150 c.Header("Content-Type", contentType) 151 152 return c.SendString(assetFile.Content) 153 }) 154 }(assetPath, assetFile) 155 } 156 } 157 } 158 159 s.Use(middlewares.All()...) 160 manage.RegisterRoutes(s) 161 162 compose := s.Group("/posts/:id") 163 compose.Get("", webpost.Compose, authPostCompose) 164 compose.Post("", webpost.Save, authPostSave) 165 compose.Delete("", webpost.Delete, authPostDelete) 166 167 comment := s.Group("/comments") 168 comment.Get("", webcomment.List, authCommentList) 169 comment.Post("/:id", webcomment.Save, authCommentSave) 170 comment.Delete("/:id", webcomment.Delete, authCommentDelete) 171 172 file := s.Group("/files") 173 file.Post("/upload", Upload, authFileUpload) 174 file.Get("", FileList, authFileList) 175 file.Delete("/:id", FileDelete, authFileDelete) 176 177 profile := s.Group("/u") 178 profile.Get("/:username", webuser.Profile, authUserProfile) 179 180 s.Get("", Index) 181 s.Get("/search", Search) 182 s.Get("/feed", Feed) 183 s.Get("/activate", webuser.Active) 184 s.Get("/inactive", webuser.Inactive) 185 s.Get("/login", webuser.Login) 186 s.Post("/login", webuser.PostLogin) 187 s.Get("/register", webuser.Register) 188 s.Post("/register", webuser.PostRegister) 189 s.Get("/logout", webuser.Logout) 190 s.Get("/sitemap/index.xml", websitemap.Index) 191 s.Get("/sitemap/topics.xml", websitemap.Topic) 192 s.Get("/sitemap/users.xml", websitemap.User) 193 s.Get("/sitemap/posts-:page.xml", websitemap.Post) 194 s.Get("/settings", websetting.Index, authUserSettingCompose) 195 s.Post("/settings", websetting.Save, authUserSettingSave) 196 197 s.Get("/posts", webpost.List, authPostList) 198 s.Get("/:slug.html", webpost.View, authPostView) 199 s.Get("/:slug", TopicView, authTopicView) 200 s.Get("/:slug/feed", TopicFeed, authTopicFeed) 201 202 return s 203 }