git-svn-id: svn://losandesgames.com/alfheim-website@37 15359d88-9307-4e75-a9c1-e5686e5897df

This commit is contained in:
Vicente Ferrari Smith 2025-01-01 21:53:07 +00:00
parent 07cdba73e5
commit c911550021
4 changed files with 585 additions and 591 deletions

View File

@ -23,508 +23,508 @@ import "github.com/stripe/stripe-go/v78/webhook"
import "github.com/stripe/stripe-go/v78/customer" import "github.com/stripe/stripe-go/v78/customer"
import "github.com/stripe/stripe-go/v78/subscription" import "github.com/stripe/stripe-go/v78/subscription"
type templatedata struct { type TemplateData struct {
AuthenticatedUser int32 AuthenticatedUser int32
FormErrors map[string]string FormErrors map[string]string
Account Account Account Account
Prices []stripe.Price Prices []stripe.Price
ClientSecret string ClientSecret string
ActiveSubscription bool ActiveSubscription bool
} }
func favicon(w http.ResponseWriter, r *http.Request) { func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico") http.ServeFile(w, r, "favicon.ico")
} }
func authenticated_user(w http.ResponseWriter, r *http.Request) int32 { func authenticated_user(w http.ResponseWriter, r *http.Request) int32 {
session, err := store.Get(r, "id") session, err := store.Get(r, "id")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return 0 return 0
} }
id, ok := session.Values["id"].(int32) id, ok := session.Values["id"].(int32)
if !ok { if !ok {
trace := fmt.Sprintf("%s\n%s", errors.New("type assertion to int32 failed").Error(), debug.Stack()) trace := fmt.Sprintf("%s\n%s", errors.New("type assertion to int32 failed").Error(), debug.Stack())
_ = trace _ = trace
//log.Println(trace) //log.Println(trace)
return 0 return 0
} }
// check if the saved id exists in the database, otherwise it's a bad id and has to be removed from the cookies // check if the saved id exists in the database, otherwise it's a bad id and has to be removed from the cookies
exists := users.ExistsAccount(id) exists := users.ExistsAccount(id)
if !exists { if !exists {
session, _ := store.Get(r, "id") session, _ := store.Get(r, "id")
session.Values["id"] = 0 session.Values["id"] = 0
session.Save(r, w) session.Save(r, w)
return 0 return 0
} }
return id return id
} }
func home(w http.ResponseWriter, r *http.Request) { func home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
activesubscription := subscriptions.HasActiveSubscription(id) activesubscription := subscriptions.HasActiveSubscription(id)
text, err := template.ParseFiles("ui/base.html", "ui/index.html") text, err := template.ParseFiles("ui/base.html", "ui/index.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
} }
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, ActiveSubscription: activesubscription}) err := text.Execute(w, TemplateData{AuthenticatedUser: id, Account: account, ActiveSubscription: activesubscription})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
case http.MethodPost: case http.MethodPost:
err := text.Execute(w, templatedata{}) err := text.Execute(w, TemplateData{})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
} }
} }
func login(w http.ResponseWriter, r *http.Request) { func login(w http.ResponseWriter, r *http.Request) {
text, err := template.ParseFiles("ui/base.html", "ui/login.html") text, err := template.ParseFiles("ui/base.html", "ui/login.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
} }
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
err := text.Execute(w, templatedata{}) err := text.Execute(w, TemplateData{})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
case http.MethodPost: case http.MethodPost:
session, _ := store.Get(r, "id") session, _ := store.Get(r, "id")
password := r.FormValue("password") password := r.FormValue("password")
username := r.FormValue("username") username := r.FormValue("username")
errors := make(map[string]string) errors := make(map[string]string)
if strings.TrimSpace(username) == "" { if strings.TrimSpace(username) == "" {
errors["username"] = "This field cannot be blank" errors["username"] = "This field cannot be blank"
} else if utf8.RuneCountInString(username) > 20 { } else if utf8.RuneCountInString(username) > 20 {
errors["username"] = "This field is too long (the maximum is 20 characters)" errors["username"] = "This field is too long (the maximum is 20 characters)"
} }
if strings.TrimSpace(password) == "" { if strings.TrimSpace(password) == "" {
errors["password"] = "This field cannot be blank" errors["password"] = "This field cannot be blank"
} else if utf8.RuneCountInString(password) < 8 { } else if utf8.RuneCountInString(password) < 8 {
errors["password"] = "This field is too short (the minimum is 8 characters)" errors["password"] = "This field is too short (the minimum is 8 characters)"
} }
if len(errors) > 0 { if len(errors) > 0 {
err := text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors}) err := text.Execute(w, TemplateData{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
return return
} }
id, err := users.Authenticate(username, password) id, err := users.Authenticate(username, password)
if err == ErrInvalidCredentials { if err == ErrInvalidCredentials {
errors["generic"] = "Email or Password is incorrect" errors["generic"] = "Email or Password is incorrect"
err := text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors}) err := text.Execute(w, TemplateData{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
return return
} }
if id > 0 { if id > 0 {
session.Values["id"] = id session.Values["id"] = id
//log.Println("Logged in with id:", id) //log.Println("Logged in with id:", id)
session.Save(r, w) session.Save(r, w)
http.Redirect(w, r, "/account", http.StatusSeeOther) http.Redirect(w, r, "/account", http.StatusSeeOther)
} }
} }
} }
func logout(w http.ResponseWriter, r *http.Request) { func logout(w http.ResponseWriter, r *http.Request) {
text, err := template.ParseFiles("ui/base.html", "ui/logout.html") text, err := template.ParseFiles("ui/base.html", "ui/logout.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
} }
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
activesubscription := subscriptions.HasActiveSubscription(id) activesubscription := subscriptions.HasActiveSubscription(id)
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, ActiveSubscription: activesubscription}) err := text.Execute(w, TemplateData{AuthenticatedUser: id, Account: account, ActiveSubscription: activesubscription})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
case http.MethodPost: case http.MethodPost:
session, _ := store.Get(r, "id") session, _ := store.Get(r, "id")
session.Values["id"] = 0 session.Values["id"] = 0
session.Save(r, w) session.Save(r, w)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }
} }
func register(w http.ResponseWriter, r *http.Request) { func register(w http.ResponseWriter, r *http.Request) {
text, err := template.ParseFiles("ui/base.html", "ui/register.html") text, err := template.ParseFiles("ui/base.html", "ui/register.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
} }
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) err := text.Execute(w, TemplateData{AuthenticatedUser: id, Account: account})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
case http.MethodPost: case http.MethodPost:
account := Account{Username: r.FormValue("username"), Password: []byte(r.FormValue("password")), Firstname: r.FormValue("firstname"), Lastname: r.FormValue("lastname"), Email: r.FormValue("email")} account := Account{Username: r.FormValue("username"), Password: []byte(r.FormValue("password")), Firstname: r.FormValue("firstname"), Lastname: r.FormValue("lastname"), Email: r.FormValue("email")}
errors := make(map[string]string) errors := make(map[string]string)
if strings.TrimSpace(account.Username) == "" { if strings.TrimSpace(account.Username) == "" {
errors["username"] = "This field cannot be blank" errors["username"] = "This field cannot be blank"
} else if utf8.RuneCountInString(account.Username) > 20 { } else if utf8.RuneCountInString(account.Username) > 20 {
errors["username"] = "This field is too long (the maximum is 20 characters)" errors["username"] = "This field is too long (the maximum is 20 characters)"
} }
if strings.TrimSpace(string(account.Password)) == "" { if strings.TrimSpace(string(account.Password)) == "" {
errors["password"] = "This field cannot be blank" errors["password"] = "This field cannot be blank"
} else if utf8.RuneCountInString(string(account.Password)) < 8 { } else if utf8.RuneCountInString(string(account.Password)) < 8 {
errors["password"] = "This field is too short (the minimum is 8 characters)" errors["password"] = "This field is too short (the minimum is 8 characters)"
} }
if len(errors) > 0 { if len(errors) > 0 {
err := text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors}) err := text.Execute(w, TemplateData{AuthenticatedUser: authenticated_user(w, r), FormErrors: errors})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
} }
_, err := users.Insert(account.Username, string(account.Password), account.Firstname, account.Lastname, account.Email) _, err := users.Insert(account.Username, string(account.Password), account.Firstname, account.Lastname, account.Email)
if err == ErrDuplicateEmail || err == ErrDuplicateUsername { if err == ErrDuplicateEmail || err == ErrDuplicateUsername {
} else if err != nil { } else if err != nil {
} }
http.Redirect(w, r, "/login", http.StatusSeeOther) http.Redirect(w, r, "/login", http.StatusSeeOther)
} }
} }
func account(w http.ResponseWriter, r *http.Request) { func account(w http.ResponseWriter, r *http.Request) {
//id, err := strconv.Atoi(r.URL.Query().Get("id")) //id, err := strconv.Atoi(r.URL.Query().Get("id"))
//if err != nil || id < 1 { //if err != nil || id < 1 {
// http.NotFound(w, r) // http.NotFound(w, r)
// return // return
//} //}
//account, err := users.Get_account(int32(id)) //account, err := users.Get_account(int32(id))
//if err != nil { //if err != nil {
// log.Fatal(err) // log.Fatal(err)
//} //}
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
//log.Println(id, account) //log.Println(id, account)
text, err := template.ParseFiles("ui/base.html", "ui/account.html") text, err := template.ParseFiles("ui/base.html", "ui/account.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
} }
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) err := text.Execute(w, TemplateData{AuthenticatedUser: id, Account: account})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
} }
//case http.MethodPost: //case http.MethodPost:
// text.Execute(w, false) // text.Execute(w, false)
// if err != nil { // if err != nil {
// log.Fatal(err) // log.Fatal(err)
// http.Error(w, "Internal Server Error", 500) // http.Error(w, "Internal Server Error", 500)
// } // }
} }
func deleteaccount(w http.ResponseWriter, r *http.Request) { func deleteaccount(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r) id := authenticated_user(w, r)
switch r.Method { switch r.Method {
case http.MethodPost: case http.MethodPost:
log.Println("Deleting account with id ", id) log.Println("Deleting account with id ", id)
users.Delete(id) users.Delete(id)
session, _ := store.Get(r, "id") session, _ := store.Get(r, "id")
session.Values["id"] = 0 session.Values["id"] = 0
session.Save(r, w) session.Save(r, w)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }
} }
func subscribe(w http.ResponseWriter, r *http.Request) { func subscribe(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
params := &stripe.PriceListParams{} params := &stripe.PriceListParams{}
params.Limit = stripe.Int64(3) params.Limit = stripe.Int64(3)
params.AddExpand("data.product") params.AddExpand("data.product")
results := price.List(params) results := price.List(params)
prices := make([]stripe.Price, 0) prices := make([]stripe.Price, 0)
for results.Next() { for results.Next() {
//log.Println(results.Current()) //log.Println(results.Current())
prices = append(prices, *results.Price()) prices = append(prices, *results.Price())
} }
fm := template.FuncMap{ fm := template.FuncMap{
"divide": func(a, b float64) float64 { "divide": func(a, b float64) float64 {
return a / b return a / b
}, },
} }
text, err := template.New("base.html").Funcs(fm).ParseFiles("ui/base.html", "ui/subscribe.html") text, err := template.New("base.html").Funcs(fm).ParseFiles("ui/base.html", "ui/subscribe.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
return return
} }
err = text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, Prices: prices}) err = text.Execute(w, TemplateData{AuthenticatedUser: id, Account: account, Prices: prices})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
} }
func subscribe_stripe(w http.ResponseWriter, r *http.Request) { func subscribe_stripe(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
params := &stripe.CustomerSessionParams{ params := &stripe.CustomerSessionParams{
Customer: stripe.String(account.StripeID), Customer: stripe.String(account.StripeID),
Components: &stripe.CustomerSessionComponentsParams{ Components: &stripe.CustomerSessionComponentsParams{
PricingTable: &stripe.CustomerSessionComponentsPricingTableParams{ PricingTable: &stripe.CustomerSessionComponentsPricingTableParams{
Enabled: stripe.Bool(true), Enabled: stripe.Bool(true),
}, },
}, },
} }
result, err := customersession.New(params) result, err := customersession.New(params)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
text, err := template.ParseFiles("ui/base.html", "ui/subscribe_stripe.html") text, err := template.ParseFiles("ui/base.html", "ui/subscribe_stripe.html")
if err != nil { if err != nil {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
} }
err = text.Execute(w, templatedata{AuthenticatedUser: id, Account: account, ClientSecret: result.ClientSecret}) err = text.Execute(w, TemplateData{AuthenticatedUser: id, Account: account, ClientSecret: result.ClientSecret})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
} }
func managebilling(w http.ResponseWriter, r *http.Request) { func managebilling(w http.ResponseWriter, r *http.Request) {
id := authenticated_user(w, r) id := authenticated_user(w, r)
account, err := users.GetAccount(id) account, err := users.GetAccount(id)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
params := &stripe.BillingPortalSessionParams{ params := &stripe.BillingPortalSessionParams{
Customer: stripe.String(account.StripeID), Customer: stripe.String(account.StripeID),
ReturnURL: stripe.String("http://localhost:8080/account"), ReturnURL: stripe.String("http://localhost:8080/account"),
} }
result, err := session.New(params) result, err := session.New(params)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
http.Redirect(w, r, result.URL, http.StatusSeeOther) http.Redirect(w, r, result.URL, http.StatusSeeOther)
} }
func webhooks(w http.ResponseWriter, r *http.Request) { func webhooks(w http.ResponseWriter, r *http.Request) {
const MaxBodyBytes = int64(65536) const MaxBodyBytes = int64(65536)
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes) r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
payload, err := ioutil.ReadAll(r.Body) payload, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
log.Printf("Error reading request body: %v\n", err) log.Printf("Error reading request body: %v\n", err)
w.WriteHeader(http.StatusServiceUnavailable) w.WriteHeader(http.StatusServiceUnavailable)
return return
} }
endpointsecret := "whsec_43420f280f7695d9aa411c17da9ffac9afcecc3d36687035a8cb26f7f892f1cf" endpointsecret := "whsec_43420f280f7695d9aa411c17da9ffac9afcecc3d36687035a8cb26f7f892f1cf"
event, err := webhook.ConstructEvent(payload, r.Header.Get("Stripe-Signature"), endpointsecret) event, err := webhook.ConstructEvent(payload, r.Header.Get("Stripe-Signature"), endpointsecret)
if err != nil { if err != nil {
log.Printf("Error verifying webhook signature: %v\n", err) log.Printf("Error verifying webhook signature: %v\n", err)
w.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature w.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature
return return
} }
err = json.Unmarshal(payload, &event) err = json.Unmarshal(payload, &event)
if err != nil { if err != nil {
log.Printf("Failed to parse webhook body json: %v\n", err.Error()) log.Printf("Failed to parse webhook body json: %v\n", err.Error())
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
// Unmarshal the event data into an appropriate struct depending on its Type // Unmarshal the event data into an appropriate struct depending on its Type
switch event.Type { switch event.Type {
case "checkout.session.completed": case "checkout.session.completed":
var checkoutSession stripe.CheckoutSession var checkoutSession stripe.CheckoutSession
err := json.Unmarshal(event.Data.Raw, &checkoutSession) err := json.Unmarshal(event.Data.Raw, &checkoutSession)
if err != nil { if err != nil {
log.Printf("Error parsing webhook JSON: %v\n", err) log.Printf("Error parsing webhook JSON: %v\n", err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
// Then define and call a func to handle the successful payment intent. // Then define and call a func to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent) // handlePaymentIntentSucceeded(paymentIntent)
handle_checkout_session_completed(checkoutSession) handle_checkout_session_completed(checkoutSession)
case "payment_intent.succeeded": case "payment_intent.succeeded":
var paymentIntent stripe.PaymentIntent var paymentIntent stripe.PaymentIntent
err := json.Unmarshal(event.Data.Raw, &paymentIntent) err := json.Unmarshal(event.Data.Raw, &paymentIntent)
if err != nil { if err != nil {
log.Printf("Error parsing webhook JSON: %v\n", err) log.Printf("Error parsing webhook JSON: %v\n", err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
// Then define and call a func to handle the successful payment intent. // Then define and call a func to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent) // handlePaymentIntentSucceeded(paymentIntent)
case "payment_method.attached": case "payment_method.attached":
var paymentMethod stripe.PaymentMethod var paymentMethod stripe.PaymentMethod
err := json.Unmarshal(event.Data.Raw, &paymentMethod) err := json.Unmarshal(event.Data.Raw, &paymentMethod)
if err != nil { if err != nil {
log.Printf("Error parsing webhook JSON: %v\n", err) log.Printf("Error parsing webhook JSON: %v\n", err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
case "setup_intent.succeeded": case "setup_intent.succeeded":
var setupIntent stripe.SetupIntent var setupIntent stripe.SetupIntent
err := json.Unmarshal(event.Data.Raw, &setupIntent) err := json.Unmarshal(event.Data.Raw, &setupIntent)
if err != nil { if err != nil {
log.Printf("Error parsing webhook JSON: %v\n", err) log.Printf("Error parsing webhook JSON: %v\n", err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
// ... handle other event types // ... handle other event types
default: default:
log.Printf("Unhandled event type: %s\n", event.Type) log.Printf("Unhandled event type: %s\n", event.Type)
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func handle_checkout_session_completed(checkoutsession stripe.CheckoutSession) error { func handle_checkout_session_completed(checkoutsession stripe.CheckoutSession) error {
//toprint, _ := json.MarshalIndent(checkoutSession, "", " ") //toprint, _ := json.MarshalIndent(checkoutSession, "", " ")
//log.Println(string(toprint)) //log.Println(string(toprint))
subscription, err := subscription.Get(checkoutsession.Subscription.ID, nil); subscription, err := subscription.Get(checkoutsession.Subscription.ID, nil);
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return err return err
} }
//log.Println(subscription.Customer.ID) //log.Println(subscription.Customer.ID)
//var status SubscriptionStatus //var status SubscriptionStatus
//switch subscription.Status { //switch subscription.Status {
// case "incomplete": // case "incomplete":
// status = incomplete // status = incomplete
// case "incomplete_expired": // case "incomplete_expired":
// status = incomplete_expired // status = incomplete_expired
// case "trialing": // case "trialing":
// status = trialing // status = trialing
// case "active": // case "active":
// status = active // status = active
// case "past_due": // case "past_due":
// status = past_due // status = past_due
// case "canceled": // case "canceled":
// status = canceled // status = canceled
// case "unpaid": // case "unpaid":
// status = unpaid // status = unpaid
// case "paused": // case "paused":
// status = paused // status = paused
//} //}
subscriptions.Insert(checkoutsession.Customer.ID, subscription.ID, checkoutsession.ID, subscription.Status) subscriptions.Insert(checkoutsession.Customer.ID, subscription.ID, checkoutsession.ID, subscription.Status)
return nil return nil
} }
func handle_payment_method_attached(paymentmethod stripe.PaymentMethod) error { func handle_payment_method_attached(paymentmethod stripe.PaymentMethod) error {
//toprint, _ := json.MarshalIndent(setupintent, "", " ") //toprint, _ := json.MarshalIndent(setupintent, "", " ")
//log.Println(string(toprint)) //log.Println(string(toprint))
// make this the new customer's default payment method // make this the new customer's default payment method
params := &stripe.CustomerParams{} params := &stripe.CustomerParams{}
params.DefaultSource = &paymentmethod.ID params.DefaultSource = &paymentmethod.ID
/*result*/_, err := customer.Update(paymentmethod.Customer.ID, params) /*result*/_, err := customer.Update(paymentmethod.Customer.ID, params)
//log.Println(result) //log.Println(result)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return err return err
} }
return nil return nil
} }

112
main.go
View File

@ -23,77 +23,77 @@ var store = sessions.NewCookieStore(key)
var emailrx = regexp.MustCompile("/^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/") var emailrx = regexp.MustCompile("/^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/")
func main() { func main() {
addr := flag.String("addr", "127.0.0.1:8080", "HTTP network addr") addr := flag.String("addr", "127.0.0.1:8080", "HTTP network addr")
prodaddr := flag.String("prodaddr", "45.76.84.7:443", "HTTP network addr") prodaddr := flag.String("prodaddr", "45.76.84.7:443", "HTTP network addr")
production := flag.Bool("production", false, "Whether to use production port and TLS") production := flag.Bool("production", false, "Whether to use production port and TLS")
_ = addr _ = addr
flag.Parse() flag.Parse()
log.Println("Hello, Sailor!") log.Println("Hello, Sailor!")
stripe.Key = "sk_test_51PGebgKUHKCjyTmc97rfDPcvew6EhqDz2qp3U7XoAMIilAU9IVo2NO4P7ylkTvbBafFVr94trha1VYY32jRWMw2K00Yq7YJXFf" stripe.Key = "sk_test_51PGebgKUHKCjyTmc97rfDPcvew6EhqDz2qp3U7XoAMIilAU9IVo2NO4P7ylkTvbBafFVr94trha1VYY32jRWMw2K00Yq7YJXFf"
store.MaxAge(0) store.MaxAge(0)
db, err := sql.Open("postgres", "postgres://elves_database:iK2SoVbDhdCki5n3LxGyP6zKpLspt4@80.240.25.87/elves_database") db, err := sql.Open("postgres", "postgres://elves_database:iK2SoVbDhdCki5n3LxGyP6zKpLspt4@80.240.25.87/elves_database")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer db.Close() defer db.Close()
users = &Usermodel{db} users = &Usermodel{db}
subscriptions = &SubscriptionModel{db} subscriptions = &SubscriptionModel{db}
mux := http.NewServeMux() mux := http.NewServeMux()
//rows, err := db.Query("SELECT * FROM accounts") //rows, err := db.Query("SELECT * FROM accounts")
//if err != nil { //if err != nil {
// log.Fatal(err) // log.Fatal(err)
//} //}
//defer rows.Close() //defer rows.Close()
//accounts := make([]*Account, 0) //accounts := make([]*Account, 0)
//for rows.Next() { //for rows.Next() {
// acc := new(Account) // acc := new(Account)
// err := rows.Scan(&acc.id, &acc.Username, &acc.password, &acc.Color) // err := rows.Scan(&acc.id, &acc.Username, &acc.password, &acc.Color)
// if err != nil { // if err != nil {
// log.Fatal(err) // log.Fatal(err)
// } // }
// accounts = append(accounts, acc) // accounts = append(accounts, acc)
//} //}
//if err = rows.Err() err != nil { //if err = rows.Err() err != nil {
// log.Fatal(err) // log.Fatal(err)
//} //}
//for _, acc := range accounts { //for _, acc := range accounts {
// log.Println(acc) // log.Println(acc)
//} //}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
mux.HandleFunc("/favicon.ico", favicon) mux.HandleFunc("/favicon.ico", favicon)
mux.HandleFunc("/", home) mux.HandleFunc("/", home)
mux.HandleFunc("/login", login) mux.HandleFunc("/login", login)
mux.HandleFunc("/logout", logout) mux.HandleFunc("/logout", logout)
mux.HandleFunc("/register", register) mux.HandleFunc("/register", register)
mux.HandleFunc("/account", require_authenticated_user(account)) mux.HandleFunc("/account", require_authenticated_user(account))
mux.HandleFunc("/deleteaccount", require_authenticated_user(deleteaccount)) mux.HandleFunc("/deleteaccount", require_authenticated_user(deleteaccount))
mux.HandleFunc("/subscribe", require_authenticated_user(subscribe_stripe)) mux.HandleFunc("/subscribe", require_authenticated_user(subscribe_stripe))
mux.HandleFunc("/managebilling", require_authenticated_user(managebilling)) mux.HandleFunc("/managebilling", require_authenticated_user(managebilling))
mux.HandleFunc("/webhook", webhooks) mux.HandleFunc("/webhook", webhooks)
if *production { if *production {
server := &http.Server{ server := &http.Server{
Addr: *prodaddr, Addr: *prodaddr,
Handler: log_connection(secure_headers(mux)), Handler: log_connection(secure_headers(mux)),
} }
log.Fatal(server.ListenAndServeTLS("/home/alfheim/cert/config/live/alfheimgame.com/fullchain.pem", "/home/alfheim/cert/config/live/alfheimgame.com/privkey.pem")) log.Fatal(server.ListenAndServeTLS("/home/alfheim/cert/config/live/alfheimgame.com/fullchain.pem", "/home/alfheim/cert/config/live/alfheimgame.com/privkey.pem"))
} else { } else {
log.Fatal(http.ListenAndServe(*addr, log_connection(secure_headers(mux)))) log.Fatal(http.ListenAndServe(*addr, log_connection(secure_headers(mux))))
} }
} }

View File

@ -1,201 +1,201 @@
html { html {
height: 100%; height: 100%;
padding: 8px; padding: 8px;
/*background: no-repeat url(/static/image.png); /*background: no-repeat url(/static/image.png);
background-size: cover; background-size: cover;
background-position: center;*/ background-position: center;*/
background-color: black; background-color: black;
box-sizing: border-box; box-sizing: border-box;
} }
body { body {
background-color: #3475CB; background-color: #3475CB;
font-family: "Vollkorn"; font-family: "Vollkorn";
color: white; color: white;
margin-top: 0px; margin-top: 0px;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
margin-bottom: 0px; margin-bottom: 0px;
min-height: 100%; display: flex;
max-width: 900px; flex-direction: column;
min-height: 100%;
max-width: 900px;
} }
header { header {
max-width: 100%; max-width: 100%;
} }
nav { nav {
padding-left: 8px; padding-left: 8px;
padding-right: 8px; padding-right: 8px;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
} }
.navbuttons a { .navbuttons a {
display: inline-block; display: inline-block;
} }
.maintitle { .maintitle {
margin: 0px; margin: 0px;
font-size: 6rem; font-size: 6rem;
color: white; color: white;
} }
@media (max-width: 1080px) { @media (max-width: 1080px) {
.maintitle { .maintitle {
font-size: 4rem; font-size: 4rem;
} }
} }
.loginbutton { .loginbutton {
align-self: center; align-self: center;
display: flex; display: flex;
border: 12px solid; border: 12px solid;
border-image-source: url("/static/panel-000.png"); border-image-source: url("/static/panel-000.png");
border-image-slice: 12 fill; border-image-slice: 12 fill;
padding-top: 15px; padding-top: 15px;
padding-right: 20px; padding-right: 20px;
padding-bottom: 15px; padding-bottom: 15px;
padding-left: 20px; padding-left: 20px;
color: black; color: black;
/*box-shadow: 10px 10px 5px lightblue;*/ /*box-shadow: 10px 10px 5px lightblue;*/
} }
main { main {
margin: 0px; margin: 0px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
min-height: 100%; min-height: 100%;
max-height: 100%; max-height: 100%;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
} }
p { p {
text-align: center; text-align: center;
font-size: 1rem; font-size: 1rem;
} }
.account-wrapper { .account-wrapper {
background: transparent; background: transparent;
border: 2px solid white; border: 2px solid white;
backdrop-filter: blur(20px); backdrop-filter: blur(20px);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 15px; border-radius: 15px;
padding: 30px 40px; padding: 30px 40px;
font-size: 1.5rem; font-size: 1.5rem;
} }
.wrapper { .wrapper {
display: inline-block; display: inline-block;
background: transparent; background: transparent;
border: 2px solid white; border: 2px solid white;
backdrop-filter: blur(20px); backdrop-filter: blur(20px);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 15px; border-radius: 15px;
padding: 30px 40px; padding: 30px 40px;
width: 80%; width: 80%;
text-align: center; text-align: center;
} }
.wrapper h1 { .wrapper h1 {
font-size: 1.5rem; font-size: 1.5rem;
text-align: center; text-align: center;
} }
.wrapper .input-box { .wrapper .input-box {
width: 100%; width: 100%;
} }
.input-box input { .input-box input {
box-sizing: border-box; box-sizing: border-box;
background: transparent; background: transparent;
width: 100%; width: 100%;
height: 100%; height: 100%;
outline: none; outline: none;
border: 2px solid rgba(255, 255, 255, .2); border: 2px solid rgba(255, 255, 255, .2);
border-radius: 25px; border-radius: 25px;
padding: 10px 45px 10px 20px padding: 10px 45px 10px 20px
} }
.input-box input::placeholder { .input-box input::placeholder {
color: white; color: white;
} }
.wrapper .input-error { .wrapper .input-error {
width: 100%; width: 100%;
} }
.error { .error {
color: rgba(240, 0, 0, .8); color: rgba(240, 0, 0, .8);
} }
.input-error input { .input-error input {
box-sizing: border-box; box-sizing: border-box;
background: transparent; background: transparent;
width: 100%; width: 100%;
height: 100%; height: 100%;
outline: none; outline: none;
border: 2px solid rgba(255, 0, 0, .2); border: 2px solid rgba(255, 0, 0, .2);
border-radius: 25px; border-radius: 25px;
padding: 10px 45px 10px 20px padding: 10px 45px 10px 20px
} }
.input-error input::placeholder { .input-error input::placeholder {
color: red; color: red;
} }
.login-btn-wrapper { .login-btn-wrapper {
display: flex; display: flex;
justify-content: center; justify-content: center;
} }
.wrapper .btn { .wrapper .btn {
width: 100%; width: 100%;
outline: none; outline: none;
border: none; border: none;
border-radius: 20px; border-radius: 20px;
} }
.wrapper .register-link { .wrapper .register-link {
display: flex; display: flex;
justify-content: center; justify-content: center;
} }
input { input {
font-family: inherit; font-family: inherit;
} }
footer { footer {
position: relative; margin-top: auto;
left: 0; width: 100%;
bottom: 0; background-color: #132123;
width: 100%; color: white;
background-color: #132123; text-align: center;
color: white;
text-align: center;
} }
a:link { a:link {
text-decoration: none; text-decoration: none;
} }
.wrapper a:link { .wrapper a:link {
text-decoration: none; text-decoration: none;
} }
.wrapper a:visited { .wrapper a:visited {
text-decoration: none; text-decoration: none;
} }
.wrapper a:hover { .wrapper a:hover {
text-decoration: underline; text-decoration: underline;
} }
video { video {
width: 100%; width: 100%;
height: auto; height: auto;
} }

View File

@ -1,54 +1,48 @@
<!DOCTYPE html> <!DOCTYPE html>
<!--
/\ | |--- | | |--- || |\ /|
/__\ | |-- |---| |-- || | \ / |
/ \ |___ | | | |___ || | \/ |
-->
<html lang="en"> <html lang="en">
<head> <head>
<title>Alfheim</title> <title>Alfheim</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="description" content="A handcrafted economy and politics MMO." /> <meta name="description" content="A handcrafted economy and politics MMO." />
<meta name="author" content="Vicente Ferrari Smith" /> <meta name="author" content="Vicente Ferrari Smith" />
<meta name="keywords" content="Alfheim, indie, video game, mmo, colony, colony simulator, vicente ferrari smith, game, economy, politics, alfheim" /> <meta name="keywords" content="Alfheim, indie, video game, mmo, colony, colony simulator, vicente ferrari smith, game, economy, politics, alfheim" />
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<style> <style>
@font-face { @font-face {
font-family: "Vollkorn"; font-family: "Vollkorn";
src: url(/static/Vollkorn-VariableFont_wght.ttf); src: url(/static/Vollkorn-VariableFont_wght.ttf);
} }
</style> </style>
<link href="/static/style.css" rel="stylesheet" /> <link href="/static/style.css" rel="stylesheet" />
</head> </head>
<body> <body>
<header> <header>
<nav> <nav>
<a href="/"><h1 class="maintitle">Alfheim</h1></a> <a href="/"><h1 class="maintitle">Alfheim</h1></a>
<div class="navbuttons"> <div class="navbuttons">
{{if not .ActiveSubscription}} {{if not .ActiveSubscription}}
<a href="/subscribe"><div class="loginbutton"><strong>Subscribe</strong></div></a> <a href="/subscribe"><div class="loginbutton"><strong>Subscribe</strong></div></a>
{{end}} {{end}}
{{if .AuthenticatedUser}} {{if .AuthenticatedUser}}
<a href="/account"><div class="loginbutton"><strong>Account</strong></div></a> <a href="/account"><div class="loginbutton"><strong>Account</strong></div></a>
<a href="/logout"><div class="loginbutton"><strong>Log out</strong></div></a> <a href="/logout"><div class="loginbutton"><strong>Log out</strong></div></a>
{{else}} {{else}}
<a href="/login"><div class="loginbutton"><strong>Log in</strong><img src="/static/login_24dp_FILL0_wght400_GRAD0_opsz24.svg" alt=""/></div></a> <a href="/login"><div class="loginbutton"><strong>Log in</strong><img src="/static/login_24dp_FILL0_wght400_GRAD0_opsz24.svg" alt=""/></div></a>
<a href="/register"><div class="loginbutton"><strong>Register</strong></div></a> <a href="/register"><div class="loginbutton"><strong>Register</strong></div></a>
{{end}} {{end}}
</div> </div>
</nav> </nav>
</header> </header>
<main> <main>
{{template "body" .}} {{template "body" .}}
</main> </main>
<footer> <footer>
Alfheim &copy; 2024, Vicente Ferrari Smith, Los Andes Games, Vienna, Austria. All rights reserved. Alfheim &copy; 2025, Vicente Ferrari Smith, Los Andes Games, Vienna, Austria. All rights reserved.
</footer> </footer>
</body> </body>
</html> </html>