Finish refactoring
ci/woodpecker/push/test_golang Pipeline was successful
Details
ci/woodpecker/push/test_golang Pipeline was successful
Details
parent
04cde598a0
commit
2d2a1b1486
@ -1,16 +1,21 @@
|
||||
git.lcomrade.su/root/lineend v1.0.0 h1:qcxrR4DS18Erx+pG/EspoDhEDai+mjgSYefwSK2dq5g=
|
||||
git.lcomrade.su/root/lineend v1.0.0/go.mod h1:D0q3jMx0I1PFZjAFwkmUNW8D5tIw8rZJoeUAxhYD7Ec=
|
||||
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
|
||||
github.com/alecthomas/assert/v2 v2.2.1/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
||||
github.com/alecthomas/chroma/v2 v2.7.0 h1:hm1rY6c/Ob4eGclpQ7X/A3yhqBOZNUTk9q+yhyLIViI=
|
||||
github.com/alecthomas/chroma/v2 v2.7.0/go.mod h1:yrkMI9807G1ROx13fhe1v6PN2DDeaR73L3d+1nmYQtw=
|
||||
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
|
||||
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
|
||||
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/urfave/cli/v2 v2.25.1 h1:zw8dSP7ghX0Gmm8vugrs6q9Ku0wzweqPyshy+syu9Gw=
|
||||
github.com/urfave/cli/v2 v2.25.1/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
|
@ -0,0 +1,168 @@
|
||||
// Copyright (C) 2021-2023 Leonid Maslakov.
|
||||
|
||||
// This file is part of Lenpaste.
|
||||
|
||||
// Lenpaste is free software: you can redistribute it
|
||||
// and/or modify it under the terms of the
|
||||
// GNU Affero Public License as published by the
|
||||
// Free Software Foundation, either version 3 of the License,
|
||||
// or (at your option) any later version.
|
||||
|
||||
// Lenpaste is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
// or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
// See the GNU Affero Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU Affero Public License along with Lenpaste.
|
||||
// If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.lcomrade.su/root/lenpaste/internal/model"
|
||||
)
|
||||
|
||||
func Load(cfgDir string) (*Config, error) {
|
||||
// Default configuration
|
||||
cfg := Config{
|
||||
HTTP: ConfigHTTP{
|
||||
Address: ":80",
|
||||
},
|
||||
|
||||
DB: ConfigDB{
|
||||
Driver: "",
|
||||
Source: "",
|
||||
MaxOpenConns: 25,
|
||||
MaxIdleConns: 5,
|
||||
ConnMaxLifetime: 5 * 60,
|
||||
ConnMaxLifetimeStr: "5m",
|
||||
},
|
||||
|
||||
Public: ConfigPublic{
|
||||
AdminName: "",
|
||||
AdminMail: "",
|
||||
|
||||
RobotsDisallow: false,
|
||||
},
|
||||
|
||||
UI: ConfigUI{
|
||||
DefaultTheme: "dark",
|
||||
},
|
||||
|
||||
Paste: ConfigPaste{
|
||||
TitleMaxLen: 100,
|
||||
BodyMaxLen: 20000,
|
||||
MaxLifetime: 0,
|
||||
MaxLifetimeStr: "unlimited",
|
||||
|
||||
UiDefaultLifetime: 0,
|
||||
UiDefaultLifetimeStr: "",
|
||||
},
|
||||
|
||||
About: nil,
|
||||
Rules: nil,
|
||||
TermsOfUse: nil,
|
||||
|
||||
Paths: ConfigPaths{
|
||||
MainCfg: filepath.Join(cfgDir, model.SmallName+".json"),
|
||||
|
||||
AboutDir: filepath.Join(cfgDir, "about"),
|
||||
RulesDir: filepath.Join(cfgDir, "rules"),
|
||||
TermsDir: filepath.Join(cfgDir, "terms"),
|
||||
|
||||
ThemesDir: filepath.Join(cfgDir, "themes"),
|
||||
|
||||
LenPasswdFile: filepath.Join(cfgDir, "lenpasswd"),
|
||||
},
|
||||
}
|
||||
|
||||
// Read main configuration file
|
||||
cfgFile, err := os.Open(cfg.Paths.MainCfg)
|
||||
if err != nil {
|
||||
return nil, errors.New("config: " + err.Error())
|
||||
}
|
||||
defer cfgFile.Close()
|
||||
|
||||
err = json.NewDecoder(cfgFile).Decode(&cfg)
|
||||
if err != nil {
|
||||
return nil, errors.New("config: " + err.Error())
|
||||
}
|
||||
|
||||
// Convert strings duration to time
|
||||
cfg.DB.ConnMaxLifetime, err = parseDuration(cfg.DB.ConnMaxLifetimeStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg.Paste.MaxLifetime, err = parseDuration(cfg.Paste.MaxLifetimeStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg.Paste.UiDefaultLifetime, err = parseDuration(cfg.Paste.UiDefaultLifetimeStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read about, rules and terms of use
|
||||
cfg.About, err = loadL10nFiles(cfg.Paths.AboutDir, ".txt")
|
||||
if err != nil {
|
||||
return nil, errors.New("config: " + err.Error())
|
||||
}
|
||||
|
||||
cfg.Rules, err = loadL10nFiles(cfg.Paths.RulesDir, ".txt")
|
||||
if err != nil {
|
||||
return nil, errors.New("config: " + err.Error())
|
||||
}
|
||||
|
||||
cfg.TermsOfUse, err = loadL10nFiles(cfg.Paths.TermsDir, ".txt")
|
||||
if err != nil {
|
||||
return nil, errors.New("config: " + err.Error())
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func loadL10nFiles(dir, ext string) (map[string]string, error) {
|
||||
// Get list files in directory
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read files
|
||||
out := make(map[string]string)
|
||||
|
||||
for _, part := range files {
|
||||
if part.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
fileName := part.Name()
|
||||
if !strings.HasSuffix(fileName, ext) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Read file and add it to map
|
||||
fileByte, err := os.ReadFile(filepath.Join(dir, fileName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out[strings.TrimSuffix(fileName, ext)] = bytes.NewBuffer(fileByte).String()
|
||||
}
|
||||
|
||||
// If map is empty return nil
|
||||
if len(out) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
Loading…
Reference in New Issue