-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
122 lines (100 loc) · 2.67 KB
/
main.go
File metadata and controls
122 lines (100 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func main() {
cfg, err := loadConfig("Config/config.json")
if err != nil {
log.Fatalf("[ERROR] Can't load config: %v", err)
}
app := fiber.New(fiber.Config{
AppName: "CulliGo API",
ServerHeader: "CulliGo",
DisableStartupMessage: false,
})
app.Use(recover.New())
app.Use(logger.New())
app.Use(cors.New())
url := launcher.New().
Headless(true).
Append("--no-sandbox").
MustLaunch()
browser := rod.New().ControlURL(url).NoDefaultDevice().MustConnect()
defer browser.MustClose()
app.Get("/health", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"status": "ok",
"timestamp": time.Now(),
})
})
app.Get("/scrape/:week", func(c *fiber.Ctx) error {
week_flag := c.Params("week", "this")
page := browser.MustPage(cfg.URL)
page.MustWaitLoad()
var loginErr error
for i := 1; i <= 3; i++ {
loginErr = try_login(page, *cfg)
fmt.Println("[DEBUG] Login Attempt", i, ":", loginErr)
if loginErr == nil {
break
}
time.Sleep(2 * time.Second)
}
if loginErr != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"error": "login failed",
"details": loginErr.Error(),
})
}
page = browser.MustPage(cfg.URL + "Reservation/Reservation.aspx")
food, err := scraper(page, week_flag)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "scraping failed",
"details": err.Error(),
})
}
data, err := json.MarshalIndent(food, "", " ")
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "failed to marshal JSON",
"details": err.Error(),
})
}
if _, err := os.Stat("data"); os.IsNotExist(err) {
err = os.Mkdir("data", 0755)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "failed to create data directory",
"details": err.Error(),
})
}
}
err = os.WriteFile("data/data.json", data, 0644)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "failed to write data file",
"details": err.Error(),
})
}
return c.JSON(food)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("[INFO] CulliGo API running on port %s", port)
if err := app.Listen(":" + port); err != nil {
log.Fatalf("[ERROR] Server failed: %v", err)
}
}