This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/cmd/cmd.go

88 lines
2.0 KiB
Go
Raw Normal View History

2021-01-03 03:21:11 +00:00
package cmd
import (
2021-01-10 00:44:02 +00:00
"fmt"
"errors"
"reichard.io/imagini/routes"
2021-01-06 19:36:09 +00:00
"reichard.io/imagini/internal/db"
2021-01-10 00:44:02 +00:00
"reichard.io/imagini/internal/auth"
"reichard.io/imagini/internal/models"
2021-01-08 02:45:59 +00:00
"reichard.io/imagini/internal/config"
2021-01-04 05:16:58 +00:00
2021-01-03 22:31:16 +00:00
"github.com/urfave/cli/v2"
2021-01-10 00:44:02 +00:00
"net/http"
log "github.com/sirupsen/logrus"
2021-01-03 03:21:11 +00:00
)
var CmdServe = cli.Command{
2021-01-03 22:31:16 +00:00
Name: "serve",
Aliases: []string{"s"},
Usage: "Start Imagini web server.",
Action: serveWeb,
2021-01-03 03:21:11 +00:00
}
2021-01-04 05:16:58 +00:00
var CmdDBTest = cli.Command{
2021-01-10 00:44:02 +00:00
Name: "test",
Aliases: []string{"t"},
2021-01-04 05:16:58 +00:00
Usage: "test db.",
Action: testDatabase,
}
2021-01-03 03:21:11 +00:00
func serveWeb(ctx *cli.Context) error {
2021-01-10 00:44:02 +00:00
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")
2021-01-08 02:45:59 +00:00
c := config.NewConfig()
db.ConnectDB(c)
2021-01-03 22:31:16 +00:00
2021-01-10 00:44:02 +00:00
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")
}
2021-01-08 02:45:59 +00:00
2021-01-10 00:44:02 +00:00
resp = auth.AuthenticateUser("user@evan.pub", "myPassword123")
if resp == true {
log.Info("USER SUCCESSFULLY AUTHENTICATED BY EMAIL")
}else {
log.Info("USER NOT AUTHENTICATED")
}
2021-01-08 02:45:59 +00:00
2021-01-10 00:44:02 +00:00
resp = auth.AuthenticateUser("user@evan.pub", "myPassword12")
if resp == true {
log.Info("USER SUCCESSFULLY AUTHENTICATED BY EMAIL")
}else {
log.Info("USER NOT AUTHENTICATED")
}
2021-01-08 02:45:59 +00:00
2021-01-10 00:44:02 +00:00
// 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)
2021-01-08 02:45:59 +00:00
// }
2021-01-04 05:16:58 +00:00
return nil
}