Lindenii Project Forge
Login
Commit info
IDd60a53a06add1248e7d6ed9e6636d212ed43662a
AuthorRunxi Yu<me@runxiyu.org>
Author dateFri, 07 Mar 2025 18:50:12 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateFri, 07 Mar 2025 18:50:12 +0800
Actions
Get patch
http/templates: Add query_escape and path_escape
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileContributor: Runxi Yu <https://runxiyu.org>

package main

import (
	"path"
	"strings"
	"net/url"
)

func first_line(s string) string {
	before, _, _ := strings.Cut(s, "\n")
	return before
}

func base_name(s string) string {
	return path.Base(s)
}

func path_escape(s string) string {
	return url.PathEscape(s)
}

func query_escape(s string) string {
	return url.QueryEscape(s)
}
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileContributor: Runxi Yu <https://runxiyu.org>

package main

import (
	"embed"
	"html/template"
	"io/fs"
	"net/http"
)

// We embed all source for easy AGPL compliance.
//
//go:embed .gitignore .gitattributes
//go:embed LICENSE README.md
//go:embed *.go go.mod go.sum
//go:embed *.scfg
//go:embed Makefile
//go:embed static/* templates/* scripts/* sql/*
//go:embed git_hooks_client/*.c
//go:embed vendor/*
var source_fs embed.FS

var source_handler = http.StripPrefix(
	"/:/source/",
	http.FileServer(http.FS(source_fs)),
)

//go:embed templates/* static/* git_hooks_client/git_hooks_client
var resources_fs embed.FS

var templates *template.Template

func load_templates() (err error) {
	templates, err = template.New("templates").Funcs(template.FuncMap{
		"first_line": first_line,
		"base_name":  base_name,
		"path_escape": path_escape,
		"query_escape": query_escape,
	}).ParseFS(resources_fs, "templates/*")
	return err
}

var static_handler http.Handler

func init() {
	static_fs, err := fs.Sub(resources_fs, "static")
	if err != nil {
		panic(err)
	}
	static_handler = http.StripPrefix("/:/static/", http.FileServer(http.FS(static_fs)))
}