@@ -41,11 +41,3 @@ func Init() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get(key string) any {
|
|
||||||
return viper.Get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Set(key string, value any) {
|
|
||||||
viper.Set(key, value)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
var serverDef = server{
|
|
||||||
Address: ":8000",
|
|
||||||
DebugMode: false,
|
|
||||||
FileLogger: false,
|
|
||||||
JwtSecret: "something",
|
|
||||||
}
|
|
||||||
|
|
||||||
var databaseDef = database{
|
|
||||||
Type: "postgres",
|
|
||||||
Host: "",
|
|
||||||
Name: "",
|
|
||||||
Username: "",
|
|
||||||
Password: "",
|
|
||||||
}
|
|
||||||
@@ -7,8 +7,8 @@ type config struct {
|
|||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
DebugMode bool `yaml:"debug_mode"`
|
DebugMode string `yaml:"debug_mode"`
|
||||||
FileLogger bool `yaml:"file_logger"`
|
FileLogger string `yaml:"file_logger"`
|
||||||
JwtSecret string `yaml:"jwt_secret"`
|
JwtSecret string `yaml:"jwt_secret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
data/data.go
12
data/data.go
@@ -1,22 +1,22 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/config"
|
|
||||||
"nixcn-cms/data/drivers"
|
"nixcn-cms/data/drivers"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Database *drivers.DBClient
|
var Database *drivers.DBClient
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
// Init database
|
// Init database
|
||||||
dbType := config.Get("database.type").(string)
|
dbType := viper.GetString("database.type")
|
||||||
exDSN := drivers.ExternalDSN{
|
exDSN := drivers.ExternalDSN{
|
||||||
Host: config.Get("database.host").(string),
|
Host: viper.GetString("database.host"),
|
||||||
Name: config.Get("database.name").(string),
|
Name: viper.GetString("database.name"),
|
||||||
Username: config.Get("database.username").(string),
|
Username: viper.GetString("database.username"),
|
||||||
Password: config.Get("database.password").(string),
|
Password: viper.GetString("database.password"),
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbType != "postgres" {
|
if dbType != "postgres" {
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ package jwt
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"nixcn-cms/config"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Claims struct {
|
type Claims struct {
|
||||||
@@ -17,7 +17,7 @@ type Claims struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func JWTAuth() gin.HandlerFunc {
|
func JWTAuth() gin.HandlerFunc {
|
||||||
var JwtSecret = []byte(config.Get("server.jwt_secret").(string))
|
var JwtSecret = []byte(viper.GetString("server.jwt_secret"))
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
auth := c.GetHeader("Authorization")
|
auth := c.GetHeader("Authorization")
|
||||||
if auth == "" {
|
if auth == "" {
|
||||||
@@ -62,7 +62,7 @@ func JWTAuth() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GenerateToken(userID uuid.UUID, application string) (string, error) {
|
func GenerateToken(userID uuid.UUID, application string) (string, error) {
|
||||||
var JwtSecret = []byte(config.Get("server.jwt_secret").(string))
|
var JwtSecret = []byte(viper.GetString("server.jwt_secret"))
|
||||||
claims := Claims{
|
claims := Claims{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
RegisteredClaims: jwt.RegisteredClaims{
|
RegisteredClaims: jwt.RegisteredClaims{
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -17,7 +18,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateTestToken(userID uuid.UUID, expire time.Duration) string {
|
func generateTestToken(userID uuid.UUID, expire time.Duration) string {
|
||||||
var JwtSecret = []byte(config.Get("server.jwt_secret").(string))
|
var JwtSecret = []byte(viper.GetString("server.jwt_secret"))
|
||||||
claims := Claims{
|
claims := Claims{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
RegisteredClaims: jwt.RegisteredClaims{
|
RegisteredClaims: jwt.RegisteredClaims{
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/config"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
FileLogger := config.Get("server.file_logger").(bool)
|
FileLogger := viper.GetBool("server.file_logger")
|
||||||
DebugMode := config.Get("server.debug_mode").(bool)
|
DebugMode := viper.GetBool("server.debug_mode")
|
||||||
|
|
||||||
if FileLogger {
|
if FileLogger == true {
|
||||||
gin.DisableConsoleColor()
|
gin.DisableConsoleColor()
|
||||||
file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -28,7 +28,7 @@ func Init() {
|
|||||||
log.SetLevel(log.WarnLevel)
|
log.SetLevel(log.WarnLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
if DebugMode {
|
if DebugMode == true {
|
||||||
gin.SetMode(gin.DebugMode)
|
gin.SetMode(gin.DebugMode)
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"nixcn-cms/config"
|
|
||||||
"nixcn-cms/logger"
|
"nixcn-cms/logger"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
@@ -18,14 +18,14 @@ func Start() {
|
|||||||
|
|
||||||
// Start http server
|
// Start http server
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: config.Get("server.address").(string),
|
Addr: viper.GetString("server.address"),
|
||||||
Handler: r,
|
Handler: r,
|
||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
WriteTimeout: 10 * time.Second,
|
WriteTimeout: 10 * time.Second,
|
||||||
MaxHeaderBytes: 1 << 20,
|
MaxHeaderBytes: 1 << 20,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Starting server on " + config.Get("server.address").(string))
|
log.Info("Starting server on " + viper.GetString("server.address"))
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Errorf("Error starting server: %v\n", err)
|
log.Errorf("Error starting server: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user