From aa33ed1324671a4d531aef0e8bee4e25ea014ca8 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Fri, 4 Feb 2022 12:08:47 -0500 Subject: [PATCH] init --- .gitignore | 1 + go.mod | 14 ++++++++++++++ go.sum | 24 ++++++++++++++++++++++++ main.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ structs.go | 32 +++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 structs.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e46596 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cache \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..85da429 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module turbogo + +go 1.17 + +require github.com/gofiber/fiber/v2 v2.25.0 + +require ( + github.com/andybalholm/brotli v1.0.2 // indirect + github.com/klauspost/compress v1.13.4 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.32.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3a1fc16 --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E= +github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= +github.com/gofiber/fiber/v2 v2.25.0 h1:kv8dmG/sAFDFpTueCMEn4X0JS5d72pEFTKLZ3miOREw= +github.com/gofiber/fiber/v2 v2.25.0/go.mod h1:7efVWcBOZi1PyMWznnbitjnARPA7nYZxmQXJVod0bo0= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= +github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.32.0 h1:keswgWzyKyNIIjz2a7JmCYHOOIkRp6HMx9oTV6QrZWY= +github.com/valyala/fasthttp v1.32.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..93d6987 --- /dev/null +++ b/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/logger" +) + +func main() { + app := fiber.New() + app.Use(logger.New(logger.Config{ + // log query string parameters + Format: "[${ip}]:${port} ${status} - ${method} ${path} - ${queryParams}\n", + })) + app.Use(func(c *fiber.Ctx) error { + token := os.Getenv("API_TOKEN") + if token == "" { + token = "ckynqopsk0002zp6ehp2n6d5n" + } + return c.Next() + }) + app.Get("/", func(c *fiber.Ctx) error { + // return hello world + return c.SendString("Hello, World!") + }) + + // apis for turborepo + app.Get("/v2/teams", func(c *fiber.Ctx) error { + return c.SendStatus(fiber.StatusOK) + }) + + app.Get("/v2/user", func(c *fiber.Ctx) error { + return c.SendStatus(fiber.StatusOK) + }) + + app.Get("/v8/artifacts/:hash", func(c *fiber.Ctx) error { + // log hello world + // serialize query params to + fmt.Println(string(c.Request().URI().QueryString())) + return c.SendFile("./cache/" + c.Params("hash")) + }) + app.Put("/v8/artifacts/:hash", func(c *fiber.Ctx) error { + fmt.Println(string(c.Request().URI().QueryString())) + return os.WriteFile("./cache/"+c.Params("hash"), c.Request().Body(), 0644) + }) + port := os.Getenv("PORT") + if port == "" { + port = "5002" + } + + log.Fatal(app.Listen(fmt.Sprintf(":%s", port))) +} diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..30c987c --- /dev/null +++ b/structs.go @@ -0,0 +1,32 @@ +package main + +// Team is a Vercel Team object +type Team struct { + ID string `json:"id,omitempty"` + Slug string `json:"slug,omitempty"` + Name string `json:"name,omitempty"` + CreatedAt int `json:"createdAt,omitempty"` + Created string `json:"created,omitempty"` +} + +type Pagination struct { + Count int `json:"count,omitempty"` + Next int `json:"next,omitempty"` + Prev int `json:"prev,omitempty"` +} + +type TeamsResponse struct { + Teams []Team `json:"teams,omitempty"` + Pagination Pagination `json:"pagination,omitempty"` +} + +type User struct { + ID string `json:"id,omitempty"` + Username string `json:"username,omitempty"` + Email string `json:"email,omitempty"` + Name string `json:"name,omitempty"` + CreatedAt int `json:"createdAt,omitempty"` +} +type UserResponse struct { + User User `json:"user,omitempty"` +}