Added basic insert with auth
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
b8ab28d39d
commit
718f6bf6bb
|
@ -0,0 +1,6 @@
|
|||
package models
|
||||
|
||||
func AddBook(book Book) (err error){
|
||||
_, err = x.Insert(&book)
|
||||
return err
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Add(c echo.Context) error {
|
||||
book := c.FormValue("book")
|
||||
if book == "" {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
|
||||
}
|
||||
|
||||
var bookstruct models.Book
|
||||
dec := json.NewDecoder(strings.NewReader(book))
|
||||
|
||||
err := dec.Decode(&bookstruct)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book"})
|
||||
}
|
||||
|
||||
err = models.AddBook(bookstruct)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, models.Message{"success"})
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"time"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Login(c echo.Context) error {
|
||||
username := c.FormValue("username")
|
||||
password := c.FormValue("password")
|
||||
|
||||
if username == "jon" && password == "shhh!" {
|
||||
// Create token
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
|
||||
// Set claims
|
||||
claims := token.Claims.(jwt.MapClaims)
|
||||
claims["name"] = "Jon Snow"
|
||||
claims["admin"] = true
|
||||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
||||
|
||||
// Generate encoded token and send it as response.
|
||||
t, err := token.SignedString([]byte("secret"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"token": t,
|
||||
})
|
||||
}
|
||||
|
||||
return echo.ErrUnauthorized
|
||||
}
|
|
@ -4,17 +4,23 @@ import (
|
|||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/middleware"
|
||||
apiv1 "git.mowie.cc/konrad/Library/routes/api/v1"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
)
|
||||
|
||||
func NewEcho() *echo.Echo {
|
||||
e := echo.New()
|
||||
|
||||
//Logger
|
||||
// Logger
|
||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||
Format: "${time_rfc3339}: ${remote_ip} ${method} ${status} ${uri} - ${user_agent}\n",
|
||||
}))
|
||||
|
||||
//Static Content
|
||||
// Sessions
|
||||
e.Use(session.Middleware(sessions.NewFilesystemStore("./session.store", []byte("secret"))))
|
||||
|
||||
// Static Content
|
||||
e.Static("/assets", "assets")
|
||||
|
||||
return e
|
||||
|
@ -26,10 +32,17 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a := e.Group("/api/v1")
|
||||
a.GET("/books/list", apiv1.List)
|
||||
|
||||
// Login Route
|
||||
e.POST("/login", Login)
|
||||
|
||||
a.Use(middleware.JWT([]byte("secret")))
|
||||
a.POST("/books/add", apiv1.Add)
|
||||
|
||||
/*
|
||||
Alles nur mit Api machen, davor dann einen onepager mit vue.js.
|
||||
|
||||
(Alles mit | benötigt Authentifizierung)
|
||||
|
||||
Routes:
|
||||
/ - entweder übersicht anzeigen (wenn der nutzer eingeloggt ist) oder auf /login weiterleiten
|
||||
/login - Einloggen
|
||||
|
@ -59,4 +72,4 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
/user/:id/delete - |nutzer löschen
|
||||
/user/:id/edit - |nutzer bearbeiten
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue