repro-run/gitea-ci/main.go

70 lines
1.6 KiB
Go
Raw Normal View History

2023-01-23 00:58:44 +00:00
package main
import (
"log"
"net/http"
"os"
"strings"
"gitea.nulo.in/Nulo/repro-run/gitea-ci/gitea"
)
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)
http.Handle("/webhook", webhook{config: config, gitea: g})
http.Handle("/logs/socket/", logWebsocket{})
http.Handle("/", http.FileServer(http.Dir(".")))
log.Fatal(http.ListenAndServe(":8080", nil))
}
type config struct {
allowedUsers []string
giteaWebhookSecret string
giteaUrl string
giteaUsername string
giteaPassword string
2023-01-23 01:02:59 +00:00
publicUrl string
2023-01-23 00:58:44 +00:00
}
func parseConfig() (c config) {
var allowedUsersStr string
2023-01-23 01:22:12 +00:00
if allowedUsersStr = os.Getenv("ALLOWED_USERS"); len(allowedUsersStr) == 0 {
log.Fatal("ALLOWED_USERS is nil")
2023-01-23 00:58:44 +00:00
}
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")
}
2023-01-23 01:02:59 +00:00
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"
}
2023-01-23 00:58:44 +00:00
return
}