Skip to content

Commit

Permalink
chore: mv config/state => config/define
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jan 6, 2024
1 parent 172ef79 commit c17cb5c
Show file tree
Hide file tree
Showing 30 changed files with 195 additions and 191 deletions.
3 changes: 1 addition & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
FlareData "github.com/soulteary/flare/config/data"
FlareDefine "github.com/soulteary/flare/config/define"
FlareModel "github.com/soulteary/flare/config/model"
FlareState "github.com/soulteary/flare/config/state"
FlareLogger "github.com/soulteary/flare/internal/logger"
"github.com/soulteary/flare/internal/version"
)
Expand Down Expand Up @@ -43,7 +42,7 @@ func Parse() FlareModel.Flags {
}
}

FlareState.AppFlags = flags
FlareDefine.AppFlags = flags
return flags
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestParse(t *testing.T) {
parsedEnvs := map[string]string{}
expectedFlags := FlareModel.Flags{}

defaults := FlareDefine.GetDefaultEnvVars()
defaults := FlareDefine.DefaultEnvVars
expectedFlags.User = defaults.User
expectedFlags.Port = defaults.Port
expectedFlags.EnableGuide = defaults.EnableGuide
Expand Down
2 changes: 1 addition & 1 deletion cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ParseEnvVars() (stor FlareModel.Flags) {
log := FlareLogger.GetLogger()

// 1. init default values
defaults := FlareDefine.GetDefaultEnvVars()
defaults := FlareDefine.DefaultEnvVars

// 2. overwrite with user input
if err := env.Parse(&defaults); err != nil {
Expand Down
11 changes: 6 additions & 5 deletions cmd/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func TestParseEnvVars(t *testing.T) {
os.Setenv("FLARE_OFFLINE", ")))))))@#$%^&*()")
defer os.Unsetenv("FLARE_OFFLINE")
flags = FlareCMD.ParseEnvVars()
defaultEnvs := FlareDefine.GetDefaultEnvVars()
defaultEnvs := FlareDefine.DefaultEnvVars
assert.Equal(t, flags.EnableOfflineMode, defaultEnvs.EnableOfflineMode)
}

func TestInitAccountFromEnvVars_normal(t *testing.T) {
defaultEnvs := FlareDefine.GetDefaultEnvVars()
defaultEnvs := FlareDefine.DefaultEnvVars

err := env.Parse(&defaultEnvs)
assert.Nil(t, err, "TestInitAccountFromEnvVars Faild")
Expand All @@ -68,7 +68,7 @@ func TestInitAccountFromEnvVars_normal(t *testing.T) {
}

func TestInitAccountFromEnvVars_EmptyUser(t *testing.T) {
defaultEnvs := FlareDefine.GetDefaultEnvVars()
defaultEnvs := FlareDefine.DefaultEnvVars

err := env.Parse(&defaultEnvs)
assert.Nil(t, err, "TestInitAccountFromEnvVars Faild")
Expand All @@ -92,10 +92,11 @@ func TestInitAccountFromEnvVars_EmptyUser(t *testing.T) {
}

func TestInitAccountFromEnvVars_EmptyPass(t *testing.T) {
defaultEnvs := FlareDefine.GetDefaultEnvVars()
defaultEnvs := FlareDefine.DefaultEnvVars

err := env.Parse(&defaultEnvs)
assert.Nil(t, err, "TestInitAccountFromEnvVars Faild")

var target FlareModel.Flags

// 4. test empty password
Expand All @@ -115,7 +116,7 @@ func TestInitAccountFromEnvVars_EmptyPass(t *testing.T) {
}

func TestInitAccountFromEnvVars_Pass(t *testing.T) {
defaultEnvs := FlareDefine.GetDefaultEnvVars()
defaultEnvs := FlareDefine.DefaultEnvVars

err := env.Parse(&defaultEnvs)
assert.Nil(t, err, "TestInitAccountFromEnvVars Faild")
Expand Down
2 changes: 1 addition & 1 deletion cmd/envfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func ParseEnvFile(baseFlags FlareModel.Flags) FlareModel.Flags {
user := GetDotEnvFileStringOrDefault(envs, "FLARE_USER", baseFlags.User)
baseFlags.User = user

defaults := FlareDefine.GetDefaultEnvVars()
defaults := FlareDefine.DefaultEnvVars

if user == defaults.User {
baseFlags.UserIsGenerated = false
Expand Down
2 changes: 1 addition & 1 deletion cmd/envfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestParseEnvFile_User(t *testing.T) {
os.Setenv("FLARE_DEBUG", "true")
defer os.Unsetenv("FLARE_DEBUG")

defaults := FlareDefine.GetDefaultEnvVars()
defaults := FlareDefine.DefaultEnvVars

envParsed := FlareCMD.ParseEnvVars()
envPath := FlareFn.GetWorkDirFile(".env")
Expand Down
10 changes: 10 additions & 0 deletions config/define/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
DEFAULT_DISABLE_CSP = false
)

// get default env config
func GetDefaultEnvVars() FlareModel.Envs {
return FlareModel.Envs{
Port: DEFAULT_PORT,
Expand All @@ -33,3 +34,12 @@ func GetDefaultEnvVars() FlareModel.Envs {
Pass: "",
}
}

var DefaultEnvVars = GetDefaultEnvVars()

var AppFlags FlareModel.Flags

// VISIABLE Levels
// - "DEFAULT"
// - "PRIVATE"
var FLARE_VISIABLE = "PRIVATE"
6 changes: 6 additions & 0 deletions config/define/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
Flare
Define all program define (consts)
*/
package FlareDefine
2 changes: 1 addition & 1 deletion config/state/router.go → config/define/router.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package FlareState
package FlareDefine

import (
FlareModel "github.com/soulteary/flare/config/model"
Expand Down
2 changes: 1 addition & 1 deletion config/state/style.go → config/define/style.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package FlareState
package FlareDefine

const PAGE_INLINE_STYLE = "/** 月出惊山鸟,时鸣春涧中。**/ * {margin: 0;padding: 0;box-sizing: border-box;}body {--color-background: #2d3436;--color-primary: #effbff;--color-accent: #ffa500;--spacing-ui: 10px;background-color: var(--color-background);transition: background-color 0.3s;font-family: Roboto, sans-serif;font-size: 14px;}h1,h2,h3,h4,h5,div,p,a,span {color: var(--color-primary);}a {text-decoration: none;}.clearfix::after {content: \"\";clear: both;display: table;}h1 {font-weight: 700;font-size: 4em;display: inline-block;}.app-content-uppercase h2 a,.app-content-uppercase .plugin-container h2 {text-transform: uppercase;}h2 a {font-weight: 900;font-size: 20px;margin-bottom: 16px;}.pageview {position: absolute;left: 0;right: 0;top: 0;bottom: 0;display: block;}#page-home {display: block;overflow: auto;}.container {padding: 25px 40px;width: 90%;margin: 0 auto;}.pull-left {float: left;}.pull-right {float: right;}.no-select {user-select: none;}#container-apps,#container-bookmakrs {margin: 2rem 0;}.plugin-container h2 {font-weight: 900;font-size: 20px;margin-bottom: 16px;}#page-home .footer-container {text-align: center;bottom: 10px;right: 40px;opacity: 0.6;}#page-home .footer-container:hover {opacity: 1;}#page-home.pageview .container {min-width: 360px;}@media (max-width: 1200px) {#page-home.pageview .container {width: 90%;}}@media (max-width: 768px) {#page-home.pageview .container {width: 90%;padding: 20px;margin: 0 auto;}}@media (max-width: 375px) {#page-home.pageview .container {width: 100%;padding: 20px;margin: 0 auto;}}@media (min-width: 1201px) {#page-home.pageview .container {width: 90%;padding: 50px 250px;}}#container-apps .apps-container {width: 100%;}#container-apps .apps-container .app-container {float: left;width: 25%;}#container-apps .apps-container .app-item {display: block;padding: 2px 4px;margin: 12px 0;height: 40px;overflow: hidden;position: relative;}#container-apps .apps-container .app-item:hover {background-color: rgba(0, 0, 0, 0.2);border-radius: 4px;transition: all .1s;}#container-apps .apps-container .app-icon {width: 35px;height: 35px;position: absolute;left: 5px;}#container-apps .apps-container .app-icon * {width: 90%;color: var(--color-primary);}#container-apps .apps-container .app-icon img{display: block;width: 32px;height: 32px;color: var(--color-primary);}.app-content-uppercase #container-apps .apps-container .app-text {text-transform: uppercase;}#container-apps .apps-container .app-text {margin-left: 35px;}#container-apps .apps-container .app-title {font-size: 1em;font-weight: 500;margin-bottom: -4px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}#container-apps .apps-container .app-desc {color: var(--color-accent);font-weight: 400;font-size: 0.8em;opacity: 1;margin-top: 4px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}@media (max-width: 767px) {#container-apps .apps-container .app-container {width: 50%;}}.app-content-uppercase #container-bookmakrs .bookmark-group-container h3.bookmark-group-title {text-transform: uppercase;}#container-bookmakrs .bookmark-group-container h3.bookmark-group-title {color: var(--color-accent);margin-bottom: 10px;font-size: 16px;font-weight: 400;}#container-bookmakrs .bookmark-group-container {display: inline-block;width: 23.99%;}#container-bookmakrs .bookmark-group-container h3{white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}#container-bookmakrs .bookmark-group-container .bookmark-list {list-style: none;margin: 5px 0;}#container-bookmakrs .bookmark-group-container .bookmark-list a.bookmark {line-height: 2;transition: all 0.25s;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;display: block;}#container-bookmakrs .bookmark-group-container .bookmark-list a.bookmark:hover {text-decoration: underline;padding-left: 10px;}#container-bookmakrs .bookmark-group-container .bookmark-list a.bookmark img {display: inline-block;width: 20px;height: 20px;color: var(--color-primary);vertical-align: middle;}#container-bookmakrs .bookmark-group-container .bookmark svg,#container-bookmakrs .bookmark-group-container .bookmark span {display: inline;vertical-align: middle;}#container-bookmakrs .bookmark-group-container .bookmark span {margin-left: 4px;}#container-bookmakrs .bookmark-group-container .bookmark svg {width: 24px;}@media (max-width: 767px) {#container-bookmakrs .bookmark-group-container {width: 30%;float: none;}}#page-home #hero-container {position: relative;}#plugin-datetime {}#plugin-weather{position: absolute;right: 20px;top: 30px;}#page-home #hero-container p {font-weight: 300;text-transform: uppercase;height: 30px;}#hero-container .weather-value-container {justify-content: center;font-size: 16px;margin-left: 10px;font-weight: 500;}#hero-container .weather-value-container p:first-child {border-bottom: 1px solid var(--color-primary);padding-bottom: 5px;}#search-container #search {width: 100%;padding: 10px 0;color: var(--color-primary);font-size: 20px;background-color: transparent;border: none;border-bottom: 2px solid var(--color-accent);opacity: 0.5;transition: all 0.2s;outline: none;}#search-container #search-label {text-align: right;width: 100%;display: block;opacity: 0;}#search-container #search:active + label {opacity: 1;transition: all 0.2s;}#search-container #search:focus + label {opacity: 1;transition: all 0.2s;}.toolbar-container {width: 38px;position: fixed;bottom: var(--spacing-ui);left: var(--spacing-ui);display: block;-webkit-justify-content: center;justify-content: center;-webkit-align-items: center;align-items: center;visibility: visible;}#btn-open-settings {width: 100%;height: 100%;display: block;text-align: center;overflow: hidden;position: relative;}#btn-open-settings svg {color: var(--color-primary);width: 90%;margin-top: 2px;}#btn-open-settings span {position: absolute;left: 100%;opacity: 0;}#btn-open-help {width: 100%;height: 100%;display: block;text-align: center;overflow: hidden;position: relative;}#btn-open-help svg {color: var(--color-primary);width: 90%;margin-top: 2px;}#btn-open-help span {position: absolute;left: 100%;opacity: 0;}.toolbar-btn-bg{width: 35px;height: 35px;background-color: var(--color-accent);border-radius: 50%;display: block;-webkit-justify-content: center;justify-content: center;-webkit-align-items: center;align-items: center;transition: all 0.3s;visibility: visible;opacity: 0.25;}.toolbar-btn-bg:hover {opacity: 1;}.toolbar-btn-settings{margin: 0 0 10px 0;}}#page-settings .form-group label,#page-settings .form-group select,#page-settings .form-group input.btn-submit,#page-settings .form-group input.option-input {display: block;}#page-settings .form-group select {margin: 8px 0;width: 100%;border: none;border-radius: 4px;padding: 10px;background-color: var(--color-primary);color: var(--color-background);}#page-settings .form-group input.option-input {margin: 8px 0;width: 100%;border: none;border-radius: 4px;padding: 10px;background-color: var(--color-primary);color: var(--color-background);}#page-settings .form-group .help-text {font-size: 12px;color: var(--color-primary);}#page-settings .form-group input.btn-submit {padding: 8px 15px;border: 1px solid var(--color-accent);background-color: var(--color-background);color: var(--color-primary);border-radius: 4px;}#page-settings .container {padding: 25px 20px;width: 100%;margin: 0;}#page-settings h1 {font-size: 30px;}#page-settings .main-container {margin: 20px 0;position: relative;}#page-settings .main-container .setting-group-container {margin-left: 110px;}#page-settings.pageview .container {min-width: 375px;}@media (min-width: 1201px) {#page-settings.pageview .container {width: 90%;padding: 50px 250px;margin: 0 auto;}}@media (max-width: 1200px) {#page-settings.pageview .container {width: 90%;margin: 0 auto;}}@media (max-width: 768px) {#page-settings.pageview .container {width: 90%;padding: 20px;margin: 0 auto;}}@media (max-width: 375px) {#page-settings.pageview .container {width: 100%;padding: 20px;margin: 0 auto;}}.setting-group-container h2 {margin-bottom: 10px;}.setting-group-container .form-group .help-text {margin-bottom: 12px;text-decoration: underline;font-style: italic;opacity: 0.8;}.setting-group-container .form-group input.btn-submit {margin-top: 20px;margin-bottom: 20px;}#page-settings .form-group label {margin-top: 12px;margin-bottom: 6px;}#page-settings .form-group select,#page-settings .form-group input.option-input {margin-top: 6px;margin-bottom: 6px;}#page-settings .form-group textarea {resize: none;width: 100%;height: 80px;border: none;border-radius: 4px;padding: 10px;background-color: var(--color-primary);color: var(--color-background);margin-top: 6px;margin-bottom: 6px;}#page-settings .form-about p {line-height: 2;}#page-settings .form-about a {margin: 0 4px;border-bottom: 1px dotted #ccc;}#page-settings .form-about hr {margin: 10px 0;}#page-settings .sidebar {position: absolute;left: 0;width: 100px;list-style: none;}#page-settings .sidebar a {padding-left: 7px;border-left: 3px solid transparent;display: block;height: 40px;line-height: 40px;transition: all 0.3s;}#page-settings .sidebar a.active {border-left: 3px solid var(--color-primary);}#page-settings .sidebar:hover a.active {border-left: 3px solid transparent;}#page-settings .sidebar a:hover,#page-settings .sidebar a.active:hover {border-left: 3px solid var(--color-primary);}.theme-groups .theme {display: inline-block;width: 30%;}.theme-groups .theme .theme-container {width: 120px;border: 1px solid silver;position: relative;}.theme-groups .theme .theme-color {width: 33.33%;height: 40px;}.theme-groups .theme .theme-title {text-transform: capitalize;margin: 8px 0;color: var(--color-primary);cursor: default;}.theme-groups .theme .theme-choose {position: absolute;z-index: 1;left: 0;right: 0;top: 0;bottom: 0;width: 100%;cursor: pointer;opacity: 0;}.theme-blackboard .theme-color.color-background {background-color: rgb(26, 26, 26);}.theme-blackboard .theme-color.color-primary {background-color: rgb(255, 253, 234);}.theme-blackboard .theme-color.color-accent {background-color: rgb(92, 92, 92);}@media (max-width: 768px) {.theme-groups .theme {display: inline-block;width: 100px;margin-right: 1.5%;}.theme-groups .theme .theme-container {width: 92px;}.theme-groups .theme .theme-color {width: 30px;height: 30px;}}@media (max-width: 375px) {.theme-groups .theme {display: inline-block;width: 47%;margin-right: 1.5%;}.theme-groups .theme .theme-container {width: 92px;}.theme-groups .theme .theme-color {width: 30px;height: 30px;}}"
2 changes: 1 addition & 1 deletion config/state/theme.go → config/define/theme.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package FlareState
package FlareDefine

// TODO:挪到合适地方,拆分 assets

Expand Down
12 changes: 0 additions & 12 deletions config/state/cmd.go

This file was deleted.

30 changes: 15 additions & 15 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"

FlareState "github.com/soulteary/flare/config/state"
FlareDefine "github.com/soulteary/flare/config/define"
)

const (
Expand All @@ -23,27 +23,27 @@ func RequestHandle(router *gin.Engine) {
// TODO: 剥离逻辑
// TODO:替换密钥为用户相关数据
store := cookie.NewStore([]byte("secret"))
router.Use(sessions.Sessions("flare_"+strconv.Itoa(FlareState.AppFlags.Port), store))
router.Use(sessions.Sessions("flare_"+strconv.Itoa(FlareDefine.AppFlags.Port), store))

// 非离线模式注册路由
if !FlareState.AppFlags.DisableLoginMode {
router.POST(FlareState.MiscPages.Login.Path, login)
router.POST(FlareState.MiscPages.Logout.Path, logout)
if !FlareDefine.AppFlags.DisableLoginMode {
router.POST(FlareDefine.MiscPages.Login.Path, login)
router.POST(FlareDefine.MiscPages.Logout.Path, logout)
}
}

var commonText = `<a href="` + FlareState.SettingPages.Others.Path + `">返回重试</a></p><p>或前往 <a href="https://github.com/soulteary/docker-flare/issues/" target="_blank">https://github.com/soulteary/docker-flare/issues/</a> 反馈使用中的问题,谢谢!`
var commonText = `<a href="` + FlareDefine.SettingPages.Others.Path + `">返回重试</a></p><p>或前往 <a href="https://github.com/soulteary/docker-flare/issues/" target="_blank">https://github.com/soulteary/docker-flare/issues/</a> 反馈使用中的问题,谢谢!`
var internalErrorInput = []byte(`<html><p>请填写正确的用户名和密码 ` + commonText + `</html>`)
var internalErrorEmpty = []byte(`<html><p>用户名或密码不能为空 ` + commonText + `</html>`)
var internalErrorSave = []byte(`<html><p>程序内部错误,保存登陆状态失败 ` + commonText + `</html>`)

func AuthRequired(c *gin.Context) {

if !FlareState.AppFlags.DisableLoginMode {
if !FlareDefine.AppFlags.DisableLoginMode {
session := sessions.Default(c)
user := session.Get(SESSION_KEY_USER_NAME)
if user == nil {
c.Redirect(http.StatusFound, FlareState.SettingPages.Others.Path)
c.Redirect(http.StatusFound, FlareDefine.SettingPages.Others.Path)
c.Abort()
return
}
Expand All @@ -53,7 +53,7 @@ func AuthRequired(c *gin.Context) {
}

func CheckUserIsLogin(c *gin.Context) bool {
if !FlareState.AppFlags.DisableLoginMode {
if !FlareDefine.AppFlags.DisableLoginMode {
session := sessions.Default(c)
user := session.Get(SESSION_KEY_USER_NAME)
return user != nil
Expand All @@ -62,7 +62,7 @@ func CheckUserIsLogin(c *gin.Context) bool {
}

func GetUserName(c *gin.Context) interface{} {
if !FlareState.AppFlags.DisableLoginMode {
if !FlareDefine.AppFlags.DisableLoginMode {
session := sessions.Default(c)
data := session.Get(SESSION_KEY_USER_NAME)
return data
Expand All @@ -71,7 +71,7 @@ func GetUserName(c *gin.Context) interface{} {
}

func GetUserLoginDate(c *gin.Context) interface{} {
if !FlareState.AppFlags.DisableLoginMode {
if !FlareDefine.AppFlags.DisableLoginMode {
session := sessions.Default(c)
data := session.Get(SESSION_KEY_LOGIN_DATE)
return data
Expand All @@ -95,7 +95,7 @@ func login(c *gin.Context) {
return
}

if username != FlareState.AppFlags.User || password != FlareState.AppFlags.Pass {
if username != FlareDefine.AppFlags.User || password != FlareDefine.AppFlags.Pass {
c.Data(http.StatusBadRequest, _HTMLContentType, internalErrorInput)
c.Abort()
return
Expand All @@ -110,7 +110,7 @@ func login(c *gin.Context) {
return
}

c.Redirect(http.StatusFound, FlareState.SettingPages.Others.Path)
c.Redirect(http.StatusFound, FlareDefine.SettingPages.Others.Path)
c.Abort()
}

Expand All @@ -119,7 +119,7 @@ func logout(c *gin.Context) {
user := session.Get(SESSION_KEY_USER_NAME)
if user == nil {
// 直接跳转登陆页面
c.Redirect(http.StatusFound, FlareState.SettingPages.Others.Path)
c.Redirect(http.StatusFound, FlareDefine.SettingPages.Others.Path)
c.Abort()
return
}
Expand All @@ -131,6 +131,6 @@ func logout(c *gin.Context) {
c.Abort()
return
}
c.Redirect(http.StatusFound, FlareState.SettingPages.Others.Path)
c.Redirect(http.StatusFound, FlareDefine.SettingPages.Others.Path)
c.Abort()
}
8 changes: 4 additions & 4 deletions internal/misc/deprecated/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/gin-gonic/gin"

FlareState "github.com/soulteary/flare/config/state"
FlareDefine "github.com/soulteary/flare/config/define"
)

// TODO:样式优化
Expand All @@ -31,10 +31,10 @@ func makeLandingPage(originURL string, currentURL string, delay string) []byte {
func RegisterRouting(router *gin.Engine) {
const urlMDI = "/resources/mdi-cheat-sheets/"
router.GET(urlMDI, func(c *gin.Context) {
if FlareState.AppFlags.EnableDeprecatedNotice {
c.Data(http.StatusOK, "text/html; charset=utf-8", makeLandingPage(urlMDI, FlareState.RegularPages.Icons.Path, "5"))
if FlareDefine.AppFlags.EnableDeprecatedNotice {
c.Data(http.StatusOK, "text/html; charset=utf-8", makeLandingPage(urlMDI, FlareDefine.RegularPages.Icons.Path, "5"))
} else {
c.Redirect(http.StatusFound, FlareState.RegularPages.Icons.Path)
c.Redirect(http.StatusFound, FlareDefine.RegularPages.Icons.Path)
}
c.Abort()
})
Expand Down
Loading

0 comments on commit c17cb5c

Please sign in to comment.