2020-05-24 14:41:19 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2023-07-09 11:28:23 +03:00
|
|
|
"github.com/duke-git/lancet/v2/strutil"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2020-05-24 14:41:19 +03:00
|
|
|
"html/template"
|
2021-02-03 22:53:27 +03:00
|
|
|
"net/http"
|
2020-05-24 17:34:32 +03:00
|
|
|
"strings"
|
2021-04-11 13:42:43 +03:00
|
|
|
|
2022-07-09 06:58:16 +03:00
|
|
|
"github.com/duke-git/lancet/v2/datetime"
|
2021-04-11 13:42:43 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/utils"
|
2022-07-09 06:58:16 +03:00
|
|
|
"github.com/muety/wakapi/views"
|
2020-05-24 14:41:19 +03:00
|
|
|
)
|
|
|
|
|
2021-02-03 22:53:27 +03:00
|
|
|
type action func(w http.ResponseWriter, r *http.Request) (int, string, string)
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
var templates map[string]*template.Template
|
|
|
|
|
2021-08-08 13:33:04 +03:00
|
|
|
func Init() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:07:14 +03:00
|
|
|
func DefaultTemplateFuncs() template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2021-02-13 14:59:59 +03:00
|
|
|
"json": utils.Json,
|
2022-12-01 12:57:07 +03:00
|
|
|
"date": helpers.FormatDateHuman,
|
|
|
|
"datetime": helpers.FormatDateTimeHuman,
|
|
|
|
"simpledate": helpers.FormatDate,
|
|
|
|
"simpledatetime": helpers.FormatDateTime,
|
|
|
|
"duration": helpers.FmtWakatimeDuration,
|
2022-03-25 14:48:56 +03:00
|
|
|
"floordate": datetime.BeginOfDay,
|
2021-04-16 12:53:37 +03:00
|
|
|
"ceildate": utils.CeilDate,
|
2021-02-13 14:59:59 +03:00
|
|
|
"title": strings.Title,
|
|
|
|
"join": strings.Join,
|
2022-12-01 12:57:07 +03:00
|
|
|
"add": add,
|
2023-07-09 11:28:23 +03:00
|
|
|
"capitalize": strutil.Capitalize,
|
2022-10-04 00:52:22 +03:00
|
|
|
"lower": strings.ToLower,
|
2021-02-13 14:59:59 +03:00
|
|
|
"toRunes": utils.ToRunes,
|
2022-01-02 22:25:07 +03:00
|
|
|
"localTZOffset": utils.LocalTZOffset,
|
2021-02-13 14:59:59 +03:00
|
|
|
"entityTypes": models.SummaryTypes,
|
2022-10-04 00:52:22 +03:00
|
|
|
"strslice": utils.SubSlice[string],
|
2021-02-13 14:59:59 +03:00
|
|
|
"typeName": typeName,
|
2021-02-21 15:02:11 +03:00
|
|
|
"isDev": func() bool {
|
|
|
|
return config.Get().IsDev()
|
|
|
|
},
|
2020-05-24 18:32:26 +03:00
|
|
|
"getBasePath": func() string {
|
2020-10-04 11:37:38 +03:00
|
|
|
return config.Get().Server.BasePath
|
2020-05-24 18:32:26 +03:00
|
|
|
},
|
2020-05-24 22:42:15 +03:00
|
|
|
"getVersion": func() string {
|
2020-09-29 19:55:07 +03:00
|
|
|
return config.Get().Version
|
2020-05-24 22:42:15 +03:00
|
|
|
},
|
2020-10-16 17:58:16 +03:00
|
|
|
"getDbType": func() string {
|
2021-01-18 23:34:08 +03:00
|
|
|
return strings.ToLower(config.Get().Db.Type)
|
2020-10-16 17:58:16 +03:00
|
|
|
},
|
2020-05-30 21:41:27 +03:00
|
|
|
"htmlSafe": func(html string) template.HTML {
|
|
|
|
return template.HTML(html)
|
|
|
|
},
|
2022-10-06 00:36:57 +03:00
|
|
|
"urlSafe": func(s string) template.URL {
|
|
|
|
return template.URL(s)
|
|
|
|
},
|
2021-10-14 13:01:06 +03:00
|
|
|
"avatarUrlTemplate": func() string {
|
|
|
|
return config.Get().App.AvatarURLTemplate
|
|
|
|
},
|
2022-01-21 14:35:05 +03:00
|
|
|
"defaultWakatimeUrl": func() string {
|
|
|
|
return config.WakatimeApiUrl
|
|
|
|
},
|
2021-04-30 15:07:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:26:52 +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"
|
|
|
|
}
|
2021-06-11 21:59:34 +03:00
|
|
|
if t == models.SummaryLabel {
|
|
|
|
return "label"
|
|
|
|
}
|
2022-01-02 15:39:20 +03:00
|
|
|
if t == models.SummaryBranch {
|
|
|
|
return "branch"
|
|
|
|
}
|
2023-03-22 22:45:27 +03:00
|
|
|
if t == models.SummaryEntity {
|
|
|
|
return "entity"
|
|
|
|
}
|
2021-01-21 02:26:52 +03:00
|
|
|
return "unknown"
|
|
|
|
}
|
2021-02-12 20:37:30 +03:00
|
|
|
|
2021-08-08 13:33:04 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:37:30 +03:00
|
|
|
func defaultErrorRedirectTarget() string {
|
2023-01-02 20:05:28 +03:00
|
|
|
return config.Get().Server.BasePath + "/"
|
2021-02-12 20:37:30 +03:00
|
|
|
}
|
2022-12-01 12:57:07 +03:00
|
|
|
|
|
|
|
func add(i, j int) int {
|
|
|
|
return i + j
|
|
|
|
}
|