-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (60 loc) · 1.73 KB
/
main.go
File metadata and controls
75 lines (60 loc) · 1.73 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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"grd0.net/proxy/s3/database"
"grd0.net/proxy/s3/proxy"
)
func main() {
err := godotenv.Load()
if err != nil {
panic("Error loading .env file")
}
s3_origin_endpoint := os.Getenv("S3_ORIGIN_ENDPOINT")
s3_origin_use_ssl := os.Getenv("S3_ORIGIN_USE_SSL")
s3_origin_protocol := "https"
if s3_origin_use_ssl == "false" {
s3_origin_protocol = "http"
}
// Create a file to store data and downloaded content
localfs_path := os.Getenv("LOCALFS_PATH")
err = os.MkdirAll(localfs_path, os.ModePerm)
if err != nil {
panic(fmt.Errorf("failed to create directories: %w", err))
}
database.InitDatabase()
e := echo.New()
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://localhost:8443", "https://cloud.grd0.net"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
e.Use(middleware.Logger())
e.GET("/:bucket/:object", proxy.GetExistingObject)
// Must register the path params, otherwise middleware will fail to obtain the data
e.HEAD("/:bucket/:object", func(c echo.Context) error {
return c.String(http.StatusNotImplemented, "")
})
url1, err := url.Parse(s3_origin_protocol + "://" + s3_origin_endpoint)
if err != nil {
e.Logger.Fatal(err)
}
balancer := middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{
{
URL: url1,
},
})
e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
Balancer: balancer,
Skipper: proxy.SkipperLogic,
}))
e.Use(middleware.Decompress())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
e.Logger.Fatal(e.Start(":80"))
}