teafolio/main.go

66 lines
1.4 KiB
Go
Raw Normal View History

2020-05-02 02:16:49 +00:00
package main
import (
"context"
2020-05-02 02:16:49 +00:00
"flag"
"log"
"net/http"
"regexp"
"sync"
2022-12-31 01:06:43 +00:00
"teafolio/gitea"
2020-05-02 02:16:49 +00:00
"github.com/BurntSushi/toml"
)
type Config struct {
BindTo string
Gitea struct {
URL, Org, Token string
MaxConnections int64
2020-05-02 02:16:49 +00:00
}
Redirect map[string]string
2020-05-02 02:16:49 +00:00
Template struct {
AppName string
HomepageHeaderHTML string
CustomLogoPngBase64 string
}
2022-12-31 01:18:47 +00:00
OverrideOrder map[string]int
2020-05-02 02:16:49 +00:00
}
type Application struct {
cfg Config
rxRepoPage, rxRepoImage *regexp.Regexp
2022-12-31 01:06:43 +00:00
gitea *gitea.APIClient
reposMut sync.RWMutex
2022-12-31 01:06:43 +00:00
reposCache []gitea.Repo // Sorted by recently-created-first
reposCacheByName map[string]int
reposAlphabeticalOrder map[string]int
2020-05-02 02:16:49 +00:00
}
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 {
2020-11-18 21:16:33 +00:00
log.Fatalf("toml.DecodeFile: %s", err.Error())
2020-05-02 02:16:49 +00:00
}
2022-12-31 01:06:43 +00:00
// Create Gitea API client
app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.Token, app.cfg.Gitea.MaxConnections)
// Sync worker
go app.syncWorker(context.Background())
log.Printf("Starting web server on [%s]...", app.cfg.BindTo)
2020-05-02 02:16:49 +00:00
log.Fatal(http.ListenAndServe(app.cfg.BindTo, &app))
}