feat 修改server参数
This commit is contained in:
parent
32b016a3bf
commit
39a3d6066b
19
bin/syz.go
19
bin/syz.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -40,14 +39,13 @@ var rootCmd = &cobra.Command{
|
|||
return
|
||||
}
|
||||
|
||||
if err := store.Migrate(ctx); err != nil {
|
||||
cancel()
|
||||
slog.Error("store migrate failed", "err", err)
|
||||
return
|
||||
}
|
||||
// if err := store.Migrate(ctx); err != nil {
|
||||
// cancel()
|
||||
// slog.Error("store migrate failed", "err", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", config.Addr, config.Port)
|
||||
s := server.NewServer(ctx, addr, store)
|
||||
s := server.NewServer(ctx, config, store)
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
|
||||
|
@ -78,11 +76,14 @@ func init() {
|
|||
rootCmd.PersistentFlags().String("mode", "dev", `mode of server, can be "prod" or "dev"`)
|
||||
rootCmd.PersistentFlags().String("addr", "0.0.0.0", "address of server")
|
||||
rootCmd.PersistentFlags().Int("port", 8080, "port of server")
|
||||
rootCmd.PersistentFlags().String("dsn", "", "database source name")
|
||||
rootCmd.PersistentFlags().String("dsn", "postgresql://syz:syz_passwd@localhost:5432/syz?sslmode=disable", "database source name")
|
||||
|
||||
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("syz")
|
||||
viper.AutomaticEnv()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -10,7 +10,7 @@ type Config struct {
|
|||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if c.Mode != "debug" && c.Mode != "release" {
|
||||
if c.Mode != "dev" && c.Mode != "prod" {
|
||||
return fmt.Errorf("invalid mode: %s", c.Mode)
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,13 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.zhouxhere.com/zhouxhere/syz/config"
|
||||
"git.zhouxhere.com/zhouxhere/syz/store"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -15,23 +17,32 @@ import (
|
|||
)
|
||||
|
||||
type Server struct {
|
||||
addr string
|
||||
config *config.Config
|
||||
Store *store.Store
|
||||
httpServer *http.Server
|
||||
grpcServer *grpc.Server
|
||||
}
|
||||
|
||||
func NewServer(ctx context.Context, addr string, store *store.Store) *Server {
|
||||
func NewServer(ctx context.Context, config *config.Config, store *store.Store) *Server {
|
||||
s := &Server{
|
||||
Store: store,
|
||||
}
|
||||
|
||||
s.addr = addr
|
||||
s.config = config
|
||||
|
||||
if config.Mode == "prod" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
} else {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
}
|
||||
|
||||
ginServer := gin.Default()
|
||||
addr := fmt.Sprintf("%s:%d", config.Addr, config.Port)
|
||||
s.httpServer = &http.Server{
|
||||
Addr: addr,
|
||||
Handler: ginServer,
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
s.grpcServer = grpcServer
|
||||
|
||||
|
@ -39,7 +50,8 @@ func NewServer(ctx context.Context, addr string, store *store.Store) *Server {
|
|||
}
|
||||
|
||||
func (s *Server) Start(ctx context.Context) error {
|
||||
listener, err := net.Listen("tcp", s.addr)
|
||||
addr := fmt.Sprintf("%s:%d", s.config.Addr, s.config.Port)
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to listen")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue