Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add frontend serving and ping routes

天舟bf6222f3e6c0
diff --git a/backend/.gitignore b/backend/.gitignore
index 9c76a090f8..783f21d3b4 100644
--- a/backend/.gitignore
+++ b/backend/.gitignore
@@ -15,4 +15,7 @@
# vendor/
# Air (hot reload) generated
-.air
\ No newline at end of file
+.air
+
+# Frontend asset
+dist
\ No newline at end of file
diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go
index f243906f47..13d9be4163 100644
--- a/backend/cmd/server/main.go
+++ b/backend/cmd/server/main.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/signal"
+ "time"
"github.com/bytebase/bytebase/server"
"github.com/bytebase/bytebase/sqlite"
@@ -41,18 +42,6 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
-
- // r := gin.Default()
- // // Dont worry about this line just yet, it will make sense in the Dockerise bit!
- // r.Use(static.Serve("/", static.LocalFile("./web", true)))
- // api := r.Group("/api")
- // api.GET("/ping", func(c *gin.Context) {
- // c.JSON(200, gin.H{
- // "message": "pong from backend",
- // })
- // })
-
- // r.Run()
}
func NewMain() *Main {
@@ -69,20 +58,28 @@ func (m *Main) Run() error {
// m.Server.TodoService = sqlite.NewTodoService(m.DB)
- // if err := m.Server.Run(); err != nil {
- // return err
- // }
+ if err := m.Server.Run(); err != nil {
+ return err
+ }
return nil
}
// Close gracefully stops the program.
func (m *Main) Close() error {
- // if m.Server != nil {
- // if err := m.Server.Close(); err != nil {
- // return err
- // }
- // }
+ // Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
+ // Use a buffered channel to avoid missing signals as recommended for signal.Notify
+ quit := make(chan os.Signal, 1)
+ signal.Notify(quit, os.Interrupt)
+ <-quit
+
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+
+ if m.Server != nil {
+ m.Server.Close(ctx)
+ }
+
if m.DB != nil {
if err := m.DB.Close(); err != nil {
return err
diff --git a/backend/go.mod b/backend/go.mod
index 9eabe8ffb9..166384128d 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -2,4 +2,7 @@ module github.com/bytebase/bytebase
go 1.16
-require github.com/mattn/go-sqlite3 v1.14.7 // indirect
+require (
+ github.com/labstack/echo/v4 v4.2.2 // indirect
+ github.com/mattn/go-sqlite3 v1.14.7 // indirect
+)
diff --git a/backend/go.sum b/backend/go.sum
index 96ff82415a..b45d09f5f2 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -1,2 +1,46 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
+github.com/labstack/echo/v4 v4.2.2 h1:bq2fdZCionY1jck8rzUpQEu2YSmI8QbX6LHrCa60IVs=
+github.com/labstack/echo/v4 v4.2.2/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
+github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
+github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
+github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
+github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
+github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
+github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
+github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
+github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+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/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
+github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
+github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM=
+golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
+golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 h1:DvY3Zkh7KabQE/kfzMvYvKirSiguP9Q/veMtkYyf0o8=
+golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
diff --git a/backend/server/debug.go b/backend/server/debug.go
new file mode 100644
index 0000000000..31902e7151
--- /dev/null
+++ b/backend/server/debug.go
@@ -0,0 +1,16 @@
+package server
+
+import (
+ "fmt"
+ "net/http"
+ "time"
+
+ "github.com/labstack/echo/v4"
+)
+
+func (s *Server) registerDebugRoutes(e *echo.Echo) {
+ e.GET("/api/ping", func(c echo.Context) error {
+ return c.String(http.StatusOK, fmt.Sprintf("[%v] Pong from backend", time.Now().Format("2006-01-02 15:04:05")))
+ })
+
+}
diff --git a/backend/server/environment.go b/backend/server/environment.go
index 1c3adb09c7..8a1614d065 100644
--- a/backend/server/environment.go
+++ b/backend/server/environment.go
@@ -1,7 +1,11 @@
package server
-import "fmt"
+import (
+ "fmt"
-func (s *Server) registerEnvironmentRoutes() {
+ "github.com/labstack/echo/v4"
+)
+
+func (s *Server) registerEnvironmentRoutes(e *echo.Echo) {
fmt.Println("registerEnvironmentRoutes")
}
diff --git a/backend/server/server.go b/backend/server/server.go
index d320428f69..fa59c58df1 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -1,5 +1,14 @@
package server
+import (
+ "context"
+ "embed"
+ "io/fs"
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+)
+
type Environment struct {
ID string `json:"id"`
Name string `json:"name"`
@@ -11,12 +20,46 @@ type EnvironmentService interface {
type Server struct {
EnvironmentService EnvironmentService
+
+ e *echo.Echo
+}
+
+//go:embed dist
+var embededFiles embed.FS
+
+func getFileSystem() http.FileSystem {
+ fsys, err := fs.Sub(embededFiles, "dist")
+ if err != nil {
+ panic(err)
+ }
+
+ return http.FS(fsys)
}
func NewServer() *Server {
- s := &Server{}
+ e := echo.New()
- s.registerEnvironmentRoutes()
+ assetHandler := http.FileServer(getFileSystem())
+ e.GET("/", echo.WrapHandler(assetHandler))
+ e.GET("/assets/*", echo.WrapHandler(assetHandler))
+
+ s := &Server{
+ e: e,
+ }
+
+ s.registerDebugRoutes(e)
+
+ s.registerEnvironmentRoutes(e)
return s
}
+
+func (s *Server) Run() error {
+ return s.e.Start(":8080")
+}
+
+func (s *Server) Close(ctx context.Context) {
+ if err := s.e.Shutdown(ctx); err != nil {
+ s.e.Logger.Fatal(err)
+ }
+}