forked from Nixius/authelia
1
0
Fork 0
ATLAS/docker/ss-atlas/internal/handlers/activate.go

73 lines
2.3 KiB
Go

package handlers
import (
"log"
"net/http"
)
func (a *App) handleActivateGet(w http.ResponseWriter, r *http.Request) {
acct, identity, err := a.currentAccount(r)
if err != nil || acct == nil {
data := map[string]any{
"IdentityURL": a.cfg.IdentityURL,
"AppURL": a.cfg.AppURL,
"NeedLogin": true,
}
a.tmpl.ExecuteTemplate(w, "activate.html", data)
return
}
if isSubscribedAccount(acct) {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
data := map[string]any{
"User": accountDisplay(acct, identity),
"AppURL": a.cfg.AppURL,
"Ready": true,
}
if err := a.tmpl.ExecuteTemplate(w, "activate.html", data); err != nil {
log.Printf("template error: %v", err)
http.Error(w, "internal error", http.StatusInternalServerError)
}
}
func (a *App) handleActivatePost(w http.ResponseWriter, r *http.Request) {
acct, _, err := a.currentAccount(r)
if err != nil || acct == nil {
http.Error(w, "not authenticated", http.StatusUnauthorized)
return
}
if isSubscribedAccount(acct) {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return
}
if acct.StripeCustomerID == "" {
http.Error(w, "no paid checkout found", http.StatusForbidden)
return
}
inst, err := a.accounts.InstanceByAccountID(r.Context(), acct.ID)
if err != nil {
log.Printf("activate: instance lookup failed for account %d: %v", acct.ID, err)
http.Error(w, "activation failed, contact support", http.StatusInternalServerError)
return
}
if err := a.accounts.MarkSubscriptionStatus(r.Context(), acct.StripeCustomerID, "active"); err != nil {
log.Printf("activate: subscription status update failed for account %d: %v", acct.ID, err)
}
if err := a.swarm.RestoreVolumes(inst.StackName, a.cfg.ArchivePath); err != nil {
log.Printf("activate: volume restore failed for %s: %v", inst.StackName, err)
}
if err := a.swarm.DeployStack(inst.StackName, inst.Slug, a.cfg.TraefikDomain); err != nil {
log.Printf("activate: stack deploy failed for %s: %v", inst.StackName, err)
} else if err := a.accounts.UpdateInstanceState(r.Context(), inst.StackName, "running", true); err != nil {
log.Printf("activate: state update failed for %s: %v", inst.StackName, err)
}
log.Printf("activated account %d stack=%s", acct.ID, inst.StackName)
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
}