13
"github.com/xelbot/yetacache"
19
var GoVersionNumbers string
21
type URLGenerator func(string, bool, ...string) string
23
type Application struct {
28
intCache *yetacache.Cache[string, int]
29
strCache *yetacache.Cache[string, string]
32
var urlGen URLGenerator
35
re := regexp.MustCompile(`^\D*(\d+\.\d+(?:\.\d+)?)`)
36
GoVersionNumbers = re.FindStringSubmatch(runtime.Version())[1]
41
func checkBuildFlags() {
42
if len(Version) == 0 {
43
panic("ldflags: xelbot.com/reprogl/container.Version is empty")
45
if len(GitRevision) == 0 {
46
panic("ldflags: xelbot.com/reprogl/container.GitRevision is empty")
49
_, err := time.Parse(time.RFC3339, BuildTime)
51
panic("ldflags: xelbot.com/reprogl/container.BuildTime wrong format")
55
func BuildTimeRFC1123() string {
56
t, _ := time.Parse(time.RFC3339, BuildTime)
58
return t.Format(time.RFC1123)
61
func SetURLGenerator(u URLGenerator) {
65
func GenerateURL(routeName string, pairs ...string) string {
66
return urlGen(routeName, false, pairs...)
69
func GenerateAbsoluteURL(routeName string, pairs ...string) string {
70
return urlGen(routeName, true, pairs...)
73
func (app *Application) NotFound(w http.ResponseWriter) {
74
app.ClientError(w, http.StatusNotFound)
77
func (app *Application) ServerError(w http.ResponseWriter, err error) {
78
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
79
app.ErrorLog.Println(trace)
81
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
84
func (app *Application) ClientError(w http.ResponseWriter, status int) {
85
http.Error(w, http.StatusText(status), status)
88
func (app *Application) LogError(err error) {
89
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
90
app.ErrorLog.Println(trace)
93
func (app *Application) GetIntCache() *yetacache.Cache[string, int] {
94
if app.intCache == nil {
95
app.InfoLog.Println("[CACHE] create integer instance")
96
app.intCache = yetacache.New[string, int](time.Hour, 8*time.Hour)
102
func (app *Application) GetStringCache() *yetacache.Cache[string, string] {
103
if app.strCache == nil {
104
app.InfoLog.Println("[CACHE] create string instance")
105
app.strCache = yetacache.New[string, string](time.Hour, 8*time.Hour)
111
func (app *Application) Stop() error {
112
err := app.DB.Close()
116
app.InfoLog.Print("The database connection is closed")