-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (71 loc) · 2.74 KB
/
main.go
File metadata and controls
87 lines (71 loc) · 2.74 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
package main
import (
"log"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"google.golang.org/api/sheets/v4"
)
func main() {
// load .env file from given path
// we keep it empty it will load .env from current directory
err := godotenv.Load(".env")
if err != nil {
log.Println("Warning: .env file not found. Falling back to environment variables.")
}
// Initialize GCP services
var sheetsService = setUpGoogleSheetsAPI()
// Initialize Echo
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
// Routes
e.GET("/", handleRoot)
e.GET("/get_networkpoints", func(c echo.Context) error { return handleGetNetworkpoints(c, sheetsService) })
e.GET("/get_level1", func(c echo.Context) error { return handleGetLevel1(c, sheetsService) })
e.GET("/get_level2", func(c echo.Context) error { return handleGetLevel2(c, sheetsService) })
e.GET("/get_level3", func(c echo.Context) error { return handleGetLevel3(c, sheetsService) })
e.GET("/get_level4", func(c echo.Context) error { return handleGetLevel4(c, sheetsService) })
// Start server
port := os.Getenv("PORT")
address := ":" + port
e.Logger.Fatal(e.Start(address))
}
// Handlers
func handleRoot(c echo.Context) error {
return c.String(http.StatusOK, "PCW Network Map GeoJson API")
}
func handleGetNetworkpoints(c echo.Context, sheetsService *sheets.Service) error {
sheet_values := get_sheet_values(sheetsService, "networkpoints")
networkpoints := process_networkpoints(sheet_values)
shelled := prep_for_export(networkpoints)
return c.JSON(http.StatusOK, shelled)
}
func handleGetLevel1(c echo.Context, sheetsService *sheets.Service) error {
sheet_values := get_sheet_values(sheetsService, "level1")
networkpoints := process_level1(sheet_values)
shelled := prep_for_export_level1(networkpoints)
return c.JSON(http.StatusOK, shelled)
}
func handleGetLevel2(c echo.Context, sheetsService *sheets.Service) error {
sheet_values := get_sheet_values(sheetsService, "level2")
networkpoints := process_level2_level3(sheet_values)
shelled := prep_for_export_level2_3(networkpoints)
return c.JSON(http.StatusOK, shelled)
}
func handleGetLevel3(c echo.Context, sheetsService *sheets.Service) error {
sheet_values := get_sheet_values(sheetsService, "level3")
networkpoints := process_level2_level3(sheet_values)
shelled := prep_for_export_level2_3(networkpoints)
return c.JSON(http.StatusOK, shelled)
}
func handleGetLevel4(c echo.Context, sheetsService *sheets.Service) error {
sheet_values := get_sheet_values(sheetsService, "level4")
networkpoints := process_level2_level3(sheet_values)
shelled := prep_for_export_level2_3(networkpoints)
return c.JSON(http.StatusOK, shelled)
}