diff --git a/cmd/kuriweb/http.go b/cmd/kuriweb/http.go index edb22c7..64efce2 100644 --- a/cmd/kuriweb/http.go +++ b/cmd/kuriweb/http.go @@ -41,7 +41,7 @@ func setupHTTPWithTempl(srv xhttp.Server, router *mux.Router, log *slog.Logger) } func setupHTTPWithGoTemplates(srv xhttp.Server, router *mux.Router, log *slog.Logger) { - coursesAPI := srv.Courses() + coursesAPI := srv.Courses(true) coursesRouter := router.PathPrefix("/courses").Subrouter().StrictSlash(true) coursesRouter.HandleFunc("/", coursesAPI.List).Methods(http.MethodGet) diff --git a/internal/kurious/ports/http/course.go b/internal/kurious/ports/http/course.go index 0481ff4..d48c185 100644 --- a/internal/kurious/ports/http/course.go +++ b/internal/kurious/ports/http/course.go @@ -2,6 +2,7 @@ package http import ( "encoding/json" + "html/template" "log/slog" "net/http" "sort" @@ -21,6 +22,8 @@ import ( type courseServer struct { app service.Application log *slog.Logger + + useTailwind bool } type pagination struct { @@ -218,7 +221,13 @@ func (c courseServer) List(w http.ResponseWriter, r *http.Request) { }) } - err = getCoreTemplate(ctx, c.log).ExecuteTemplate(w, "courses", templateCourses) + var tmpl *template.Template + if c.useTailwind { + tmpl = getTemplateHTMLBySpecificFiles(ctx, c.log, "list.html") + } else { + tmpl = getCoreTemplate(ctx, c.log) + } + err = tmpl.ExecuteTemplate(w, "courses", templateCourses) if handleError(ctx, err, w, c.log, "unable to execute template") { return } diff --git a/internal/kurious/ports/http/course_test.go b/internal/kurious/ports/http/course_test.go deleted file mode 100644 index 7f62c6a..0000000 --- a/internal/kurious/ports/http/course_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package http - -import ( - "strconv" - "strings" - "testing" - "time" - - "git.loyso.art/frx/kurious/internal/kurious/domain" -) - -var courses = func() []domain.Course { - out := make([]domain.Course, 0) - out = append(out, makeBatchCourses("prog", []string{"go", "rust"}, 4)...) - out = append(out, makeBatchCourses("front", []string{"js", "html"}, 4)...) - - return out -}() - -func makeBatchCourses(lt string, cts []string, num int) []domain.Course { - out := make([]domain.Course, 0, len(cts)*num) - for _, ct := range cts { - for i := 0; i < num; i++ { - name := strings.Join([]string{ - lt, ct, - strconv.Itoa(i), - }, ".") - out = append(out, makeCourse(lt, ct, name)) - } - } - - return out -} - -func makeCourse(lt, ct, name string) domain.Course { - return domain.Course{ - LearningType: lt, - Thematic: ct, - Name: name, - ID: lt + ct + name, - FullPrice: 123, - Duration: time.Second * 100, - StartsAt: time.Now(), - } -} - -func TestRenderTemplate(t *testing.T) { - t.SkipNow() - result := mapDomainCourseToTemplate(courses...) - t.Logf("%#v", result) - - var out strings.Builder - err := listTemplateParsed.ExecuteTemplate(&out, "courses", result) - if err != nil { - t.Fatalf("executing: %v", err) - } - - t.Log(out.String()) - t.Fail() -} diff --git a/internal/kurious/ports/http/html/index.html b/internal/kurious/ports/http/html/index.html new file mode 100644 index 0000000..6e4b0c9 --- /dev/null +++ b/internal/kurious/ports/http/html/index.html @@ -0,0 +1,20 @@ +{{ define "base" }} + + + + {{ template "htmlhead" . }} + + + {{ template "htmlbody" . }} + + +{{ end }} + +{{ define "htmlhead" }} + + + + {{ .AppName }} + + +{{ end }} diff --git a/internal/kurious/ports/http/html/list.html b/internal/kurious/ports/http/html/list.html new file mode 100644 index 0000000..49fb101 --- /dev/null +++ b/internal/kurious/ports/http/html/list.html @@ -0,0 +1,14 @@ +{{ define "htmlbody" }} + {{ template "header" .}} + {{ template "body" .}} + {{ template "footer" .}} +{{ end }} + +{{ define "header" }} +{{ end }} + +{{ define "body" }} +{{ end }} + +{{ define "footer" }} +{{ end }} diff --git a/internal/kurious/ports/http/listtemplate.go b/internal/kurious/ports/http/listtemplate.go index bc43d77..83bb35e 100644 --- a/internal/kurious/ports/http/listtemplate.go +++ b/internal/kurious/ports/http/listtemplate.go @@ -12,7 +12,11 @@ import ( "git.loyso.art/frx/kurious/internal/common/xslices" ) -const baseTemplatePath = "./internal/kurious/ports/http/templates/" +const ( + baseTemplatePath = "./internal/kurious/ports/http" + templateDir = "/templates" + htmlPath = "/html" +) func must[T any](t T, err error) T { if err != nil { @@ -21,9 +25,10 @@ func must[T any](t T, err error) T { return t } -func scanFiles() []string { +func scanFiles(dir string) []string { + dst := path.Join(baseTemplatePath, dir) entries := xslices.Map( - must(os.ReadDir(baseTemplatePath)), + must(os.ReadDir(dst)), func(v fs.DirEntry) string { return path.Join(baseTemplatePath, v.Name()) }, @@ -32,161 +37,31 @@ func scanFiles() []string { return entries } +func getTemplateHTMLBySpecificFiles(ctx context.Context, log *slog.Logger, filenames ...string) *template.Template { + filenames = append([]string{"index.html"}, filenames...) + dir := path.Join(baseTemplatePath, htmlPath) + out := xslices.Map(filenames, func(in string) string { + return path.Join(dir, in) + }) + + tmpl, err := template.New("courses").ParseFiles(out...) + if err != nil { + xcontext.LogWithWarnError(ctx, log, err, "unable to parse template") + + return nil + } + + return tmpl +} + func getCoreTemplate(ctx context.Context, log *slog.Logger) *template.Template { - filenames := scanFiles() + filenames := scanFiles(templateDir) out, err := template.New("courses").ParseFiles(filenames...) if err != nil { xcontext.LogWithWarnError(ctx, log, err, "unable to parse template") - return listTemplateParsed + return nil } return out } - -var listTemplateParsed = template.Must( - template.New("courses"). - Parse(listTemplate), -) - -const listTemplate = `{{define "courses"}} - - - - Courses - - - -
-

Courses

- -
- {{range $category := .Categories}} -

Category {{$category.Name}}

-

Course Description: {{$category.Description}}

- {{range $subcategory := $category.Subcategories}} -
-

Subcategory: {{$subcategory.Name}}

-

Subcategory Description: {{$subcategory.Description}}

- {{range $course := $subcategory.Courses}} -
-

{{$course.Name}}

-

Description:

{{or $course.Description "..."}}

-

Full price: {{$course.FullPrice}}

-

Discount: {{$course.Discount}}

-

Thematic: {{$course.Thematic}}

-

Learning type: {{$course.LearningType}}

-

Duration: {{$course.Duration}}

-

Starts at: {{$course.StartsAt}}

-
-
- {{end}} - {{end}} - {{end}} - - - - - -{{end}}` diff --git a/internal/kurious/ports/http/server.go b/internal/kurious/ports/http/server.go index 9b16138..5c1223a 100644 --- a/internal/kurious/ports/http/server.go +++ b/internal/kurious/ports/http/server.go @@ -23,8 +23,12 @@ func NewServer(app service.Application, log *slog.Logger) Server { } } -func (s Server) Courses() courseServer { - return courseServer(s) +func (s Server) Courses(useTailwind bool) courseServer { + return courseServer{ + app: s.app, + log: s.log, + useTailwind: useTailwind, + } } func (s Server) CoursesByTempl() courseTemplServer {