Skip to content

Commit

Permalink
docs: auth docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wer1d committed Dec 26, 2023
1 parent 5a8bd3e commit 9b0a405
Show file tree
Hide file tree
Showing 20 changed files with 1,828 additions and 223 deletions.
4 changes: 4 additions & 0 deletions apperror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ var (
InternalError = &AppError{"internal-server-error", http.StatusInternalServerError}
BadRequest = &AppError{"bad-request", http.StatusBadRequest}
InvalidEventId = &AppError{"invalid-event-id", http.StatusNotFound}
InvalidToken = &AppError{"invalid-token", http.StatusUnauthorized}
InvalidEmail = &AppError{"invalid-email", http.StatusNotFound}
DuplicateEmail = &AppError{"duplicate-email", http.StatusConflict}
Unauthorized = &AppError{"unauthorized", http.StatusUnauthorized}
)
20 changes: 19 additions & 1 deletion cfgldr/cfgldr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Config struct {
DatabaseConfig DatabaseConfig
AppConfig AppConfig
RedisConfig RedisConfig
OAuth2Config OAuth2Config
}

type DatabaseConfig struct {
Expand All @@ -25,8 +26,15 @@ type RedisConfig struct {
Password string `mapstructure:"PASSWORD"`
}

type OAuth2Config struct {
RedirectURL string `mapstructure:"REDIRECT_URL"`
ClientID string `mapstructure:"CLIENT_ID"`
ClientSecret string `mapstructure:"CLIENT_SECRET"`
Scopes []string `mapstructure:"SCOPES"`
Endpoint string `mapstructure:"ENDPOINT"`
}

func LoadConfig() (*Config, error) {

dbCfgLdr := viper.New()
dbCfgLdr.SetEnvPrefix("DB")
dbCfgLdr.AutomaticEnv()
Expand Down Expand Up @@ -54,10 +62,20 @@ func LoadConfig() (*Config, error) {
return nil, err
}

oauth2CfgLdr := viper.New()
oauth2CfgLdr.SetEnvPrefix("OAUTH2")
oauth2CfgLdr.AutomaticEnv()
oauth2CfgLdr.AllowEmptyEnv(false)
oauth2Config := OAuth2Config{}
if err := oauth2CfgLdr.Unmarshal(&oauth2Config); err != nil {
return nil, err
}

return &Config{
DatabaseConfig: dbConfig,
AppConfig: appConfig,
RedisConfig: redisConfig,
OAuth2Config: oauth2Config,
}, nil
}

Expand Down
22 changes: 13 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"
)

// @title OPH-66 Backend API
// @version 1.0
// @description Documentation outlines the specifications and endpoints for the OPH-66 Backend API.
// @Schemes http https
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
// @title OPH-66 Backend API
// @version 1.0
// @description Documentation outlines the specifications and endpoints for the OPH-66 Backend API.
// @Schemes http https
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
func main() {
container, err := di.Init()
if err != nil {
Expand All @@ -34,10 +34,14 @@ func main() {
r.GET("/live", container.FeatureflagHandler.GetLivestreamInfo)
r.GET("/events", container.EventHandler.GetAllEvents)
r.GET("/events/:eventId", container.EventHandler.GetEventById)
r.POST("/auth/register", container.AuthHandler.Register)
r.GET("/auth/me", container.AuthHandler.GetProfile)
r.GET("/auth/login", container.AuthHandler.GoogleLogin)
r.GET("/auth/callback", container.AuthHandler.GoogleCallback)

if container.Config.AppConfig.Env == "development" {
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
}
if err := r.Run(fmt.Sprintf(":%v", container.Config.AppConfig.Port)); err != nil {
container.Logger.Fatal("unable to start server")
}
Expand Down
8 changes: 7 additions & 1 deletion di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/isd-sgcu/oph66-backend/cache"
"github.com/isd-sgcu/oph66-backend/cfgldr"
"github.com/isd-sgcu/oph66-backend/database"
auth "github.com/isd-sgcu/oph66-backend/internal/auth"
event "github.com/isd-sgcu/oph66-backend/internal/event"
featureflag "github.com/isd-sgcu/oph66-backend/internal/feature_flag"
healthcheck "github.com/isd-sgcu/oph66-backend/internal/health_check"
Expand All @@ -19,15 +20,17 @@ type Container struct {
EventHandler event.Handler
HcHandler healthcheck.Handler
FeatureflagHandler featureflag.Handler
AuthHandler auth.Handler
Config *cfgldr.Config
Logger *zap.Logger
}

func newContainer(eventHandler event.Handler, hcHandler healthcheck.Handler, featureflagHandler featureflag.Handler, config *cfgldr.Config, logger *zap.Logger) Container {
func newContainer(eventHandler event.Handler, hcHandler healthcheck.Handler, featureflagHandler featureflag.Handler, authHandler auth.Handler, config *cfgldr.Config, logger *zap.Logger) Container {
return Container{
eventHandler,
hcHandler,
featureflagHandler,
authHandler,
config,
logger,
}
Expand All @@ -48,6 +51,9 @@ func Init() (Container, error) {
featureflag.NewCache,
featureflag.NewService,
featureflag.NewRepository,
auth.NewHandler,
auth.NewService,
auth.NewRepository,
logger.InitLogger,
)

Expand Down
10 changes: 8 additions & 2 deletions di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9b0a405

Please sign in to comment.