Compare commits
No commits in common. "9cfd944760a3217b3bf5d3c5e5c27998979ed8ad" and "32b016a3bf7833e34e34745fc4487fc0848a5a1d" have entirely different histories.
9cfd944760
...
32b016a3bf
|
@ -1,24 +0,0 @@
|
||||||
{
|
|
||||||
// 使用 IntelliSense 了解相关属性。
|
|
||||||
// 悬停以查看现有属性的描述。
|
|
||||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "Launch Package in Debug Mode",
|
|
||||||
"type": "go",
|
|
||||||
"request": "launch",
|
|
||||||
"mode": "debug",
|
|
||||||
"program": "./bin/syz.go",
|
|
||||||
"args": ["start"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Launch Package in Prod Mode",
|
|
||||||
"type": "go",
|
|
||||||
"request": "launch",
|
|
||||||
"mode": "debug",
|
|
||||||
"program": "./bin/syz.go",
|
|
||||||
"args": ["start", "--mode", "prod"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
19
bin/syz.go
19
bin/syz.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -39,13 +40,14 @@ var rootCmd = &cobra.Command{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if err := store.Migrate(ctx); err != nil {
|
if err := store.Migrate(ctx); err != nil {
|
||||||
// cancel()
|
cancel()
|
||||||
// slog.Error("store migrate failed", "err", err)
|
slog.Error("store migrate failed", "err", err)
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
s := server.NewServer(ctx, config, store)
|
addr := fmt.Sprintf("%s:%d", config.Addr, config.Port)
|
||||||
|
s := server.NewServer(ctx, addr, store)
|
||||||
|
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
|
|
||||||
|
@ -76,14 +78,11 @@ func init() {
|
||||||
rootCmd.PersistentFlags().String("mode", "dev", `mode of server, can be "prod" or "dev"`)
|
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().String("addr", "0.0.0.0", "address of server")
|
||||||
rootCmd.PersistentFlags().Int("port", 8080, "port of server")
|
rootCmd.PersistentFlags().Int("port", 8080, "port of server")
|
||||||
rootCmd.PersistentFlags().String("dsn", "postgresql://syz:syz_passwd@localhost:5432/syz?sslmode=disable", "database source name")
|
rootCmd.PersistentFlags().String("dsn", "", "database source name")
|
||||||
|
|
||||||
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.SetEnvPrefix("syz")
|
|
||||||
viper.AutomaticEnv()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -10,7 +10,7 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
if c.Mode != "dev" && c.Mode != "prod" {
|
if c.Mode != "debug" && c.Mode != "release" {
|
||||||
return fmt.Errorf("invalid mode: %s", c.Mode)
|
return fmt.Errorf("invalid mode: %s", c.Mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,11 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zhouxhere.com/zhouxhere/syz/config"
|
|
||||||
"git.zhouxhere.com/zhouxhere/syz/store"
|
"git.zhouxhere.com/zhouxhere/syz/store"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -17,32 +15,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
config *config.Config
|
addr string
|
||||||
Store *store.Store
|
Store *store.Store
|
||||||
httpServer *http.Server
|
httpServer *http.Server
|
||||||
grpcServer *grpc.Server
|
grpcServer *grpc.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(ctx context.Context, config *config.Config, store *store.Store) *Server {
|
func NewServer(ctx context.Context, addr string, store *store.Store) *Server {
|
||||||
s := &Server{
|
s := &Server{
|
||||||
Store: store,
|
Store: store,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.config = config
|
s.addr = addr
|
||||||
|
|
||||||
if config.Mode == "prod" {
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
|
||||||
} else {
|
|
||||||
gin.SetMode(gin.DebugMode)
|
|
||||||
}
|
|
||||||
|
|
||||||
ginServer := gin.Default()
|
ginServer := gin.Default()
|
||||||
addr := fmt.Sprintf("%s:%d", config.Addr, config.Port)
|
|
||||||
s.httpServer = &http.Server{
|
s.httpServer = &http.Server{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
Handler: ginServer,
|
Handler: ginServer,
|
||||||
}
|
}
|
||||||
|
|
||||||
grpcServer := grpc.NewServer()
|
grpcServer := grpc.NewServer()
|
||||||
s.grpcServer = grpcServer
|
s.grpcServer = grpcServer
|
||||||
|
|
||||||
|
@ -50,8 +39,7 @@ func NewServer(ctx context.Context, config *config.Config, store *store.Store) *
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start(ctx context.Context) error {
|
func (s *Server) Start(ctx context.Context) error {
|
||||||
addr := fmt.Sprintf("%s:%d", s.config.Addr, s.config.Port)
|
listener, err := net.Listen("tcp", s.addr)
|
||||||
listener, err := net.Listen("tcp", addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to listen")
|
return errors.Wrap(err, "failed to listen")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue