Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
graph/*.generated.go
.env
.env
*.pem
29 changes: 23 additions & 6 deletions cmd/backend/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import (
authservice "github.com/database-playground/backend-v2/httpapi/auth"
"github.com/database-playground/backend-v2/internal/auth"
"github.com/database-playground/backend-v2/internal/config"
"github.com/database-playground/backend-v2/internal/events"
"github.com/database-playground/backend-v2/internal/httputils"
"github.com/database-playground/backend-v2/internal/sqlrunner"
"github.com/database-playground/backend-v2/internal/useraccount"
"github.com/database-playground/backend-v2/internal/workers"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/redis/rueidis"
Expand All @@ -41,8 +44,8 @@ func SqlRunner(cfg config.Config) *sqlrunner.SqlRunner {
}

// GqlgenHandler creates a gqlgen handler.
func GqlgenHandler(entClient *ent.Client, storage auth.Storage, sqlrunner *sqlrunner.SqlRunner) *handler.Server {
srv := handler.New(graph.NewSchema(entClient, storage, sqlrunner))
func GqlgenHandler(entClient *ent.Client, storage auth.Storage, sqlrunner *sqlrunner.SqlRunner, eventService *events.EventService) *handler.Server {
srv := handler.New(graph.NewSchema(entClient, storage, sqlrunner, eventService))

srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
Expand All @@ -61,9 +64,19 @@ func GqlgenHandler(entClient *ent.Client, storage auth.Storage, sqlrunner *sqlru
return srv
}

// UserAccountContext creates a useraccount.Context.
func UserAccountContext(entClient *ent.Client, storage auth.Storage, eventService *events.EventService) *useraccount.Context {
return useraccount.NewContext(entClient, storage, eventService)
}

// EventService creates an events.EventService.
func EventService(entClient *ent.Client) *events.EventService {
return events.NewEventService(entClient)
}

// AuthService creates an auth service.
func AuthService(entClient *ent.Client, storage auth.Storage, config config.Config) httpapi.Service {
return authservice.NewAuthService(entClient, storage, config)
func AuthService(entClient *ent.Client, storage auth.Storage, config config.Config, useraccount *useraccount.Context) httpapi.Service {
return authservice.NewAuthService(entClient, storage, config, useraccount)
}

// GinEngine creates a gin engine.
Expand Down Expand Up @@ -132,12 +145,12 @@ func GinLifecycle(lifecycle fx.Lifecycle, engine *gin.Engine, cfg config.Config)
}
}()

go func() {
workers.Global.Go(func() {
<-httpCtx.Done()
if err := srv.Shutdown(context.Background()); err != nil {
slog.Error("error shutting down gin engine", "error", err)
}
}()
})

return nil
},
Expand All @@ -149,6 +162,10 @@ func GinLifecycle(lifecycle fx.Lifecycle, engine *gin.Engine, cfg config.Config)
cancel()
}

// Wait for all workers to finish
slog.Info("waiting for workers to finish")
workers.Global.Wait()

return nil
},
})
Expand Down
2 changes: 2 additions & 0 deletions cmd/backend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func main() {
fx.Provide(
AuthStorage,
SqlRunner,
UserAccountContext,
EventService,
AnnotateService(AuthService),
GqlgenHandler,
fx.Annotate(
Expand Down
1 change: 1 addition & 0 deletions docs/scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `me`:針對自身的操作
- `user`:使用者操作
- 包含登入記錄、點數查詢
- `group`:群組操作
- `scopeset`:範圍集合操作
- `database`:題庫對應資料庫的操作
Expand Down
Loading