wakapi/routes/routes.go

104 lines
2.4 KiB
Go
Raw Normal View History

package routes
import (
"fmt"
"github.com/duke-git/lancet/v2/datetime"
"github.com/muety/wakapi/views"
"html/template"
2021-02-03 22:53:27 +03:00
"net/http"
2020-05-24 17:34:32 +03:00
"strings"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
)
2021-02-03 22:53:27 +03:00
type action func(w http.ResponseWriter, r *http.Request) (int, string, string)
var templates map[string]*template.Template
func Init() {
loadTemplates()
}
2021-04-30 15:07:14 +03:00
func DefaultTemplateFuncs() template.FuncMap {
return template.FuncMap{
"json": utils.Json,
"date": utils.FormatDateHuman,
2021-04-30 17:20:08 +03:00
"datetime": utils.FormatDateTimeHuman,
"simpledate": utils.FormatDate,
"simpledatetime": utils.FormatDateTime,
2021-04-30 15:07:14 +03:00
"duration": utils.FmtWakatimeDuration,
"floordate": datetime.BeginOfDay,
"ceildate": utils.CeilDate,
"title": strings.Title,
"join": strings.Join,
"add": utils.Add,
"capitalize": utils.Capitalize,
"toRunes": utils.ToRunes,
2022-01-02 22:25:07 +03:00
"localTZOffset": utils.LocalTZOffset,
"entityTypes": models.SummaryTypes,
"typeName": typeName,
"isDev": func() bool {
return config.Get().IsDev()
},
"getBasePath": func() string {
return config.Get().Server.BasePath
},
2020-05-24 22:42:15 +03:00
"getVersion": func() string {
return config.Get().Version
2020-05-24 22:42:15 +03:00
},
2020-10-16 17:58:16 +03:00
"getDbType": func() string {
return strings.ToLower(config.Get().Db.Type)
2020-10-16 17:58:16 +03:00
},
"htmlSafe": func(html string) template.HTML {
return template.HTML(html)
},
2021-10-14 13:01:06 +03:00
"avatarUrlTemplate": func() string {
return config.Get().App.AvatarURLTemplate
},
"defaultWakatimeUrl": func() string {
return config.WakatimeApiUrl
},
2021-04-30 15:07:14 +03:00
}
}
func typeName(t uint8) string {
if t == models.SummaryProject {
return "project"
}
if t == models.SummaryLanguage {
return "language"
}
if t == models.SummaryEditor {
return "editor"
}
if t == models.SummaryOS {
return "operating system"
}
if t == models.SummaryMachine {
return "machine"
}
if t == models.SummaryLabel {
return "label"
}
if t == models.SummaryBranch {
return "branch"
}
return "unknown"
}
func loadTemplates() {
// Use local file system when in 'dev' environment, go embed file system otherwise
templateFs := config.ChooseFS("views", views.TemplateFiles)
if tpls, err := utils.LoadTemplates(templateFs, DefaultTemplateFuncs()); err == nil {
templates = tpls
} else {
panic(err)
}
}
func defaultErrorRedirectTarget() string {
return fmt.Sprintf("%s/?error=unauthorized", config.Get().Server.BasePath)
}