added config file

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-10 10:40:36 +02:00 committed by kolaente
parent 1249987e5f
commit 08b14a71a9
7 changed files with 92 additions and 9 deletions

8
config.ini Normal file
View File

@ -0,0 +1,8 @@
[General]
JWTSecret = blablaGEHEMIN§)!§
[Database]
User = root
Password = jup2000
Host = 127.0.0.1
Database = library2

View File

@ -9,11 +9,19 @@ import (
func main(){
// Set Engine
err := models.SetEngine()
if err != nil {
fmt.Println(err)
}
// Init Config
err = models.SetConfig()
if err != nil {
fmt.Println(err)
}
// Start the webserver
e := routes.NewEcho()
routes.RegisterRoutes(e)
e.Start(":8082")

38
models/config.go Normal file
View File

@ -0,0 +1,38 @@
package models
import (
"github.com/go-ini/ini"
"os"
)
var Config struct{
Database struct{
Host string
User string
Password string
Database string
}
JWTLoginSecret []byte
}
func SetConfig() error{
// File Checks
if _, err := os.Stat("config.ini"); os.IsNotExist(err) {
return err
}
cfg, err := ini.Load("config.ini")
if err != nil {
return err
}
// Database
cfg.Section("Database").MapTo(Config.Database)
// JWT secret
Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String())
return nil
}

View File

@ -0,0 +1,16 @@
package v1
import (
"github.com/labstack/echo"
"github.com/dgrijalva/jwt-go"
"fmt"
)
func CheckToken(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
fmt.Println(user.Valid)
return nil
}

View File

@ -5,6 +5,7 @@ import (
"github.com/dgrijalva/jwt-go"
"time"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
func Login(c echo.Context) error {
@ -18,14 +19,14 @@ func Login(c echo.Context) error {
// 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"))
t, err := token.SignedString(models.Config.JWTLoginSecret)
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]string{
"token": t,
})

View File

@ -5,9 +5,7 @@ import (
"github.com/labstack/echo/middleware"
apiv1 "git.mowie.cc/konrad/Library/routes/api/v1"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/dgrijalva/jwt-go"
"git.mowie.cc/konrad/Library/models"
)
func NewEcho() *echo.Echo {
@ -18,9 +16,6 @@ func NewEcho() *echo.Echo {
Format: "${time_rfc3339}: ${remote_ip} ${method} ${status} ${uri} - ${user_agent}\n",
}))
// Sessions
e.Use(session.Middleware(sessions.NewFilesystemStore("./session.store", []byte("secret"))))
// Static Content
e.Static("/assets", "assets")
@ -44,7 +39,8 @@ func RegisterRoutes(e *echo.Echo) {
// ===== Routes with Authetification =====
// Authetification
a.Use(middleware.JWT([]byte("secret")))
a.Use(middleware.JWT(models.Config.JWTLoginSecret))
a.POST("/tokenTest", apiv1.CheckToken)
// Manage Books
a.POST("/books/add", apiv1.Add)

16
vendor/vendor.json vendored
View File

@ -12,6 +12,18 @@
"revision": "a539ee1a749a2b895533f979515ac7e6e0f5b650",
"revisionTime": "2017-06-08T00:51:49Z"
},
{
"checksumSHA1": "87LEfpY9cOk9CP7pWyIbmQ/6enU=",
"path": "github.com/go-ini/ini",
"revision": "20b96f641a5ea98f2f8619ff4f3e061cff4833bd",
"revisionTime": "2017-08-13T05:15:16Z",
"version": "v1.28.2",
"versionExact": "v1.28.2"
},
{
"path": "github.com/go-ini/iniv1.28.2",
"revision": ""
},
{
"checksumSHA1": "fSlTr0rwLji0DfsUOKYLkGUkiWo=",
"path": "github.com/go-sql-driver/mysql",
@ -141,6 +153,10 @@
"path": "golang.org/x/sys/unix",
"revision": "314a259e304ff91bd6985da2a7149bbf91237993",
"revisionTime": "2017-07-19T03:44:26Z"
},
{
"path": "gopkg.in/ini.v1.28.2",
"revision": ""
}
],
"rootPath": "git.mowie.cc/konrad/Library"