40 lines
923 B
Go
40 lines
923 B
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"forge.cadoles.com/wpetit/kouiz/internal/config"
|
|
kouizHTTP "forge.cadoles.com/wpetit/kouiz/internal/http"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func NewHTTPServerFromConfig(ctx context.Context, conf *config.Config) (*kouizHTTP.Server, error) {
|
|
// Configure Web UI handler
|
|
webui, err := NewWebUIHandlerFromConfig(ctx, conf)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not configure webui handler from config")
|
|
}
|
|
|
|
// Configure API handler
|
|
api, err := NewAPIHandlerFromConfig(ctx, conf)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "could not configure webui handler from config")
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", webui)
|
|
mux.Handle("/api/", http.StripPrefix("/api", api))
|
|
|
|
// Create HTTP server
|
|
|
|
server := kouizHTTP.NewServer(
|
|
mux,
|
|
kouizHTTP.WithAddress(conf.HTTP.Address),
|
|
kouizHTTP.WithBaseURL(conf.HTTP.BaseURL),
|
|
)
|
|
|
|
return server, nil
|
|
}
|