teafolio/main.go

64 lines
1.3 KiB
Go

package main
import (
"flag"
"log"
"net/http"
"regexp"
"strings"
"github.com/BurntSushi/toml"
"golang.org/x/sync/semaphore"
)
type Config struct {
BindTo string
Gitea struct {
URL, Org string
MaxConnections int64
}
Redirect map[string]string
Template struct {
AppName string
HomepageHeaderHTML string
CustomLogoPngBase64 string
}
}
type Application struct {
cfg Config
rxRepoPage, rxRepoImage *regexp.Regexp
apiSem *semaphore.Weighted
}
func main() {
app := Application{
rxRepoPage: regexp.MustCompile(`^/([^/]+)/?$`),
rxRepoImage: regexp.MustCompile(`^/:banner/([^/]+)/?$`),
}
configFile := flag.String(`ConfigFile`, `config.toml`, `Configuration file in TOML format`)
flag.Parse()
_, err := toml.DecodeFile(*configFile, &app.cfg)
if err != nil {
log.Fatalf("toml.DecodeFile: %s", err.Error())
}
// Assert Gitea URL always has trailing slash
if !strings.HasSuffix(app.cfg.Gitea.URL, `/`) {
app.cfg.Gitea.URL += `/`
}
// Create semaphore
if app.cfg.Gitea.MaxConnections == 0 { // unlimited
app.apiSem = semaphore.NewWeighted(99999)
} else {
app.apiSem = semaphore.NewWeighted(app.cfg.Gitea.MaxConnections)
}
log.Printf("Starting web server on [%s]...", app.cfg.BindTo)
log.Fatal(http.ListenAndServe(app.cfg.BindTo, &app))
}