admin

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ApiFuncs = map[string]ApiFunc{
	"generate": Generate,
	"decrypt":  Decrypt,
}
View Source
var GetHttpHandle = func() http.Handler {
	root, _ := fs.Sub(Webfs, "dist")
	fileServer := http.FileServer(http.FS(root))
	indexHtml, _ := Webfs.ReadFile("dist/index.html")
	indexHtmlStr := string(indexHtml)
	variables := map[string]string{
		"ROOTPATH": flags.Rootpath,
		"PREFIX":   flags.Prefix,
		"ENV":      "production",
		"VERSION":  version.Version,
	}
	marker := "; //__"
	endmarker := "__"
	variableRegex := regexp.MustCompile(`".*?"` + regexp.QuoteMeta(marker) +
		`([A-Z][_A-Z0-9]*?)` + regexp.QuoteMeta(endmarker))
	indexHtmlStr = variableRegex.ReplaceAllStringFunc(indexHtmlStr, func(s string) string {
		i := strings.Index(s, marker)
		j := strings.LastIndex(s, endmarker)
		variable := s[i+len(marker) : j]
		if value, ok := variables[variable]; ok {
			return fmt.Sprintf(`%q%s`, value, s[i:])
		}
		return s
	})
	indexHtml = []byte(indexHtmlStr)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("WWW-Authenticate", `Basic realm="website"`)
		if flags.Cors {
			w.Header().Set("Access-Control-Allow-Origin", "*")
		}
		if flags.Pass != "" {
			username, password, ok := r.BasicAuth()
			if !ok || username != flags.User || password != flags.Pass {
				w.WriteHeader(401)
				w.Write([]byte("Unauthorized"))
				return
			}
		}
		path := strings.TrimPrefix(r.URL.Path, "/")
		f, err := root.Open(path)
		if err == nil {
			defer f.Close()
		}
		if path == "api" {
			apiHandler(w, r)
			return
		}
		if path == "" || os.IsNotExist(err) {
			w.Write(indexHtml)
			return
		}
		fileServer.ServeHTTP(w, r)
	})
}
View Source
var Webfs embed.FS

Functions

func Decrypt added in v0.11.0

func Decrypt(params url.Values) (any, error)

func Generate

func Generate(params url.Values) (data any, err error)

Types

type ApiFunc

type ApiFunc func(params url.Values) (data any, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL