shop.tonybtw.com

shop.tonybtw.com

https://git.tonybtw.com/shop.tonybtw.com.git git://git.tonybtw.com/shop.tonybtw.com.git
2,559 bytes raw
1
package main
2
3
import (
4
	"fmt"
5
	"log"
6
	"net/http"
7
	"os"
8
	"regexp"
9
10
	"shop.tonybtw.com/internal/handlers"
11
	"shop.tonybtw.com/internal/lib"
12
)
13
14
type Route struct {
15
	method  string
16
	pattern *regexp.Regexp
17
	handler http.HandlerFunc
18
}
19
20
func main() {
21
	db, err := lib.Connect_DB()
22
	if err != nil {
23
		log.Fatal("Failed to connect to database:", err)
24
	}
25
	defer db.Close()
26
27
	ctx := lib.New_App_Context(db)
28
29
	shop_handler := handlers.New_Shop_Handler(ctx)
30
	cart_handler := handlers.New_Cart_Handler(ctx)
31
	checkout_handler := handlers.New_Checkout_Handler(ctx)
32
	webhook_handler := handlers.New_Webhook_Handler(ctx)
33
34
	routes := []Route{
35
		{
36
			method:  "GET",
37
			pattern: regexp.MustCompile(`^/$`),
38
			handler: shop_handler.Show_Home,
39
		},
40
		{
41
			method:  "GET",
42
			pattern: regexp.MustCompile(`^/product/([^/]+)$`),
43
			handler: func(w http.ResponseWriter, r *http.Request) {
44
				matches := regexp.MustCompile(`^/product/([^/]+)$`).FindStringSubmatch(r.URL.Path)
45
				if len(matches) > 1 {
46
					shop_handler.Show_Product(w, r, matches[1])
47
				}
48
			},
49
		},
50
		{
51
			method:  "GET",
52
			pattern: regexp.MustCompile(`^/cart$`),
53
			handler: cart_handler.Show_Cart,
54
		},
55
		{
56
			method:  "POST",
57
			pattern: regexp.MustCompile(`^/cart/add$`),
58
			handler: cart_handler.Add_Item,
59
		},
60
		{
61
			method:  "POST",
62
			pattern: regexp.MustCompile(`^/cart/update$`),
63
			handler: cart_handler.Update_Item,
64
		},
65
		{
66
			method:  "POST",
67
			pattern: regexp.MustCompile(`^/cart/remove$`),
68
			handler: cart_handler.Remove_Item,
69
		},
70
		{
71
			method:  "GET",
72
			pattern: regexp.MustCompile(`^/checkout$`),
73
			handler: checkout_handler.Show_Checkout,
74
		},
75
		{
76
			method:  "POST",
77
			pattern: regexp.MustCompile(`^/checkout/create$`),
78
			handler: lib.Require_CSRF(checkout_handler.Create_Session),
79
		},
80
		{
81
			method:  "GET",
82
			pattern: regexp.MustCompile(`^/success$`),
83
			handler: checkout_handler.Show_Success,
84
		},
85
		{
86
			method:  "POST",
87
			pattern: regexp.MustCompile(`^/webhook/stripe$`),
88
			handler: webhook_handler.Handle_Stripe,
89
		},
90
	}
91
92
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("public/static"))))
93
94
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
95
		for _, route := range routes {
96
			if r.Method == route.method && route.pattern.MatchString(r.URL.Path) {
97
				route.handler(w, r)
98
				return
99
			}
100
		}
101
102
		http.NotFound(w, r)
103
	})
104
105
	port := os.Getenv("PORT")
106
	if port == "" {
107
		port = "8080"
108
	}
109
110
	fmt.Printf("Server starting on http://localhost:%s\n", port)
111
	if err := http.ListenAndServe(":"+port, nil); err != nil {
112
		log.Fatal(err)
113
	}
114
}