diff --git a/api/api.go b/api/api.go index 335328f..88ec44c 100644 --- a/api/api.go +++ b/api/api.go @@ -4,19 +4,45 @@ import ( "api/db" "api/handlers" "api/utils" + "net/http" "github.com/gin-gonic/gin" "github.com/joho/godotenv" ) +func CORSMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") + c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") + c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(204) + return + } + + c.Next() + } +} + func main() { godotenv.Load() utils.LoadConfig() db.InitializeDB() router := gin.Default() + router.Use(CORSMiddleware()) + router.Static("css", "views/css") + router.Static("js", "views/js") + router.LoadHTMLGlob("views/html/*.html") + + router.GET("/", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "index.html", nil) + }) - bookmarkRouter := router.Group("/bookmarks") + apiRouter := router.Group("/api") + bookmarkRouter := apiRouter.Group("/bookmarks") { bookmarkRouter.POST("", handlers.AddBookmark) bookmarkRouter.GET("", handlers.GetBookmarks) @@ -27,7 +53,7 @@ func main() { bookmarkRouter.DELETE("/:id", handlers.DeleteBookmark) bookmarkRouter.DELETE("", handlers.BulkDeleteBookmarks) } - tagRouter := router.Group("/tags") + tagRouter := apiRouter.Group("/tags") { tagRouter.POST("", handlers.CreateTag) tagRouter.GET("", handlers.GetAllTags) @@ -36,7 +62,7 @@ func main() { tagRouter.GET("/tree", handlers.GetLinkTree) } - configRouter := router.Group("/config") + configRouter := apiRouter.Group("/config") { configRouter.GET("", handlers.GetConfig) configRouter.PATCH("/save-offline", handlers.ToggleSaveOffline) diff --git a/api/readme.md b/api/readme.md index af2baab..e425ab3 100644 --- a/api/readme.md +++ b/api/readme.md @@ -43,12 +43,10 @@ - [ ] Pagination - [x] meta favicon bug 12 -- [ ] Search +- [x] Search - https://www.sqlite.org/fts5.html - [ ] Import/Export -- [ ] Save as PDF/HTML - - go-shiori/Orca - - go-shiori/readability +- [x] Save offline as HTML 13 - [ ] Browser extension - [ ] Web frontend @@ -57,7 +55,13 @@ - [?] Sync - [ ] Phone frontend - - [-] Desktop frontend - [-] CLI frontend - \ No newline at end of file + + +### Finishing + +- [ ] Standardize Logging +- [ ] Standardize errors +- [ ] Publishing +- [ ] Trimming binaries \ No newline at end of file