filter by learning_types and course_thematics

This commit is contained in:
Aleksandr Trushkin
2024-01-10 00:02:40 +03:00
parent 728c8fa59e
commit 5fd0861e2d
10 changed files with 346 additions and 22 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"log/slog"
"net/http"
"strings"
"time"
"git.loyso.art/frx/kurious/assets/kurious"
@ -13,6 +14,23 @@ import (
"github.com/gorilla/mux"
)
const (
pathParamLearningType = "learning_type"
pathParamThematicType = "thematic_type"
)
func makePathTemplate(params ...string) string {
var sb strings.Builder
for _, param := range params {
sb.WriteRune('/')
sb.WriteRune('{')
sb.WriteString(param)
sb.WriteRune('}')
}
return sb.String()
}
func setupHTTP(cfg config.HTTP, srv xhttp.Server, log *slog.Logger) *http.Server {
router := mux.NewRouter()
@ -23,8 +41,12 @@ func setupHTTP(cfg config.HTTP, srv xhttp.Server, log *slog.Logger) *http.Server
router.HandleFunc("/updatedesc", coursesAPI.UdpateDescription).Methods(http.MethodPost)
coursesRouter := router.PathPrefix("/courses").Subrouter()
coursesRouter.HandleFunc("/", coursesAPI.List).Methods(http.MethodGet)
coursesListLearningOnlyPath := makePathTemplate(pathParamLearningType)
coursesRouter.HandleFunc(coursesListLearningOnlyPath, coursesAPI.List).Methods(http.MethodGet)
coursesListFullPath := makePathTemplate(pathParamLearningType, pathParamThematicType)
coursesRouter.HandleFunc(coursesListFullPath, coursesAPI.List).Methods(http.MethodGet)
courseRouter := coursesRouter.PathPrefix("/{course_id}").Subrouter()
courseRouter := router.PathPrefix("/course").PathPrefix("/{course_id}").Subrouter()
courseRouter.HandleFunc("/", coursesAPI.Get).Methods(http.MethodGet)
courseRouter.HandleFunc("/short", coursesAPI.GetShort).Methods(http.MethodGet)
courseRouter.HandleFunc("/editdesc", coursesAPI.RenderEditDescription).Methods(http.MethodGet)

View File

@ -16,6 +16,7 @@ import (
"git.loyso.art/frx/kurious/internal/kurious/adapters"
xhttp "git.loyso.art/frx/kurious/internal/kurious/ports/http"
"git.loyso.art/frx/kurious/internal/kurious/service"
"golang.org/x/sync/errgroup"
)
@ -88,26 +89,30 @@ func app(ctx context.Context) error {
slog.String("addr", httpServer.Addr),
)
err := httpServer.ListenAndServe()
if err != nil {
if err := httpServer.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("listening http: %w", err)
}
}
return nil
})
eg.Go(func() error {
<-egctx.Done()
xcontext.LogInfo(ctx, log, "trying to shutdown http")
sdctx, sdcancel := context.WithTimeout(context.Background(), time.Second*10)
defer sdcancel()
err := httpServer.Shutdown(sdctx)
if err != nil {
return fmt.Errorf("shutting down the server: %w", err)
}
xcontext.LogInfo(ctx, log, "server closed successfuly")
return nil
})