v3
veypi 6 months ago
parent 0e8ef78957
commit d472464d8a

@ -6,4 +6,4 @@
#
run:
@go run ./cli/*.go -f ./cfg/dev.yml -l debug
@go run ./cli/*.go -f ./cfg/dev.yml -l debug -p 4001

@ -8,8 +8,8 @@ package access
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type createOpts struct {

@ -8,8 +8,8 @@ package access
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type deleteOpts struct {

@ -8,8 +8,8 @@ package access
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Get("/:id", getAccess)

@ -8,8 +8,8 @@ package access
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type listOpts struct {

@ -7,9 +7,9 @@
package access
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
)
type updateOpts struct {

@ -3,12 +3,12 @@ package app
import (
"fmt"
"math/rand"
"oa/cfg"
"oa/errs"
"oa/libs/auth"
"oa/models"
"time"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/errs"
"github.com/veypi/OneAuth/libs/auth"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"github.com/veypi/utils"
"gorm.io/gorm"

@ -2,8 +2,8 @@ package app_user
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type createOpts struct {

@ -1,9 +1,9 @@
package app_user
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
)
type deleteOpts struct {

@ -1,9 +1,9 @@
package app_user
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
)
var _ = Router.Get("/:user_id", getAppUser)

@ -2,8 +2,8 @@ package app_user
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type listOpts struct {

@ -2,8 +2,8 @@ package app_user
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type updateOpts struct {

@ -8,11 +8,10 @@
package app
import (
"oa/api/app/access"
"oa/api/app/app_user"
"oa/api/app/resource"
"oa/api/user/role"
"github.com/veypi/OneAuth/api/app/access"
"github.com/veypi/OneAuth/api/app/app_user"
"github.com/veypi/OneAuth/api/app/resource"
"github.com/veypi/OneAuth/api/app/role"
"github.com/veypi/OneBD/rest"
)

@ -2,8 +2,8 @@ package resource
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Post("/", createResource)

@ -2,8 +2,8 @@ package resource
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Delete("/:resource_id", deleteResource)

@ -2,8 +2,8 @@ package resource
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Get("/:resource_id", getResource)

@ -2,8 +2,8 @@ package resource
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type listOpts struct {

@ -2,8 +2,8 @@ package resource
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Patch("/:resource_id", updateResource)

@ -1,9 +1,9 @@
package role
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
)
var _ = Router.Post("/", createRole)

@ -2,8 +2,8 @@ package role
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Delete("/:role_id", deleteRole)

@ -2,8 +2,8 @@ package role
import (
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
var _ = Router.Get("/:role_id", getRole)

@ -1,9 +1,9 @@
package role
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
)
var _ = Router.Get("/", listRoles)

@ -1,9 +1,9 @@
package role
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/models"
"oa/cfg"
)
var _ = Router.Patch("/:role_id", updateRole)

@ -9,10 +9,10 @@ package api
import (
"fmt"
"oa/api/app"
"oa/api/token"
"oa/api/user"
"github.com/veypi/OneAuth/api/app"
"github.com/veypi/OneAuth/api/token"
"github.com/veypi/OneAuth/api/user"
"github.com/veypi/OneBD/rest"
"github.com/veypi/OneBD/rest/middlewares"
)
@ -20,7 +20,7 @@ import (
var Router = rest.NewRouter().UseAfter(middlewares.JsonResponse, middlewares.JsonErrorResponse)
var (
_ = Router.Extend("user", user.Router)
_ = Router.Extend("user", user.Router,)
_ = Router.Extend("token", token.Router)
_ = Router.Extend("app", app.Router)
)

@ -1,10 +1,10 @@
package token
import (
"oa/cfg"
"oa/models"
"time"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
)

@ -9,12 +9,12 @@ package token
import (
"net/http"
"oa/cfg"
"oa/libs/auth"
"oa/models"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/libs/auth"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"github.com/veypi/utils/logv"
)

@ -8,9 +8,8 @@
package token
import (
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
)

@ -10,14 +10,14 @@ package user
import (
"encoding/base64"
"fmt"
"oa/cfg"
M "oa/models"
"strings"
"time"
"math/rand"
"github.com/google/uuid"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"github.com/veypi/utils"
"gorm.io/gorm"
@ -40,7 +40,7 @@ func userPost(x *rest.X) (any, error) {
if err != nil {
return nil, err
}
data := &M.User{}
data := &models.User{}
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.Username = opts.Username
@ -82,7 +82,7 @@ func userPost(x *rest.X) (any, error) {
if err != nil {
return err
}
app := &M.App{}
app := &models.App{}
err = tx.Where("id = ?", cfg.Config.ID).First(app).Error
if err != nil {
return err
@ -98,7 +98,7 @@ func userPost(x *rest.X) (any, error) {
if app.Typ != "public" {
}
return tx.Create(&M.AppUser{
return tx.Create(&models.AppUser{
UserID: data.ID,
AppID: cfg.Config.ID,
Status: status,

@ -8,8 +8,7 @@
package user
import (
"oa/api/user/role"
"github.com/veypi/OneAuth/api/user/role"
"github.com/veypi/OneBD/rest"
)

@ -9,12 +9,12 @@ package user
import (
"encoding/base64"
"oa/cfg"
"oa/libs/auth"
"oa/models"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/libs/auth"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"github.com/veypi/utils"
"github.com/veypi/utils/logv"

@ -8,9 +8,9 @@
package role
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/cfg"
M "oa/models"
)
var _ = Router.Post("/", userRolePost)
@ -28,7 +28,7 @@ func userRolePost(x *rest.X) (any, error) {
if err != nil {
return nil, err
}
data := &M.UserRole{}
data := &models.UserRole{}
data.UserID = opts.UserID
data.RoleID = opts.RoleID

@ -8,9 +8,8 @@
package role
import (
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
)

@ -8,8 +8,8 @@
package role
import (
"oa/cfg"
M "oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
)
@ -19,7 +19,7 @@ var _ = Router.Get("/:id", `
`, userRoleGet)
func userRoleGet(x *rest.X) (any, error) {
data := &M.UserRole{}
data := &models.UserRole{}
err := cfg.DB().Where("id = ?", x.Params.Get("id")).First(data).Error
return data, err
}
@ -42,7 +42,7 @@ func userRoleList(x *rest.X) (any, error) {
return nil, err
}
data := make([]*struct {
M.UserRole
models.UserRole
Username string `json:"username"`
Nickname string `json:"nickname"`
Icon string `json:"icon"`

@ -8,9 +8,9 @@
package role
import (
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"oa/cfg"
M "oa/models"
)
type patchOpts struct {
@ -26,8 +26,7 @@ func userRolePatch(x *rest.X) (any, error) {
if err != nil {
return nil, err
}
data := &M.UserRole{}
data := &models.UserRole{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {
return nil, err

@ -1,17 +1,16 @@
package user
import (
"oa/cfg"
"oa/libs/auth"
M "oa/models"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/libs/auth"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
)
var _ = Router.Delete("/:user_id", auth.Check("user", "user_id", auth.DoDelete), userDelete)
func userDelete(x *rest.X) (any, error) {
data := &M.User{}
data := &models.User{}
err := cfg.DB().Where("id = ?", x.Params.Get("user_id")).Delete(data).Error
return data, err
}
@ -28,14 +27,14 @@ func userGet(x *rest.X) (any, error) {
if err != nil {
return nil, err
}
data := &M.User{}
data := &models.User{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err
}
var _ = Router.Get("/", auth.Check("user", "", auth.DoRead), userList)
var _ = Router.Get("/", "list user", listOpts{}, auth.Check("user", "", auth.DoRead), userList)
type listOpts struct {
Username *string `json:"username" parse:"query"`
@ -51,7 +50,7 @@ func userList(x *rest.X) (any, error) {
if err != nil {
return nil, err
}
data := make([]*M.User, 0, 10)
data := make([]*models.User, 0, 10)
query := cfg.DB()
if opts.Username != nil {
@ -92,7 +91,7 @@ func userPatch(x *rest.X) (any, error) {
if err != nil {
return nil, err
}
data := &M.User{}
data := &models.User{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {

@ -7,15 +7,11 @@
package cfg
type config struct {
Host string `json:"host"`
Port int `json:"port"`
LoggerPath string `json:"logger_path,omitempty"`
LoggerLevel string `json:"logger_level,omitempty"`
DSN string `json:"dsn"`
DB string `json:"db"`
type Options struct {
DSN string `json:"dsn"` // Data Source Name
DB string `json:"db"` // DB type: mysql, postgres, sqlite
ID string `json:"id"`
Key string `json:"key"`
}
var Config = &config{}
var Config = &Options{}

@ -8,57 +8,65 @@
package main
import (
app "oa"
"oa/cfg"
"oa/models"
"github.com/veypi/OneAuth"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
"github.com/veypi/utils/flags"
"github.com/veypi/utils/logv"
)
var CMD = flags.New("app", "the backend server of app")
var cmdCfg = CMD.SubCommand("cfg", "generate cfg file")
var cmdDB = CMD.SubCommand("db", "database operations")
var cmdMain = flags.New("app", "the backend server of app")
var cmdCfg = cmdMain.SubCommand("cfg", "generate cfg file")
var cmdDB = cmdMain.SubCommand("db", "database operations")
var configFile = cmdMain.String("f", "./dev.yaml", "the config file")
var configFile = CMD.String("f", "./dev.yaml", "the config file")
var cliOpts = &struct {
Host string `json:"host"`
Port int `json:"port"`
LoggerPath string `json:"logger_path,omitempty"`
LoggerLevel string `json:"logger_level,omitempty"`
Core *cfg.Options
}{
Core: cfg.Config,
}
func init() {
CMD.StringVar(&cfg.Config.Host, "host", "0.0.0.0", "host")
CMD.IntVar(&cfg.Config.Port, "p", 4000, "port")
CMD.StringVar(&cfg.Config.LoggerLevel, "l", "info", "log level")
CMD.StringVar(&cfg.Config.DSN, "dsn", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local", "data source name")
CMD.StringVar(&cfg.Config.DB, "db", "mysql", "data source type: mysql/postgre/sqlite")
CMD.Before = func() error {
cmdMain.StringVar(&cliOpts.Host, "host", "0.0.0.0", "host")
cmdMain.IntVar(&cliOpts.Port, "p", 4000, "port")
cmdMain.StringVar(&cliOpts.LoggerLevel, "l", "info", "log level")
cmdMain.Before = func() error {
flags.LoadCfg(*configFile, cfg.Config)
CMD.Parse()
logv.SetLevel(logv.AssertFuncErr(logv.ParseLevel(cfg.Config.LoggerLevel)))
cmdMain.Parse()
logv.SetLevel(logv.AssertFuncErr(logv.ParseLevel(cliOpts.LoggerLevel)))
return nil
}
CMD.Command = runWeb
cmdMain.Command = runWeb
cmdCfg.Command = func() error {
flags.DumpCfg(*configFile, cfg.Config)
return nil
return flags.DumpCfg(*configFile, cfg.Config)
}
cmdDB.SubCommand("migrate", "migrate database").Command = models.Migrate
cmdDB.SubCommand("drop", "drop database").Command = models.Drop
cmdDB.SubCommand("init", "init db data").Command = models.Init
cmdDB.SubCommand("init", "init db data").Command = models.InitDB
}
func main() {
CMD.Parse()
err := CMD.Run()
cmdMain.Parse()
err := cmdMain.Run()
if err != nil {
logv.Warn().Msg(err.Error())
}
}
func runWeb() error {
server, err := rest.New(rest.WithHost(cfg.Config.Host), rest.WithPort(cfg.Config.Port))
server, err := rest.New(rest.WithHost(cliOpts.Host), rest.WithPort(cliOpts.Port))
if err != nil {
return err
}
server.SetRouter(app.Router)
server.SetRouter(OneAuth.Router)
server.EnableAI()
server.Router().Print()
return server.Run()
}

@ -1,4 +1,4 @@
module oa
module github.com/veypi/OneAuth
go 1.23.2

@ -4,12 +4,11 @@
// Distributed under terms of the MIT license.
//
package oa
package OneAuth
import (
"embed"
"oa/api"
"github.com/veypi/OneAuth/api"
"github.com/veypi/OneBD/rest"
"github.com/veypi/OneBD/rest/middlewares/vyes"
"github.com/veypi/vyes-ui"

@ -11,12 +11,12 @@ import (
"context"
"errors"
"fmt"
"oa/cfg"
"oa/errs"
"strings"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/errs"
"github.com/veypi/OneBD/rest"
)

@ -32,7 +32,7 @@ import (
// In the long term, this package should use the standard library's version
// only, and the internal fork deleted, once
// https://github.com/golang/go/issues/13400 is resolved.
ixml "oa/libs/webdav/internal/xml"
ixml "github.com/veypi/OneAuth/libs/webdav/internal/xml"
)
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_lockinfo

@ -16,7 +16,7 @@ import (
"strings"
"testing"
ixml "oa/libs/webdav/internal/xml"
ixml "github.com/veypi/OneAuth/libs/webdav/internal/xml"
)
func TestReadLockInfo(t *testing.T) {

@ -7,7 +7,7 @@
package models
import (
"oa/cfg"
"github.com/veypi/OneAuth/cfg"
"strings"
"time"
@ -31,7 +31,6 @@ func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
return nil
}
var AllModels = crud.New()
func init() {
@ -51,7 +50,7 @@ func Drop() error {
return crud.AutoDrop(cfg.DB(), AllModels)
}
func Init() error {
func InitDB() error {
app := &App{}
app.ID = cfg.Config.ID
app.Name = "OA"

@ -1,36 +1,71 @@
<!DOCTYPE html>
<html>
<style>
.logout-btn {
padding: 5px 10px;
background: #fff;
color: #1e88e5;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.logout-btn:hover {
background: #f5f5f5;
}
<head>
<meta name="description" content="用户头像和登录状态组件">
</head>
<style>
.header-user {
display: flex;
align-items: center;
gap: 10px;
gap: 12px;
padding: 8px 12px;
border-radius: 20px;
transition: all 0.2s ease;
}
.header-user:hover {
background: rgba(0, 0, 0, 0.05);
}
.user-avatar {
width: 32px;
height: 32px;
width: 36px;
height: 36px;
border-radius: 50%;
object-fit: cover;
border: 2px solid #f8f9fa;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.user-name {
font-size: 14px;
font-weight: 500;
color: #333;
text-decoration: none;
}
.user-name:hover {
color: #1e88e5;
}
.auth-btn {
padding: 6px 16px;
border-radius: 20px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s ease;
}
.login-btn {
background: #1e88e5;
color: white;
border: none;
}
.login-btn:hover {
background: #1565c0;
}
.logout-btn {
background: transparent;
color: #f44336;
border: 1px solid #f44336;
}
.logout-btn:hover {
background: rgba(244, 67, 54, 0.1);
}
</style>
@ -38,18 +73,20 @@
<div class="header-user ml-auto" v-if="user.name">
<img :src="user.icon" class="user-avatar" alt="用户头像">
<a href='/profile' class="user-name">{{ user.name }}</a>
<v-btn size='sm' color='#f1acac' typ='outline' @click="token.logout($env.root)" class="logout-btn">
<button @click="token.logout($env.root)" class="auth-btn logout-btn">
退出
</v-btn>
</button>
</div>
<div class="header-user" v-else>
<span>未登录</span>
<a :href="'/login'" class="logout-btn">
<a href="/login" class="auth-btn login-btn">
登录
</a>
</div>
</body>
<script setup>
import token from 'token'
user = token.body() || {}
</script>
</html>

@ -4,12 +4,10 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>oa</title>
<link rel="stylesheet" href="/assets/common.css">
<link rel="stylesheet" href="/assets/tailwind.min.css">
<script type="module" key='vyes' src="/assets/v.js"></script>
<link rel="stylesheet" href="/assets/awesomefont/css/all.min.css"
integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link href="/assets/libs/tailwind/tailwind.min.css" rel="stylesheet">
<link href="/assets/libs/animate/animate.min.css" rel="stylesheet">
<link href="/assets/libs/font-awesome/css/all.min.css" rel="stylesheet">
</head>
<body root class="h-full w-full">

Loading…
Cancel
Save