Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (58 loc) · 1.78 KB
/
Copy pathmain.go
File metadata and controls
71 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fingelpp/access"
"fingelpp/lessons"
"html/template"
"net/http"
"path/filepath"
"strings"
"github.com/IBM/fp-go/v2/result"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
func createHTMLRenderer(rootDir string) multitemplate.Renderer {
r := multitemplate.NewRenderer()
funcMap := template.FuncMap{
"nl2br": func(text string) template.HTML {
escaped := template.HTMLEscapeString(text)
withBreaks := strings.ReplaceAll(escaped, "\n", "<br>")
return template.HTML(withBreaks)
},
}
globals := []string{rootDir + "/base.tmpl", rootDir + "/svg/edit.tmpl"}
pages, err := filepath.Glob(rootDir + "/pages/*.tmpl")
if err != nil {
panic(err.Error())
}
for _, page := range pages {
files := make([]string, len(globals)+1)
copy(files, globals)
files[len(files)-1] = page
r.AddFromFilesFuncsWithOptions(filepath.Base(page), funcMap, multitemplate.TemplateOptions{}, files...)
}
return r
}
func toResult[T any](value T, err error) result.Result[T] {
if err != nil {
return result.Left[T](err)
}
return result.Right(value)
}
func main() {
r := gin.Default()
r.HTMLRender = createHTMLRenderer("./www/templates")
r.Static("/static", "./www/static")
r.GET("/", func(c *gin.Context) {
// lastOpened := result.Chain(func(value string) result.Result[lessons.LessonId] {
// lessonId, err := lessons.ParseLessonId(value)
// if err != nil {
// return result.Left[lessons.LessonId](err)
// }
// return result.Right(lessonId)
// })(c.Cookie("lastOpenedLesson"))
c.HTML(http.StatusOK, "home.tmpl", gin.H{"Chapters": lessons.CurrentBook.Chapters, "EditPerms": access.CurrentAccessFile.HasPermission(c, access.PermissionEditLesson), "LastOpened": ""})
})
lessons.Routes(r)
access.Routes(r)
r.Run("localhost:2025")
}