feat: initial commit
This commit is contained in:
54
internal/setup/webui_handler.go
Normal file
54
internal/setup/webui_handler.go
Normal file
@ -0,0 +1,54 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/config"
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui"
|
||||
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func NewWebUIHandlerFromConfig(ctx context.Context, conf *config.Config) (*webui.Handler, error) {
|
||||
opts := make([]webui.OptionFunc, 0)
|
||||
|
||||
// Configure auth handler
|
||||
authHandler, err := NewAuthHandlerFromConfig(ctx, conf)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not configure auth handler from config")
|
||||
}
|
||||
|
||||
authMiddleware := authHandler.Middleware()
|
||||
|
||||
opts = append(opts, webui.WithMount("/auth/", authHandler))
|
||||
|
||||
// Configure index redirect
|
||||
opts = append(opts, webui.WithMount("/", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/quiz", http.StatusTemporaryRedirect)
|
||||
}))))
|
||||
|
||||
// Configure quiz handler
|
||||
quizHandler, err := NewQuizHandlerFromConfig(ctx, conf)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not configure issue handler from config")
|
||||
}
|
||||
|
||||
opts = append(opts, webui.WithMount("/quiz/", authMiddleware(quizHandler)))
|
||||
|
||||
// Configure common handler
|
||||
commonHandler, err := NewCommonHandlerFromConfig(ctx, conf)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not configure common handler from config")
|
||||
}
|
||||
|
||||
opts = append(opts, webui.WithMount("/assets/", commonHandler))
|
||||
|
||||
handler := webui.NewHandler(opts...)
|
||||
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
func NewCommonHandlerFromConfig(ctx context.Context, conf *config.Config) (*common.Handler, error) {
|
||||
return common.NewHandler(), nil
|
||||
}
|
Reference in New Issue
Block a user