-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Flc゛ <[email protected]>
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Gin Server | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-kratos/kratos/v2" | ||
|
||
ginS "github.com/go-kratos-ecosystem/components/v2/gin" | ||
) | ||
|
||
func main() { | ||
gs := ginS.NewServer( | ||
gin.Default(), | ||
ginS.WithAddr(":8080"), | ||
) | ||
|
||
gs.GET("/ping", func(c *gin.Context) { | ||
c.JSON(200, gin.H{ | ||
"message": "pong", | ||
}) | ||
}) | ||
|
||
app := kratos.New( | ||
kratos.Server(gs), | ||
) | ||
|
||
err := app.Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package gin | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Server struct { | ||
*gin.Engine | ||
|
||
server *http.Server | ||
|
||
addr string | ||
} | ||
|
||
type Option func(*Server) | ||
|
||
func WithAddr(addr string) Option { | ||
return func(s *Server) { | ||
s.addr = addr | ||
} | ||
} | ||
|
||
func NewServer(e *gin.Engine, opts ...Option) *Server { | ||
srv := &Server{ | ||
Engine: e, | ||
addr: ":8080", | ||
} | ||
|
||
srv.server = &http.Server{ | ||
Addr: srv.addr, | ||
Handler: e, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(srv) | ||
} | ||
|
||
return srv | ||
} | ||
|
||
func (s *Server) Start(_ context.Context) error { | ||
return s.server.ListenAndServe() | ||
} | ||
|
||
func (s *Server) Stop(ctx context.Context) error { | ||
return s.server.Shutdown(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters