Skip to content

Commit

Permalink
Fix router clashes
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrex2001 committed May 20, 2021
1 parent b163302 commit a6108ac
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 29 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.16

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gin-gonic/gin v1.7.1
github.com/gin-gonic/gin v1.7.2-0.20210519235755-e72e584d1aba
github.com/go-playground/validator/v10 v10.6.0 // indirect
github.com/goccy/go-json v0.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/go-memdb v1.3.2
github.com/json-iterator/go v1.1.11 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gin-gonic/gin v1.7.2-0.20210519235755-e72e584d1aba h1:2jUZdpT0sXVBSeXbatd/CdRBaOK8YYFlnx5v5LBmOj4=
github.com/gin-gonic/gin v1.7.2-0.20210519235755-e72e584d1aba/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
Expand Down Expand Up @@ -172,6 +174,8 @@ github.com/go-playground/validator/v10 v10.6.0 h1:UGIt4xR++fD9QrBOoo/ascJfGe3AGH
github.com/go-playground/validator/v10 v10.6.0/go.mod h1:xm76BBt941f7yWdGnI2DVPFFg1UK3YY04qifoXU3lOk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/goccy/go-json v0.5.1 h1:R9UYTOUvo7eIY9aeDMZ4L6OVtHaSr1k2No9W6MKjXrA=
github.com/goccy/go-json v0.5.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
9 changes: 0 additions & 9 deletions internal/model/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ func (in *Database) GetContainers() ([]*types.Container, error) {
func (in *Database) SaveContainer(con *types.Container) error {
if con.ID == "" {
id := stringid.GenerateRandomID()
if id[:1] == "c" {
id = "b" + id[1:]
}
con.ID = id
con.ShortID = stringid.TruncateID(id)
con.Created = time.Now()
Expand Down Expand Up @@ -186,9 +183,6 @@ func (in *Database) GetExecs() ([]*types.Exec, error) {
func (in *Database) SaveExec(exc *types.Exec) error {
if exc.ID == "" {
id := stringid.GenerateRandomID()
if id[:1] == "c" {
id = "b" + id[1:]
}
exc.ID = id
exc.Created = time.Now()
}
Expand Down Expand Up @@ -284,9 +278,6 @@ func (in *Database) GetNetworksByIDs(ids map[string]interface{}) ([]*types.Netwo
func (in *Database) SaveNetwork(netw *types.Network) error {
if netw.ID == "" {
id := stringid.GenerateRandomID()
if id[:1] == "c" {
id = "b" + id[1:]
}
netw.ID = id
netw.ShortID = stringid.TruncateID(id)
netw.Created = time.Now()
Expand Down
10 changes: 1 addition & 9 deletions internal/model/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestDatabase(t *testing.T) {
}
}

func TestIDWorkaround(t *testing.T) {
func TestDeadlock(t *testing.T) {
db, err := New()
if err != nil {
t.Errorf("Unexpected error creating database: %s", err)
Expand All @@ -116,21 +116,13 @@ func TestIDWorkaround(t *testing.T) {
if err := db.SaveContainer(con); err != nil {
t.Errorf("Unexpected error when creating a new container: %s", err)
}
if con.ID[:1] == "c" {
t.Errorf("Container ID that start with a c cause problems in the server router setup...")
return
}
if err := db.DeleteContainer(con); err != nil {
t.Errorf("Unexpected error when deleting container: %s", err)
}
netw := &types.Network{Name: "tb303"}
if err := db.SaveNetwork(netw); err != nil {
t.Errorf("Unexpected error when creating a new network: %s", err)
}
if netw.ID[:1] == "c" {
t.Errorf("Network ID that start with a c cause problems in the server router setup...")
return
}
if err := db.DeleteNetwork(netw); err != nil {
t.Errorf("Unexpected error when deleting network: %s", err)
}
Expand Down
10 changes: 0 additions & 10 deletions internal/server/routes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,12 @@ func (cr *Router) initRoutes(router *gin.Engine) {
router.GET("/_ping", cr.Ping)
router.HEAD("/_ping", cr.Ping)

// DEBT: the below routes overlap with the create route; due to a bug in
// gin-gonic this means :id that start with a c will 404 by the router;
// as work around, no container-ids are generated that start with a 'c'...
// https://github.com/gin-gonic/gin/issues/2682
router.POST("/containers/create", cr.ContainerCreate)
router.POST("/containers/:id/start", cr.ContainerStart)
router.POST("/containers/:id/attach", cr.ContainerAttach)
router.POST("/containers/:id/exec", cr.ContainerExec)
router.POST("/containers/:id/stop", cr.ContainerStop)
router.POST("/containers/:id/kill", cr.ContainerKill)

router.DELETE("/containers/:id", cr.ContainerDelete)
router.GET("/containers/json", cr.ContainerList)
router.GET("/containers/:id/json", cr.ContainerInfo)
Expand All @@ -55,14 +50,9 @@ func (cr *Router) initRoutes(router *gin.Engine) {
router.POST("/exec/:id/start", cr.ExecStart)
router.GET("/exec/:id/json", cr.ExecInfo)

// DEBT: the below routes overlap with the create route; due to a bug in
// gin-gonic this means :id that start with a c will 404 by the router;
// as work around, no container-ids are generated that start with a 'c'...
// https://github.com/gin-gonic/gin/issues/2682
router.POST("/networks/create", cr.NetworksCreate)
router.POST("/networks/:id/connect", cr.NetworksConnect)
router.POST("/networks/:id/disconnect", cr.NetworksDisconnect)

router.GET("/networks", cr.NetworksList)
router.GET("/networks/:id", cr.NetworksInfo)
router.DELETE("/networks/:id", cr.NetworksDelete)
Expand Down

0 comments on commit a6108ac

Please sign in to comment.