Skip to content

Commit

Permalink
rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Redish101 committed Nov 14, 2023
0 parents commit 51c8e90
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 0 deletions.
42 changes: 42 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module github.com/retalkgo/retalk

go 1.21.0

require github.com/gofiber/fiber/v2 v2.50.0

require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
gorm.io/driver/sqlite v1.5.4
gorm.io/driver/sqlserver v1.5.2
gorm.io/gorm v1.25.5
)
192 changes: 192 additions & 0 deletions go.sum

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package config

import (
"embed"
"fmt"
"io"
"os"

"gopkg.in/yaml.v3"
)

type ConfigSchema struct {
Server struct {
Port int `json:"port"`
} `json:"server"`
DB struct {
Type string `json:"type"`
DSN string `json:"dsn"`
} `json:"db"`
Status struct {
Enable bool `json:"enable"`
Title string `json:"title"`
} `json:"status"`
}

//go:embed default-config.yml
var defaultConfigFile embed.FS

var configInterface *ConfigSchema

func InitConfig() {
configFile, err := os.Open("./retalk.yml")
if err != nil {
fmt.Println("未发现配置文件, 尝试加载默认配置")
_, _ = defaultConfigFile.ReadFile("default-config.yml")
defaultConfigFileBytes, _ := defaultConfigFile.ReadFile("default-config.yml")
configFile, _ = os.Create("./retalk.yml")
_, _ = configFile.Write(defaultConfigFileBytes)
configFile, err = os.Open("./retalk.yml")
}

configData, err := io.ReadAll(configFile)
if err != nil {
panic(err)
}

var config ConfigSchema
err = yaml.Unmarshal(configData, &config)
if err != nil {
panic(err)
}
configInterface = &config
defer configFile.Close()
}

func Config() *ConfigSchema {
return configInterface
}
12 changes: 12 additions & 0 deletions internal/config/default-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# retalk default config

server:
port: 3000

db:
type: sqlite
dsn: retalk.db

status:
enable: true
title: retalk status
45 changes: 45 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package db

import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)

var dbInterface *gorm.DB

func DB() *gorm.DB {
if dbInterface == nil {
db, err := gorm.Open(sqliteDriver("demo.sql"), &gorm.Config{})
if err != nil {
panic(err.Error())
}
dbInterface = db
}
return dbInterface
}

func sqliteDriver(dsn string) gorm.Dialector {
return sqlite.Open(dsn)
}

func mysqlDriver(dsn string) gorm.Dialector {
return mysql.New(mysql.Config{
DSN: dsn,
})
}

func postgresDriver(dsn string) gorm.Dialector {
return postgres.New(postgres.Config{
DSN: dsn,
PreferSimpleProtocol: true,
})
}

func sqlserverDriver(dsn string) gorm.Dialector {
return sqlserver.New(sqlserver.Config{
DSN: dsn,
})
}
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "github.com/retalkgo/retalk/server"

// retalk
// A comment system
// Copyright by Redish101
// MIT LICENSED

func main() {
server.Start()
}
12 changes: 12 additions & 0 deletions retalk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# retalk default config

server:
port: 3000

db:
type: sqlite
dsn: retalk.db

status:
enable: true
title: retalk status
48 changes: 48 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package server

import (
"fmt"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/monitor"
"github.com/retalkgo/retalk/internal/config"
)

func Start() {
config.InitConfig()

c := config.Config()

app := fiber.New(fiber.Config{
Prefork: true,
AppName: "retalk",
ServerHeader: "retalk",
})

// Middleware

// Logger
app.Use(logger.New(logger.ConfigDefault))

// Monitor
if c.Status.Enable {
var title string
if c.Status.Title != "" {
title = c.Status.Title
} else {
title = "retalk status"
}
app.Get("/status", monitor.New(monitor.Config{
Title: title,
FontURL: "https://jsd.onmicrosoft.cn/npm/@recomponent/[email protected]/dist/recomponent-global.css",
}))
} else {
app.Get("/status", func(c *fiber.Ctx) error {
return c.SendString("未")
})
}

// Listen
app.Listen(fmt.Sprintf(":%d", c.Server.Port))
}
23 changes: 23 additions & 0 deletions templates/status_not_enable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>retalk status</title>
<link rel="stylesheet" href="https://jsd.onmicrosoft.cn/npm/@recomponent/[email protected]/dist/recomponent-global.css">
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 95vh;
font-size:1.25rem;
}
</style>
</head>
<body>
<div class="container">
未启用状态面板
</div>
</body>
</html>

0 comments on commit 51c8e90

Please sign in to comment.