2021-05-02 22:49:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-23 18:40:51 +00:00
|
|
|
"errors"
|
2021-05-04 12:28:48 +00:00
|
|
|
"fmt"
|
2021-06-23 18:40:51 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2021-05-04 12:28:48 +00:00
|
|
|
"time"
|
2021-05-02 22:49:16 +00:00
|
|
|
|
2021-05-04 12:28:48 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-05-02 22:49:16 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-06-16 15:29:01 +00:00
|
|
|
"goauthentik.io/internal/common"
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-05-04 12:28:48 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/gounicorn"
|
2021-06-23 18:40:51 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
|
|
|
"goauthentik.io/internal/outpost/proxy"
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-05-04 12:07:12 +00:00
|
|
|
log.SetLevel(log.DebugLevel)
|
2021-05-02 22:49:16 +00:00
|
|
|
config.DefaultConfig()
|
2021-05-04 12:07:12 +00:00
|
|
|
config.LoadConfig("./authentik/lib/default.yml")
|
|
|
|
config.LoadConfig("./local.env.yml")
|
2021-05-02 22:49:16 +00:00
|
|
|
config.ConfigureLogger()
|
|
|
|
|
2021-05-04 12:28:48 +00:00
|
|
|
if config.G.ErrorReporting.Enabled {
|
|
|
|
sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8",
|
|
|
|
AttachStacktrace: true,
|
|
|
|
TracesSampleRate: 0.6,
|
|
|
|
Release: fmt.Sprintf("authentik@%s", constants.VERSION),
|
|
|
|
Environment: config.G.ErrorReporting.Environment,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-23 18:40:51 +00:00
|
|
|
ex := common.Init()
|
2021-06-16 15:29:01 +00:00
|
|
|
defer common.Defer()
|
|
|
|
|
2021-06-23 18:40:51 +00:00
|
|
|
u, _ := url.Parse("http://localhost:8000")
|
|
|
|
|
|
|
|
for {
|
2021-05-02 22:49:16 +00:00
|
|
|
g := gounicorn.NewGoUnicorn()
|
2021-06-23 18:40:51 +00:00
|
|
|
go attemptStartBackend(g)
|
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
ws := web.NewWebServer()
|
|
|
|
ws.Run()
|
2021-06-23 18:40:51 +00:00
|
|
|
|
|
|
|
proxy :=
|
|
|
|
|
|
|
|
<-ex
|
|
|
|
ws.Stop()
|
|
|
|
g.Kill()
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
maxTries := 100
|
|
|
|
attempt := 0
|
|
|
|
for {
|
|
|
|
err := attemptProxyStart(u, ex)
|
|
|
|
if err != nil {
|
|
|
|
attempt += 1
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
if attempt > maxTries {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
}()
|
2021-06-23 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func attemptStartBackend(g *gounicorn.GoUnicorn) error {
|
|
|
|
for {
|
|
|
|
err := g.Start()
|
|
|
|
log.WithField("logger", "authentik.g").WithError(err).Warning("gunicorn process died, restarting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func attemptProxyStart(u *url.URL, exitSignal chan os.Signal) (*ak.APIController, error) {
|
|
|
|
ac := ak.NewAPIController(*u, config.G.SecretKey)
|
|
|
|
if ac == nil {
|
|
|
|
return nil, errors.New("failed to start")
|
|
|
|
}
|
|
|
|
ac.Server = proxy.NewServer(ac)
|
|
|
|
return ac, nil
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|