88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"errors"
|
|
"reichard.io/imagini/routes"
|
|
"reichard.io/imagini/internal/db"
|
|
"reichard.io/imagini/internal/auth"
|
|
"reichard.io/imagini/internal/models"
|
|
"reichard.io/imagini/internal/config"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
"net/http"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var CmdServe = cli.Command{
|
|
Name: "serve",
|
|
Aliases: []string{"s"},
|
|
Usage: "Start Imagini web server.",
|
|
Action: serveWeb,
|
|
}
|
|
|
|
var CmdDBTest = cli.Command{
|
|
Name: "test",
|
|
Aliases: []string{"t"},
|
|
Usage: "test db.",
|
|
Action: testDatabase,
|
|
}
|
|
|
|
func serveWeb(ctx *cli.Context) error {
|
|
log.Info("Serving Web")
|
|
routes.RegisterRoutes()
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func testDatabase(ctx *cli.Context) error {
|
|
log.Info("Testing Database")
|
|
c := config.NewConfig()
|
|
db.ConnectDB(c)
|
|
|
|
err := auth.CreateUser(models.User{
|
|
Username: "User12346",
|
|
Email: "user26@evan.pub",
|
|
FirstName: "User",
|
|
LastName: "Reichard",
|
|
AuthType: "Local",
|
|
}, "myPassword123")
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
resp := auth.AuthenticateUser("User123", "myPassword123")
|
|
if resp == true {
|
|
log.Info("USER SUCCESSFULLY AUTHENTICATED BY USERNAME")
|
|
}else {
|
|
log.Info("USER NOT AUTHENTICATED")
|
|
}
|
|
|
|
resp = auth.AuthenticateUser("user@evan.pub", "myPassword123")
|
|
if resp == true {
|
|
log.Info("USER SUCCESSFULLY AUTHENTICATED BY EMAIL")
|
|
}else {
|
|
log.Info("USER NOT AUTHENTICATED")
|
|
}
|
|
|
|
resp = auth.AuthenticateUser("user@evan.pub", "myPassword12")
|
|
if resp == true {
|
|
log.Info("USER SUCCESSFULLY AUTHENTICATED BY EMAIL")
|
|
}else {
|
|
log.Info("USER NOT AUTHENTICATED")
|
|
}
|
|
|
|
// foundUser, err := db.GetUser(db.User{Username: "User123"})
|
|
|
|
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
// log.Warn("RECORD NOT FOUND")
|
|
// } else {
|
|
// log.Info("FOUND USER", foundUser)
|
|
// }
|
|
|
|
return nil
|
|
}
|