126 lines
4.3 KiB
Go
126 lines
4.3 KiB
Go
package setup
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.artlef.fr/bibliomane/front"
|
|
"git.artlef.fr/bibliomane/internal/appcontext"
|
|
"git.artlef.fr/bibliomane/internal/config"
|
|
"git.artlef.fr/bibliomane/internal/createuser"
|
|
"git.artlef.fr/bibliomane/internal/db"
|
|
"git.artlef.fr/bibliomane/internal/fileutils"
|
|
i18nresource "git.artlef.fr/bibliomane/internal/i18nresource"
|
|
"git.artlef.fr/bibliomane/internal/middleware"
|
|
"git.artlef.fr/bibliomane/internal/routes"
|
|
)
|
|
|
|
func Setup(config *config.Config) *gin.Engine {
|
|
db := db.Initdb(config.DatabaseFilePath, config.DemoDataPath)
|
|
r := gin.Default()
|
|
|
|
err := fileutils.CreateFolderIfMissing(config.ImageFolderPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
bundle := i18nresource.InitializeI18n()
|
|
err = createuser.CreateDefaultUsers(appcontext.AppContext{C: nil, Db: db, I18n: bundle, Config: config})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
ws := r.Group("/ws")
|
|
ws.Use(middleware.Auth())
|
|
|
|
ws.GET("/appinfo", func(c *gin.Context) {
|
|
routes.GetAppInfo(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
|
|
ws.GET("/mybooks/read", func(c *gin.Context) {
|
|
routes.GetMyBooksReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/mybooks/reading", func(c *gin.Context) {
|
|
routes.GetMyBooksReadingHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/mybooks/wantread", func(c *gin.Context) {
|
|
routes.GetMyBooksWantReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/search/:searchterm", func(c *gin.Context) {
|
|
routes.GetSearchBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/inventaire/books/:workId", func(c *gin.Context) {
|
|
routes.GetInventaireBooks(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/book/:id", func(c *gin.Context) {
|
|
routes.GetBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.PUT("/book/:id/read", func(c *gin.Context) {
|
|
routes.PutReadUserBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.PUT("/book/:id/wantread", func(c *gin.Context) {
|
|
routes.PutWantReadUserBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.PUT("/book/:id/startread", func(c *gin.Context) {
|
|
routes.PutStartReadUserBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.PUT("/book/:id/rate", func(c *gin.Context) {
|
|
routes.PutRateUserBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.POST("/book", func(c *gin.Context) {
|
|
routes.PostBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.POST("/importbook", func(c *gin.Context) {
|
|
routes.PostImportBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/author/:id", func(c *gin.Context) {
|
|
routes.GetAuthorHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.GET("/author/:id/books", func(c *gin.Context) {
|
|
routes.GetAuthorBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.POST("/auth/signup", func(c *gin.Context) {
|
|
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.POST("/auth/login", func(c *gin.Context) {
|
|
routes.PostLoginHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
ws.POST("/upload/cover", func(c *gin.Context) {
|
|
routes.PostUploadBookCoverHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
})
|
|
|
|
r.Static("/static/bookcover", config.ImageFolderPath)
|
|
|
|
folders := []string{"assets", "css", "image"}
|
|
|
|
for _, folder := range folders {
|
|
subFs, err := fs.Sub(front.Frontend, folder)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
r.StaticFS("/"+folder, http.FS(subFs))
|
|
}
|
|
|
|
r.StaticFileFS("/favicon.svg", "favicon.svg", http.FS(front.Frontend))
|
|
r.GET("/", serveIndexHtml)
|
|
|
|
r.NoRoute(serveIndexHtml)
|
|
|
|
return r
|
|
}
|
|
|
|
func serveIndexHtml(c *gin.Context) {
|
|
indexHtml, err := front.Frontend.Open("index.html")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer indexHtml.Close()
|
|
c.Header("Content-Type", "text/html")
|
|
c.Status(http.StatusOK)
|
|
fileReader := bufio.NewReader(indexHtml)
|
|
_, err = io.Copy(c.Writer, fileReader)
|
|
}
|