-
Notifications
You must be signed in to change notification settings - Fork 3
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
11 changed files
with
302 additions
and
114 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,52 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/Navid2zp/citus-failover/config" | ||
"github.com/Navid2zp/citus-failover/logging" | ||
"github.com/gin-gonic/gin" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var logger *logging.Logger | ||
|
||
func initAPI() { | ||
logger = logging.NewLogger("api") | ||
if !config.Config.Settings.Debug { | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
} | ||
|
||
func adminMiddleware(c *gin.Context) { | ||
secret := c.Request.Header.Get("Secret") | ||
if secret == "" { | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": ErrCodeSecretMissing}) | ||
return | ||
} | ||
if secret != config.Config.API.Secret { | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": ErrCodeInvalidSecret}) | ||
return | ||
} | ||
c.Next() | ||
} | ||
|
||
func Serve() { | ||
initAPI() | ||
|
||
router := gin.Default() | ||
v1 := router.Group("/v1", adminMiddleware) | ||
|
||
{ | ||
v1.GET("/workers", getPrimaryWorkers) | ||
v1.GET("/coordinator", getPrimaryCoordinator) | ||
v1.GET("/coordinators", getAllCoordinators) | ||
v1.GET("/nodes", getNodes) | ||
} | ||
|
||
log.Println("Starting api on port", config.Config.API.Port, "...") | ||
|
||
err := router.Run(":" + config.Config.API.Port) | ||
if err != nil { | ||
logger.APIStartFailed(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,69 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/Navid2zp/citus-failover/core" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
ErrCodeServerError = "SERVER_ERROR" | ||
ErrCodeSecretMissing = "SECRET_MISSING" | ||
ErrCodeInvalidSecret = "INVALID_SECRET" | ||
) | ||
|
||
func getPrimaryWorkers(c *gin.Context) { | ||
workers, err := core.GetPrimaryWorkers() | ||
if err != nil { | ||
logger.GetWorkersFailed(err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": ErrCodeServerError, | ||
"message": "failed to get workers from coordinator", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, workers) | ||
} | ||
|
||
func getPrimaryCoordinator(c *gin.Context) { | ||
coordinator, err := core.GetCoordinator() | ||
if err != nil { | ||
logger.GetCoordinatorFailed(err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": ErrCodeServerError, | ||
"message": "failed to get coordinator from monitor", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, coordinator) | ||
} | ||
|
||
func getAllCoordinators(c *gin.Context) { | ||
coordinators, err := core.GetAllCoordinators() | ||
if err != nil { | ||
logger.GetCoordinatorFailed(err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": ErrCodeServerError, | ||
"message": "failed to get coordinators from monitor", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, coordinators) | ||
} | ||
|
||
func getNodes(c *gin.Context) { | ||
nodes, err := core.GetNodes() | ||
if err != nil { | ||
logger.GetWorkersFailed(err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": ErrCodeServerError, | ||
"message": "failed to get nodes from monitor", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, nodes) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package logging | ||
|
||
import "go.uber.org/zap" | ||
|
||
func (l *Logger) APIStartFailed(err error) { | ||
l.l.Error("failed to start api", | ||
zap.String("service", l.service), zap.Error(err)) | ||
} |
Oops, something went wrong.