Lindenii Project Forge
Login
Commit info
ID342e92162ccf0ec4b8a421942bd8035e7edcfe04
AuthorRunxi Yu<me@runxiyu.org>
Author dateWed, 05 Feb 2025 02:35:12 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateWed, 05 Feb 2025 02:35:12 +0800
Actions
Get patch
*: Basic web server
/forge
package main

import (
	"bufio"
	"os"

	"go.lindenii.runxiyu.org/lindenii-common/scfg"
)

var config struct {
	HTTP struct {
		Net  string `scfg:"net"`
		Addr string `scfg:"addr"`
	} `scfg:"http"`
	DB struct {
		Type string `scfg:"type"`
		Conn string `scfg:"conn"`
	} `scfg:"db"`
}

func load_config(path string) (err error) {
	config_file, err := os.Open(path)
	if err != nil {
		return err
	}

	decoder := scfg.NewDecoder(bufio.NewReader(config_file))
	err = decoder.Decode(&config)
	if err != nil {
		return err
	}

	return nil
}
http {
	net tcp
	addr :8080
}
module go.lindenii.runxiyu.org/forge

go 1.23.5

require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250113062520-2daa71bfa256
go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250113062520-2daa71bfa256 h1:LCekcmEfZRfuuLMKUMT2TCgdXQRPIjxalibQIzHjzIo=
go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250113062520-2daa71bfa256/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA=
package main

import (
	"net/http"
)

func handle_index(w http.ResponseWriter, r *http.Request) {
}
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())
	}

	http.HandleFunc("/{$}", handle_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.StripPrefix(
			"/static/",
			http.FileServer(http.FS(static_fs)),
		),
	)
	return nil
}