-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Quentin Guidée <[email protected]>
- Loading branch information
1 parent
547cad8
commit a59dde5
Showing
9 changed files
with
425 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ginutils | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/vertex-center/vertex/pkg/logger" | ||
) | ||
|
||
func Logger(router string) gin.HandlerFunc { | ||
return gin.LoggerWithFormatter(func(params gin.LogFormatterParams) string { | ||
l := logger.Request(). | ||
AddKeyValue("router", router). | ||
AddKeyValue("method", params.Method). | ||
AddKeyValue("status", params.StatusCode). | ||
AddKeyValue("path", params.Path). | ||
AddKeyValue("latency", params.Latency). | ||
AddKeyValue("ip", params.ClientIP). | ||
AddKeyValue("size", params.BodySize) | ||
|
||
if params.ErrorMessage != "" { | ||
err, _ := strings.CutSuffix(params.ErrorMessage, "\n") | ||
l.AddKeyValue("error", err) | ||
} | ||
|
||
l.PrintInExternalFiles() | ||
|
||
return l.String() | ||
}) | ||
} |
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,82 @@ | ||
package repository | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
"path" | ||
|
||
"github.com/google/uuid" | ||
"github.com/vertex-center/vertex/pkg/logger" | ||
"github.com/vertex-center/vertex/pkg/storage" | ||
"github.com/vertex-center/vertex/types" | ||
) | ||
|
||
type ProxyFSRepository struct { | ||
redirects types.ProxyRedirects | ||
proxyPath string | ||
} | ||
|
||
type ProxyRepositoryParams struct { | ||
proxyPath string | ||
} | ||
|
||
func NewProxyFSRepository(params *ProxyRepositoryParams) ProxyFSRepository { | ||
if params == nil { | ||
params = &ProxyRepositoryParams{} | ||
} | ||
if params.proxyPath == "" { | ||
params.proxyPath = storage.PathProxy | ||
} | ||
|
||
repo := ProxyFSRepository{ | ||
redirects: types.ProxyRedirects{}, | ||
proxyPath: params.proxyPath, | ||
} | ||
repo.read() | ||
|
||
return repo | ||
} | ||
|
||
func (r *ProxyFSRepository) GetRedirects() types.ProxyRedirects { | ||
return r.redirects | ||
} | ||
|
||
func (r *ProxyFSRepository) AddRedirect(id uuid.UUID, redirect types.ProxyRedirect) error { | ||
r.redirects[id] = redirect | ||
return r.write() | ||
} | ||
|
||
func (r *ProxyFSRepository) RemoveRedirect(id uuid.UUID) error { | ||
delete(r.redirects, id) | ||
return r.write() | ||
} | ||
|
||
func (r *ProxyFSRepository) read() { | ||
p := path.Join(r.proxyPath, "redirects.json") | ||
file, err := os.ReadFile(p) | ||
|
||
if errors.Is(err, os.ErrNotExist) { | ||
logger.Log("redirects.json doesn't exists or could not be found").Print() | ||
} else if err != nil { | ||
logger.Error(err).Print() | ||
return | ||
} | ||
|
||
err = json.Unmarshal(file, &r.redirects) | ||
if err != nil { | ||
logger.Error(err).Print() | ||
return | ||
} | ||
} | ||
|
||
func (r *ProxyFSRepository) write() error { | ||
p := path.Join(r.proxyPath, "redirects.json") | ||
|
||
bytes, err := json.MarshalIndent(r.redirects, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.WriteFile(p, bytes, os.ModePerm) | ||
} |
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,71 @@ | ||
package router | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
"github.com/vertex-center/vertex/types" | ||
) | ||
|
||
func addProxyRoutes(r *gin.RouterGroup) { | ||
r.GET("/redirects", handleGetRedirects) | ||
r.POST("/redirect", handleAddRedirect) | ||
r.DELETE("/redirect/:id", handleRemoveRedirect) | ||
} | ||
|
||
func handleGetRedirects(c *gin.Context) { | ||
redirects := proxyService.GetRedirects() | ||
c.JSON(http.StatusOK, redirects) | ||
} | ||
|
||
type handleAddRedirectBody struct { | ||
Source string `json:"source"` | ||
Target string `json:"target"` | ||
} | ||
|
||
func handleAddRedirect(c *gin.Context) { | ||
var body handleAddRedirectBody | ||
err := c.BindJSON(&body) | ||
if err != nil { | ||
_ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("failed to parse body: %v", err)) | ||
return | ||
} | ||
|
||
redirect := types.ProxyRedirect{ | ||
Source: body.Source, | ||
Target: body.Target, | ||
} | ||
|
||
err = proxyService.AddRedirect(redirect) | ||
if err != nil { | ||
_ = c.AbortWithError(http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
c.Status(http.StatusOK) | ||
} | ||
|
||
func handleRemoveRedirect(c *gin.Context) { | ||
idString := c.Param("id") | ||
if idString == "" { | ||
_ = c.AbortWithError(http.StatusBadRequest, errors.New("failed to get redirection uuid")) | ||
return | ||
} | ||
|
||
id, err := uuid.Parse(idString) | ||
if err != nil { | ||
_ = c.AbortWithError(http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
err = proxyService.RemoveRedirect(id) | ||
if err != nil { | ||
_ = c.AbortWithError(http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
c.Status(http.StatusOK) | ||
} |
Oops, something went wrong.