diff options
author | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-21 19:26:56 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-21 19:28:56 +0300 |
commit | ce3111b0efe91e275ce070f9511b5b1b9801a46d (patch) | |
tree | 09fa4f10dfb1e17761339c798eefa73c6b18484f /app/cmd/serve.go | |
parent | e9a64f3b41b5eae47dec7c0ecfd1caae83136abc (diff) |
Множество улучшенийv0.0.2
Diffstat (limited to 'app/cmd/serve.go')
-rw-r--r-- | app/cmd/serve.go | 53 |
1 files changed, 25 insertions, 28 deletions
diff --git a/app/cmd/serve.go b/app/cmd/serve.go index 5593c32..e2d2843 100644 --- a/app/cmd/serve.go +++ b/app/cmd/serve.go @@ -2,7 +2,6 @@ package cmd import ( "context" - "database/sql" "fmt" "log/slog" "net" @@ -15,10 +14,8 @@ import ( "github.com/michaeljs1990/sqlitestore" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/uptrace/bun" - "github.com/uptrace/bun/dialect/sqlitedialect" - "github.com/uptrace/bun/extra/bundebug" "gitrepo.ru/neonxp/gorum/contextlib" + "gitrepo.ru/neonxp/gorum/db" "gitrepo.ru/neonxp/gorum/middleware" "gitrepo.ru/neonxp/gorum/repository" "gitrepo.ru/neonxp/gorum/routes" @@ -42,9 +39,9 @@ var ( ) func init() { - serverCmd.PersistentFlags().StringVar(&theme, "theme", "default", "theme to use (default is 'default')") - serverCmd.PersistentFlags().StringVar(&listen, "listen", ":8000", "bind address to listen (default is ':8000')") - serverCmd.PersistentFlags().StringVar(&sessionSecret, "session_secret", "s3cr3t", "sessions secret (default is 's3cr3t')") + serverCmd.PersistentFlags().StringVar(&theme, "theme", "default", "theme to use") + serverCmd.PersistentFlags().StringVar(&listen, "listen", ":8000", "bind address to listen") + serverCmd.PersistentFlags().StringVar(&sessionSecret, "session_secret", "s3cr3t", "sessions secret") viper.BindPFlag("theme", serverCmd.Flags().Lookup("theme")) viper.BindPFlag("listen", serverCmd.Flags().Lookup("listen")) viper.BindPFlag("session_secret", serverCmd.Flags().Lookup("session_secret")) @@ -60,14 +57,12 @@ func serve(ctx context.Context) error { ) ctx = context.WithValue(ctx, contextlib.ThemeKey, theme) - db, err := sql.Open("sqlite3", dbFile) + orm, err := db.GetDB(dbFile) if err != nil { - return fmt.Errorf("open db failed: %w", err) + return err } - defer db.Close() - orm := bun.NewDB(db, sqlitedialect.New()) - orm.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true))) + defer orm.Close() userRepo := repository.NewUser(orm) nodeRepo := repository.NewNode(orm) @@ -82,11 +77,7 @@ func serve(ctx context.Context) error { _ = utils.Render(c, views.ErrorPage(err)) } - e.Server.BaseContext = func(l net.Listener) context.Context { - return ctx - } - - sessionStore, err := sqlitestore.NewSqliteStoreFromConnection(db, "sessions", "", 0, []byte(sessionSecret)) + sessionStore, err := sqlitestore.NewSqliteStoreFromConnection(orm.DB, "sessions", "", 0, []byte(sessionSecret)) if err != nil { return fmt.Errorf("failed init session store: %w", err) } @@ -94,14 +85,14 @@ func serve(ctx context.Context) error { e.Use( echomiddleware.Recover(), echomiddleware.Gzip(), - echomiddleware.CSRFWithConfig(echomiddleware.CSRFConfig{ - Skipper: echomiddleware.DefaultSkipper, - TokenLength: 32, - TokenLookup: "form:" + echo.HeaderXCSRFToken, - ContextKey: "csrf", - CookieName: "_csrf", - CookieMaxAge: 86400, - }), + // echomiddleware.CSRFWithConfig(echomiddleware.CSRFConfig{ + // Skipper: echomiddleware.DefaultSkipper, + // TokenLength: 32, + // TokenLookup: "form:" + echo.HeaderXCSRFToken, + // ContextKey: "csrf", + // CookieName: "_csrf", + // CookieMaxAge: 86400, + // }), session.Middleware(sessionStore), middleware.UserMiddleware(), ) @@ -113,9 +104,12 @@ func serve(ctx context.Context) error { e.POST("/logout", r.Logout) e.GET("/", r.Node) - e.GET("/n/:id", r.Node) - e.GET("/n/:id/new", r.NewPost) - e.POST("/n/:id/new", r.NewPost) + e.GET("/p/:id", r.Node) + e.GET("/p/:id/new", r.NewPost) + e.POST("/p/:id/new", r.NewPost) + e.GET("/t/:id", r.Node) + e.GET("/t/:id/new", r.NewTopic) + e.POST("/t/:id/new", r.NewTopic) e.StaticFS("/assets", assets.FS) @@ -125,6 +119,9 @@ func serve(ctx context.Context) error { Addr: listen, Handler: e, ErrorLog: slog.NewLogLogger(slog.Default().Handler(), slog.LevelError), + ConnContext: func(cctx context.Context, c net.Conn) context.Context { + return ctx + }, } if err := server.ListenAndServe(); err != http.ErrServerClosed { return fmt.Errorf("server failed: %w", err) |