repro-run/gitea-ci/main.go

87 lines
2.0 KiB
Go

package main
import (
"log"
"net/http"
"os"
"strings"
"gitea.nulo.in/Nulo/repro-run/gitea-ci/gitea"
_ "embed"
)
//go:embed index.html
var indexHtml []byte
//go:embed run.html
var runHtml []byte
func main() {
config := parseConfig()
g := gitea.Gitea{
Url: config.giteaUrl,
Username: config.giteaUsername,
Password: config.giteaPassword,
}
u, err := g.GetUser()
if err != nil {
log.Fatal(err)
}
log.Printf("Logged in as @%s", u.Login)
port := ":8080"
if len(os.Args) > 1 {
port = os.Args[1]
}
http.Handle("/webhook", webhook{config: config, gitea: g})
http.Handle("/logs/socket/", logWebsocket{})
http.Handle("/", staticRoute{file: indexHtml, mime: "text/html; charset=utf-8"})
http.Handle("/run", staticRoute{file: runHtml, mime: "text/html; charset=utf-8"})
log.Fatal(http.ListenAndServe(port, nil))
}
type config struct {
allowedUsers []string
giteaWebhookSecret string
giteaUrl string
giteaUsername string
giteaPassword string
publicUrl string
doNotDeleteTmp bool
}
func parseConfig() (c config) {
var allowedUsersStr string
if allowedUsersStr = os.Getenv("ALLOWED_USERS"); len(allowedUsersStr) == 0 {
log.Fatal("ALLOWED_USERS is nil")
}
c.allowedUsers = strings.Split(allowedUsersStr, ",")
if c.giteaWebhookSecret = os.Getenv("GITEA_WEBHOOK_SECRET"); len(c.giteaWebhookSecret) == 0 {
log.Fatal("GITEA_WEBHOOK_SECRET is nil")
}
if c.giteaUrl = os.Getenv("GITEA_URL"); len(c.giteaUrl) == 0 {
log.Fatal("GITEA_URL is nil")
}
if c.giteaUsername = os.Getenv("GITEA_USERNAME"); len(c.giteaUsername) == 0 {
log.Fatal("GITEA_USERNAME is nil")
}
if c.giteaPassword = os.Getenv("GITEA_PASSWORD"); len(c.giteaPassword) == 0 {
log.Fatal("GITEA_PASSWORD is nil")
}
if c.publicUrl = os.Getenv("PUBLIC_URL"); len(c.publicUrl) == 0 {
log.Println("PUBLIC_URL is not defined, will default to localhost:8080")
c.publicUrl = "http://localhost:8080"
}
c.doNotDeleteTmp = os.Getenv("DO_NOT_DELETE_TMP") == "true"
return
}