Lindenii Project Forge
Login
Commit info
IDf34697bc9d6c9e25ca04b36f1782de90aa31571f
AuthorRunxi Yu<me@runxiyu.org>
Author dateSun, 09 Feb 2025 02:16:39 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateSun, 09 Feb 2025 02:16:39 +0800
Actions
Get patch
{main,resources}.go: Serve static/ properly
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())
	}

	http.HandleFunc("/{$}", handle_index)
	http.HandleFunc("/{project_name}/repos/{repo_name}/", handle_repo_index)

	listener, err := net.Listen(config.HTTP.Net, config.HTTP.Addr)
	if err != nil {
		clog.Fatal(1, "Listening: "+err.Error())
	}

	http.Serve(listener, nil)
}
package main

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

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

var templates *template.Template

func load_templates() (err error) {
	templates, err = template.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/",
	http.Handle("/static/{name}",
		http.StripPrefix(
			"/static/",
			http.FileServer(http.FS(static_fs)),
		),
	)
	return nil
}