-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
68 lines (54 loc) · 2.54 KB
/
main.go
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
package main
import (
"os"
"log"
"net/http"
"strings"
"github.com/rs/cors"
"github.com/joho/godotenv"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"github.com/dee-ex/shopee_crawler_api/modules/brands"
"github.com/dee-ex/shopee_crawler_api/modules/products"
"github.com/dee-ex/shopee_crawler_api/modules/jobs/trigger/crawl_brands"
"github.com/dee-ex/shopee_crawler_api/modules/jobs/trigger/crawl_products"
)
func start_server() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/brands", brands.HandleCreateBrand).Methods("POST")
router.HandleFunc("/brands", brands.HandleGetAllBrands).Methods("GET")
router.HandleFunc("/brands/{brand_id}", brands.HandleGetBrandByID).Methods("GET")
router.HandleFunc("/brands/{brand_id}", brands.HandleUpdateByID).Methods("PUT")
router.HandleFunc("/brands/{brand_id}", brands.HandleDeleteByID).Methods("DELETE")
router.HandleFunc("/brands/{brand_id}/products", brands.HandleGetAllProducts).Methods("GET")
router.HandleFunc("/products", products.HandleCreateProduct).Methods("POST")
router.HandleFunc("/products", products.HandleGetAllProducts).Methods("GET")
router.HandleFunc("/products/{product_id}", products.HandleGetProductByID).Methods("GET")
router.HandleFunc("/products/{product_id}", products.HandleUpdateByID).Methods("PUT")
router.HandleFunc("/products/{product_id}", products.HandleDeleteByID).Methods("DELETE")
router.HandleFunc("/jobs/trigger/crawl_brands", crawl_brands.HandleCrawl).Methods("GET")
router.HandleFunc("/jobs/trigger/crawl_products", crawl_products.HandleCrawlProducts).Methods("GET")
router.HandleFunc("/jobs/trigger/crawl_products/{brand_username}/", crawl_products.HandleCrawlByUsername).Methods("GET")
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions},
})
enhanced_router := c.Handler(router)
enhanced_router = handlers.LoggingHandler(os.Stdout, enhanced_router)
log.Fatal(http.ListenAndServe(":" + os.Getenv("SERVER_PORT"), enhanced_router))
}
func main() {
err := godotenv.Load("env/database.env")
if err != nil {
log.Fatal("Error loading database.env file")
}
if strings.ToUpper(os.Getenv("DATABASE_CONFIGURED")) != "YES" {
log.Fatal("Database is not configured yet")
}
err = godotenv.Load("env/server.env")
if err != nil {
log.Fatal("Error loading server.env file")
}
log.Println("Hosting: Local server: localhost:" + os.Getenv("SERVER_PORT"))
start_server()
}