This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
conf.go
98 lines (85 loc) · 1.87 KB
/
conf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"time"
)
const CONFIG_NAME = "settings.json"
type Config struct {
// sys config
Port int
Certificate string
Path string
SiteBase string
SiteURL string
// site config
SiteTitle string
SiteSubTitle string
AuthorName string
TimelineCount int
ThemeName string
}
var config *Config = nil
var sessionToken string
func init() {
config = new(Config)
// default config
config.Port = 8888
config.Certificate = SHA256Sum("42")
config.SiteBase = "localhost"
config.SiteURL = "http://localhost:8888"
config.SiteTitle = "TATTOO!"
config.Path = "/"
config.AuthorName = "root"
config.TimelineCount = 3
config.ThemeName = "sealscript"
sessionToken = GenerateSessionToken()
}
func GetSessionToken() string {
return sessionToken
}
func GenerateSessionToken() string {
sessionToken = SHA256Sum(strconv.Itoa(time.Now().Nanosecond()))
return sessionToken
}
func RevokeSessionTokon() {
sessionToken = GenerateSessionToken()
}
func GetConfig() *Config {
return config
}
func (config *Config) Load() error {
buff, err := ioutil.ReadFile(CONFIG_NAME)
if err != nil {
fmt.Println("Read file failed:", err)
config.Save()
}
buff, err = ioutil.ReadFile(CONFIG_NAME)
if err != nil {
fmt.Println("Read file failed:", err)
return err
}
if err := json.Unmarshal(buff, config); err != nil {
fmt.Println("Unmarshal json failed:", err)
return err
}
return nil
}
func (config *Config) Save() error {
jsobj, err := json.Marshal(config)
if err != nil {
fmt.Println("Marshal json failed:", err)
return err
}
ioutil.WriteFile(CONFIG_NAME, jsobj, 0644)
return nil
}
func (config *Config) Update(newcfg *Config) bool {
*config = *newcfg
return true
}
func (config *Config) String() string {
return fmt.Sprintf("{ Port: %v, Certificate: %v }", config.Port, config.Certificate)
}