-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
e5793e1
commit d48cad0
Showing
5 changed files
with
82 additions
and
79 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/TUM-Dev/Campus-Backend/server/backend/migration" | ||
gormlogrus "github.com/onrik/gorm-logrus" | ||
log "github.com/sirupsen/logrus" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// SetupDB connects to the database and migrates it if necessary | ||
func SetupDB() *gorm.DB { | ||
dbHost := os.Getenv("DB_DSN") | ||
if dbHost == "" { | ||
log.Fatal("Failed to start! The 'DB_DSN' environment variable is not defined. Take a look at the README.md for more details.") | ||
} | ||
|
||
log.Info("Connecting to dsn") | ||
db, err := gorm.Open(mysql.Open(dbHost), &gorm.Config{Logger: gormlogrus.New()}) | ||
if err != nil { | ||
log.WithError(err).Fatal("failed to connect database") | ||
} | ||
|
||
// Migrate the schema | ||
// currently not activated as | ||
if err := migration.New(db, false).Migrate(); err != nil { | ||
log.WithError(err).Fatal("Failed to migrate database") | ||
} | ||
return db | ||
} |
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,49 @@ | ||
package utils | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func GrpcErrorToWebError(err error) ErrorResponse { | ||
errorResp := ErrorResponse{Error: "Internal Server Error", StatusCode: http.StatusInternalServerError} | ||
|
||
if strings.HasPrefix(err.Error(), "no device id") { | ||
errorResp.Error = "Not Authorized" | ||
errorResp.StatusCode = http.StatusForbidden | ||
} | ||
|
||
if s, ok := status.FromError(err); ok { | ||
switch s.Code() { | ||
case codes.NotFound: | ||
errorResp.Error = "Not Found" | ||
errorResp.StatusCode = http.StatusNotFound | ||
case codes.Unimplemented: | ||
errorResp.Error = "Not Implemented" | ||
errorResp.StatusCode = http.StatusNotImplemented | ||
case codes.InvalidArgument: | ||
errorResp.Error = "Invalid Argument" | ||
errorResp.StatusCode = http.StatusBadRequest | ||
case codes.Internal: | ||
errorResp.Error = "Internal Server Error" | ||
errorResp.StatusCode = http.StatusInternalServerError | ||
} | ||
|
||
if s.Message() != "" { | ||
errorResp.Error = s.Message() | ||
} | ||
|
||
if s.Details() != nil && len(s.Details()) > 0 { | ||
errorResp.Details = s.Details() | ||
} | ||
} | ||
return errorResp | ||
} | ||
|
||
type ErrorResponse struct { | ||
Error string `json:"error"` | ||
Details []interface{} `json:"details,omitempty"` | ||
StatusCode int `json:"-"` | ||
} |
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