Hi… I am well aware that this diff view is very suboptimal. It will be fixed when the refactored server comes along!
resources: Serve source and static properly ... except that it won't run because of URL conflicts for now
package main
import (
"flag"
"net"
"net/http"
"go.lindenii.runxiyu.org/lindenii-common/clog"
)
func main() {
config_path := flag.String(
"config",
"/etc/lindenii/forge.scfg",
"path to configuration file",
)
flag.Parse()
err := load_config(*config_path)
if err != nil {
clog.Fatal(1, "Loading configuration: "+err.Error())
}
err = load_templates()
if err != nil {
clog.Fatal(1, "Loading templates: "+err.Error())
}
err = serve_static()
if err != nil {
clog.Fatal(1, "Serving static: "+err.Error())
}
serve_source()
http.HandleFunc("/{$}", handle_index)
http.HandleFunc("/{category_name}/repos/{$}", handle_category_repos)
http.HandleFunc("/{category_name}/repos/{repo_name}/{$}", handle_repo_index)
http.HandleFunc("/{category_name}/repos/{repo_name}/tree/{ref}/{rest...}", handle_repo_tree)
listener, err := net.Listen(config.HTTP.Net, config.HTTP.Addr)
if err != nil {
clog.Fatal(1, "Listening: "+err.Error())
}
err = http.Serve(listener, nil)
if err != nil {
clog.Fatal(1, "Serving: "+err.Error())
}
}
package main import ( "embed" "html/template" "io/fs" "net/http" )
//go:embed .gitignore LICENSE README.md
//go:embed *.go go.mod go.sum
//go:embed *.scfg
//go:embed static/* templates/*
var source_fs embed.FS
func serve_source() {
http.Handle("/source/",
http.StripPrefix(
"/source/",
http.FileServer(http.FS(source_fs)),
),
)
}
//go:embed templates/* static/*
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,
}).ParseFS(resources_fs, "templates/*")
return err
}
func serve_static() (err error) {
static_fs, err := fs.Sub(resources_fs, "static")
if err != nil {
return err
}
http.Handle("/static/{name}",
http.Handle("/static/",
http.StripPrefix( "/static/", http.FileServer(http.FS(static_fs)), ), ) return nil }