123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package handlers
- import (
- "fmt"
- "git.linuxforward.com/byom/byom-gateway/store"
- "github.com/gin-gonic/gin"
- "github.com/sirupsen/logrus"
- )
- type UserHandler struct {
- entry *logrus.Entry
- db *store.DataStore
- pricingPage string
- }
- // this handlers is gonna be used in order to check
- // if a specified user using its email has an hostname associated
- func NewUserHandler(db *store.DataStore, pricingPage string) *UserHandler {
- return &UserHandler{
- entry: logrus.WithField("handler", "user-host"),
- db: db,
- pricingPage: pricingPage,
- }
- }
- func (u *UserHandler) GetUserHost(c *gin.Context) {
- email := c.Query("email")
- if email == "" {
- c.JSON(400, gin.H{"error": "email is required"})
- return
- }
- host, err := u.db.GetUserHost(email)
- fmt.Printf("Host from DB: %v, err: %v\n", host, err) // Add this log
- if err != nil || host == "" {
- c.JSON(200, gin.H{"redirect": u.pricingPage})
- return
- }
- c.JSON(200, gin.H{"redirect": "https://" + host + "/login"})
- }
- func (u *UserHandler) AddUserHost(c *gin.Context) {
- var req struct {
- Email string `json:"email"`
- Host string `json:"host"`
- }
- if err := c.BindJSON(&req); err != nil {
- c.JSON(400, gin.H{"error": err.Error()})
- return
- }
- if req.Email == "" || req.Host == "" {
- c.JSON(400, gin.H{"error": "email is required"})
- return
- }
- if err := u.db.AddUserHost(req.Email, req.Host); err != nil {
- c.JSON(500, gin.H{"error": err.Error()})
- return
- }
- c.JSON(200, gin.H{"status": "ok"})
- }
- func (u *UserHandler) DeleteUserHost(c *gin.Context) {
- email := c.Query("email")
- if email == "" {
- c.JSON(400, gin.H{"error": "email is required"})
- return
- }
- if err := u.db.DeleteUserHost(email); err != nil {
- c.JSON(500, gin.H{"error": err.Error()})
- return
- }
- c.JSON(200, gin.H{"status": "ok"})
- }
- func (u *UserHandler) UpdateUserHost(c *gin.Context) {
- var req struct {
- Email string `json:"email"`
- Host string `json:"host"`
- }
- if err := c.BindJSON(&req); err != nil {
- c.JSON(400, gin.H{"error": err.Error()})
- return
- }
- if req.Email == "" || req.Host == "" {
- c.JSON(400, gin.H{"error": "email is required"})
- return
- }
- if err := u.db.UpdateUserHost(req.Email, req.Host); err != nil {
- c.JSON(500, gin.H{"error": err.Error()})
- return
- }
- c.JSON(200, gin.H{"status": "ok"})
- }
|