-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
476 additions
and
85 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/shiningrush/droplet" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/apisix/manager-api/conf" | ||
"github.com/apisix/manager-api/internal" | ||
"github.com/apisix/manager-api/internal/core/storage" | ||
"github.com/apisix/manager-api/internal/core/store" | ||
"github.com/apisix/manager-api/internal/handler" | ||
"github.com/apisix/manager-api/internal/utils" | ||
"github.com/apisix/manager-api/log" | ||
) | ||
|
||
// NewManagerAPICommand creates the manager-api command. | ||
func NewManagerAPICommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "manager-api [flags]", | ||
Short: "APISIX Manager API", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
conf.Init() | ||
droplet.Option.Orchestrator = func(mws []droplet.Middleware) []droplet.Middleware { | ||
var newMws []droplet.Middleware | ||
// default middleware order: resp_reshape, auto_input, traffic_log | ||
// We should put err_transform at second to catch all error | ||
newMws = append(newMws, mws[0], &handler.ErrorTransformMiddleware{}) | ||
newMws = append(newMws, mws[1:]...) | ||
return newMws | ||
} | ||
|
||
if err := storage.InitETCDClient(conf.ETCDConfig); err != nil { | ||
log.Errorf("init etcd client fail: %w", err) | ||
panic(err) | ||
} | ||
if err := store.InitStores(); err != nil { | ||
log.Errorf("init stores fail: %w", err) | ||
panic(err) | ||
} | ||
// routes | ||
r := internal.SetUpRouter() | ||
addr := fmt.Sprintf("%s:%d", conf.ServerHost, conf.ServerPort) | ||
s := &http.Server{ | ||
Addr: addr, | ||
Handler: r, | ||
ReadTimeout: time.Duration(1000) * time.Millisecond, | ||
WriteTimeout: time.Duration(5000) * time.Millisecond, | ||
} | ||
|
||
log.Infof("The Manager API is listening on %s", addr) | ||
|
||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
go func() { | ||
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
utils.CloseAll() | ||
log.Fatalf("listen and serv fail: %s", err) | ||
} | ||
}() | ||
|
||
sig := <-quit | ||
log.Infof("The Manager API server receive %s and start shutting down", sig.String()) | ||
|
||
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) | ||
defer cancel() | ||
|
||
if err := s.Shutdown(ctx); err != nil { | ||
log.Errorf("Shutting down server error: %s", err) | ||
} | ||
|
||
log.Infof("The Manager API server exited") | ||
|
||
utils.CloseAll() | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.PersistentFlags().StringVarP(&conf.WorkDir, "work-dir", "p", ".", "current work directory") | ||
return cmd | ||
} |
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
Oops, something went wrong.