Web 服务

This commit is contained in:
奶爸
2019-12-08 16:59:58 +08:00
parent 5a21ce6ca6
commit d8c4364653
33 changed files with 1112 additions and 21 deletions

24
model/common.go Normal file
View File

@@ -0,0 +1,24 @@
package model
import "time"
// CtxKeyIsUserLogin ..
const CtxKeyIsUserLogin = "ckiul"
// CtxKeyOauth2State ..
const CtxKeyOauth2State = "cko2s"
// Common ..
type Common struct {
ID uint64 `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
// Response ..
type Response struct {
Code uint64 `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Result interface{} `json:"result,omitempty"`
}

42
model/config.go Normal file
View File

@@ -0,0 +1,42 @@
package model
import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// Config ..
type Config struct {
Debug bool
Site struct {
Brand string // 站点名称
CookieName string // 浏览器 Cookie 名称
}
GitHub struct {
Admin string // 管理员登录名
ClientID string
ClientSecret string
}
}
// ReadInConfig ..
func ReadInConfig(path string) (*Config, error) {
viper.SetConfigFile(path)
err := viper.ReadInConfig()
if err != nil {
return nil, err
}
var c Config
err = viper.Unmarshal(&c)
if err != nil {
return nil, err
}
viper.OnConfigChange(func(in fsnotify.Event) {
viper.Unmarshal(&c)
})
go viper.WatchConfig()
return &c, nil
}

52
model/user.go Normal file
View File

@@ -0,0 +1,52 @@
package model
import (
"fmt"
"time"
"github.com/google/go-github/v28/github"
"github.com/naiba/com"
)
// User ...
type User struct {
Common `json:"common,omitempty"`
Login string `gorm:"UNIQUE_INDEX" json:"login,omitempty"` // 登录名
AvatarURL string `json:"avatar_url,omitempty"` // 头像地址
Name string `json:"name,omitempty"` // 昵称
Blog string `json:"blog,omitempty"` // 网站链接
Email string `json:"email,omitempty"` // 邮箱
Hireable bool `json:"hireable,omitempty"`
Bio string `json:"bio,omitempty"` // 个人简介
Token string `gorm:"UNIQUE_INDEX" json:"-"` // 认证 Token
TokenExpired time.Time `json:"token_expired,omitempty"` // Token 过期时间
SuperAdmin bool `json:"super_admin,omitempty"` // 超级管理员
TeamsID []uint64 `gorm:"-"`
}
// NewUserFromGitHub ..
func NewUserFromGitHub(gu *github.User) User {
var u User
u.ID = uint64(gu.GetID())
u.Login = gu.GetLogin()
u.AvatarURL = gu.GetAvatarURL()
u.Name = gu.GetName()
// 昵称为空的情况
if u.Name == "" {
u.Name = u.Login
}
u.Blog = gu.GetBlog()
u.Blog = gu.GetBlog()
u.Email = gu.GetEmail()
u.Hireable = gu.GetHireable()
u.Bio = gu.GetBio()
return u
}
// IssueNewToken ...
func (u *User) IssueNewToken() {
u.Token = com.MD5(fmt.Sprintf("%d%d%s", time.Now().UnixNano(), u.ID, u.Login))
u.TokenExpired = time.Now().AddDate(0, 0, 14)
}