generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from KyberNetwork/promotees-list-v2
intergrate service maintain promotees list to tradelog v2
- Loading branch information
Showing
23 changed files
with
2,572 additions
and
24 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,75 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
promoteeServer "github.com/KyberNetwork/tradelogs/v2/internal/server/promotees" | ||
libapp "github.com/KyberNetwork/tradelogs/v2/pkg/app" | ||
promoteeTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/promotees" | ||
"github.com/KyberNetwork/tradinglib/pkg/dbutil" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/urfave/cli" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
app := libapp.NewApp() | ||
app.Name = "promotees service" | ||
app.Action = run | ||
|
||
app.Flags = append(app.Flags, libapp.PostgresSQLFlags("tradelogs_v2")...) | ||
app.Flags = append(app.Flags, libapp.HTTPServerFlags()...) | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
|
||
func run(c *cli.Context) error { | ||
logger, _, flush, err := libapp.NewLogger(c) | ||
if err != nil { | ||
return fmt.Errorf("new logger: %w", err) | ||
} | ||
|
||
defer flush() | ||
|
||
zap.ReplaceGlobals(logger) | ||
l := logger.Sugar() | ||
l.Infow("Starting backfill service") | ||
|
||
db, err := initDB(c) | ||
l.Infow("init db successfully") | ||
if err != nil { | ||
return fmt.Errorf("cannot init DB: %w", err) | ||
} | ||
|
||
//promotion storage | ||
promoteeStorage := promoteeTypes.New(l, db) | ||
|
||
s := promoteeServer.New(promoteeStorage, c.String(libapp.HTTPPromoteeServerFlag.Name)) | ||
|
||
return s.Run() | ||
} | ||
|
||
func initDB(c *cli.Context) (*sqlx.DB, error) { | ||
db, err := libapp.NewDB(map[string]interface{}{ | ||
"host": c.String(libapp.PostgresHost.Name), | ||
"port": c.Int(libapp.PostgresPort.Name), | ||
"user": c.String(libapp.PostgresUser.Name), | ||
"password": c.String(libapp.PostgresPassword.Name), | ||
"dbname": c.String(libapp.PostgresDatabase.Name), | ||
"sslmode": "disable", | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = dbutil.RunMigrationUp(db.DB, c.String(libapp.PostgresMigrationPath.Name), | ||
c.String(libapp.PostgresDatabase.Name)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} |
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,116 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
promoteeTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/promotees" | ||
"github.com/gin-contrib/pprof" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Server struct { | ||
r *gin.Engine | ||
bindAddr string | ||
s *promoteeTypes.Storage | ||
} | ||
|
||
func New(s *promoteeTypes.Storage, bindAddr string) *Server { | ||
engine := gin.New() | ||
engine.Use(gin.Recovery()) | ||
|
||
server := &Server{ | ||
r: engine, | ||
bindAddr: bindAddr, | ||
s: s, | ||
} | ||
|
||
gin.SetMode(gin.DebugMode) | ||
server.register() | ||
|
||
return server | ||
} | ||
|
||
func (s *Server) Run() error { | ||
if err := s.r.Run(s.bindAddr); err != nil { | ||
return fmt.Errorf("run server: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Server) register() { | ||
pprof.Register(s.r, "/debug") | ||
s.r.GET("/promotees", s.getPromotees) | ||
s.r.POST("/insert_name", s.insertName) | ||
} | ||
|
||
func responseErr(c *gin.Context, status int, err error) { | ||
c.JSON(http.StatusBadRequest, gin.H{ | ||
"success": false, | ||
"error": err.Error(), | ||
"status": status, | ||
}) | ||
} | ||
|
||
func (s *Server) getPromotees(c *gin.Context) { | ||
var ( | ||
query promoteeTypes.PromoteesQuery | ||
err = c.ShouldBind(&query) | ||
) | ||
if err != nil { | ||
responseErr(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
if query.Promotee != "" { | ||
query.Promotee = strings.ToLower(query.Promotee) | ||
} | ||
if query.Promoter != "" { | ||
query.Promoter = strings.ToLower(query.Promoter) | ||
} | ||
data, err := s.s.Get(query) | ||
if err != nil { | ||
responseErr(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"data": data, | ||
}) | ||
} | ||
|
||
func (s *Server) insertName(c *gin.Context) { | ||
var queries []promoteeTypes.PromoteesQuery | ||
|
||
if err := c.ShouldBindJSON(&queries); err != nil { | ||
responseErr(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
promotees := make([]promoteeTypes.Promotee, len(queries)) | ||
for i, query := range queries { | ||
if query.Name == "" { | ||
responseErr(c, http.StatusBadRequest, fmt.Errorf("missing field 'Name' in query index %d", i)) | ||
return | ||
} | ||
if query.Promoter == "" { | ||
responseErr(c, http.StatusBadRequest, fmt.Errorf("missing field 'Promoter' in query index %d", i)) | ||
return | ||
} | ||
promotees[i] = promoteeTypes.Promotee{ | ||
Promoter: strings.ToLower(query.Promoter), | ||
Name: query.Name, | ||
} | ||
} | ||
|
||
if err := s.s.InsertPromoterName(promotees); err != nil { | ||
responseErr(c, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"data": promotees, | ||
}) | ||
} |
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
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.