2020-05-02 02:16:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-18 22:57:17 +00:00
|
|
|
"context"
|
2020-05-02 02:16:49 +00:00
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-11-18 22:57:17 +00:00
|
|
|
"sync"
|
2020-05-02 02:16:49 +00:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2020-05-24 06:39:24 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
2020-05-02 02:16:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
BindTo string
|
|
|
|
Gitea struct {
|
2020-05-24 06:39:24 +00:00
|
|
|
URL, Org string
|
|
|
|
MaxConnections int64
|
2020-05-02 02:16:49 +00:00
|
|
|
}
|
2020-05-05 07:30:28 +00:00
|
|
|
Redirect map[string]string
|
2020-05-02 02:16:49 +00:00
|
|
|
Template struct {
|
|
|
|
AppName string
|
|
|
|
HomepageHeaderHTML string
|
|
|
|
CustomLogoPngBase64 string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
cfg Config
|
|
|
|
|
|
|
|
rxRepoPage, rxRepoImage *regexp.Regexp
|
2020-05-24 06:39:24 +00:00
|
|
|
apiSem *semaphore.Weighted
|
2020-11-18 22:57:17 +00:00
|
|
|
|
|
|
|
reposMut sync.RWMutex
|
|
|
|
reposCache []Repo // Sorted by recently-created-first
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Assert Gitea URL always has trailing slash
|
|
|
|
if !strings.HasSuffix(app.cfg.Gitea.URL, `/`) {
|
|
|
|
app.cfg.Gitea.URL += `/`
|
|
|
|
}
|
|
|
|
|
2020-05-24 06:39:24 +00:00
|
|
|
// Create semaphore
|
|
|
|
if app.cfg.Gitea.MaxConnections == 0 { // unlimited
|
|
|
|
app.apiSem = semaphore.NewWeighted(99999)
|
|
|
|
} else {
|
|
|
|
app.apiSem = semaphore.NewWeighted(app.cfg.Gitea.MaxConnections)
|
|
|
|
}
|
|
|
|
|
2020-11-18 22:57:17 +00:00
|
|
|
// Sync worker
|
|
|
|
go app.syncWorker(context.Background())
|
|
|
|
|
2020-11-18 22:02:36 +00:00
|
|
|
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))
|
|
|
|
}
|