add project kontor-api-echo
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-echo/pkg/schema"
|
||||
"kontor-api-echo/pkg/utils"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type jwtCustomClaims struct {
|
||||
Name string `json:"name"`
|
||||
Admin bool `json:"admin"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func Login(c echo.Context) error {
|
||||
user := c.FormValue("user")
|
||||
pass := c.FormValue("pass")
|
||||
|
||||
var profile schema.Profile
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, _ = schema.GetDatabase()
|
||||
err = db.NewSelect().Model(&profile).Where("email = ?", user).Scan(ctx)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if !utils.ComparePassword(profile.Password, pass) {
|
||||
return echo.ErrUnauthorized
|
||||
}
|
||||
|
||||
// Set custom claims
|
||||
claims := &jwtCustomClaims{
|
||||
"Jon Snow",
|
||||
true,
|
||||
jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 72)),
|
||||
},
|
||||
}
|
||||
|
||||
// Create token with claims
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
|
||||
// Generate encoded token and send it as response.
|
||||
t, err := token.SignedString([]byte("secret"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, echo.Map{"token": t})
|
||||
}
|
||||
|
||||
func restricted(c echo.Context) error {
|
||||
user := c.Get("user").(*jwt.Token)
|
||||
claims := user.Claims.(*jwtCustomClaims)
|
||||
name := claims.Name
|
||||
return c.String(http.StatusOK, "Welcome "+name+"!")
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"kontor-api-echo/pkg/schema"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func SetupComicRoutes(api *echo.Group) {
|
||||
comics := api.Group("/comics")
|
||||
comics.GET("/comics", GetAllComics)
|
||||
comics.GET("/publishers", GetAllPublishers)
|
||||
comics.GET("/comicworks", GetAllComicWorks)
|
||||
}
|
||||
|
||||
func GetAllComics(c echo.Context) error {
|
||||
var comics []schema.Comic
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.NewSelect().Model(&comics).Relation("Publisher").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return echo.ErrInternalServerError
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, comics)
|
||||
}
|
||||
|
||||
func GetAllPublishers(c echo.Context) error {
|
||||
var publishers []schema.Publisher
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.NewSelect().Model(&publishers).Relation("ParentPublisher").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return echo.ErrInternalServerError
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, publishers)
|
||||
}
|
||||
|
||||
func GetAllComicWorks(c echo.Context) error {
|
||||
var comic_works []schema.ComicWork
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.NewSelect().Model(&comic_works).Relation("Comic").Relation("Artist").Relation("WorkType").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return echo.ErrInternalServerError
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, comic_works)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func GetHealth(c echo.Context) error {
|
||||
status := new(Status)
|
||||
status.Status = "ok"
|
||||
return c.JSON(http.StatusOK, status)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kontor-api-echo/pkg/schema"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func SetupMediaRoutes(api *echo.Group) {
|
||||
media := api.Group("/media")
|
||||
media.GET("/files", GetAllFiles)
|
||||
media.POST("/files", AddFile)
|
||||
}
|
||||
|
||||
func GetAllFiles(c echo.Context) error {
|
||||
var files []schema.MediaFile
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = db.NewSelect().Model(&files).Relation("MediaActorFiles").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return echo.ErrInternalServerError
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, files)
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func AddFile(c echo.Context) error {
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
link := new(Link)
|
||||
if err = c.Bind(link); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("URL %s has been sent", link)
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
id := uuid.NewString()
|
||||
timestamp := time.Now()
|
||||
mediafile := &schema.MediaFile{ID: id, CreatedAt: timestamp, UpdatedAt: timestamp, WebLink: fmt.Sprintf("%s", link), Version: 1, ShouldDownload: true, Review: true}
|
||||
_, err = db.NewInsert().Model(mediafile).Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.String(http.StatusCreated, "Link created")
|
||||
}
|
||||
Reference in New Issue
Block a user