Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ymh0000123 authored Jul 13, 2023
1 parent fa8eef7 commit 5830579
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var (
mutex sync.Mutex
)

const configFilePath = "config.yaml"

func main() {
// 设置日志文件
logFile, err := os.OpenFile("app.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
Expand All @@ -54,6 +56,15 @@ func main() {
log.SetOutput(logFile)
log.SetFlags(log.Ldate | log.Ltime)

// 检查配置文件是否存在,如果不存在则创建并写入默认配置
if !fileExists(configFilePath) {
err := createDefaultConfig()
if err != nil {
log.Fatal("无法创建默认配置文件:", err)
}
log.Println("已创建默认配置文件")
}

// 读取配置文件
configData, err := ioutil.ReadFile("config.yaml")
if err != nil {
Expand Down Expand Up @@ -99,7 +110,7 @@ func main() {
<button type="submit" class="btn btn-primary">上传</button>
</form>
<br>
<a href="/list" class="btn btn-secondary">文件列表</a><a href="/admin" class="btn btn-primary">管理员界面</a>
<a href="/list" class="btn btn-secondary">文件列表</a> <a href="/admin" class="btn btn-primary">管理员界面</a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.2/js/bootstrap.bundle.min.js"></script>
</body>
Expand Down Expand Up @@ -427,3 +438,28 @@ func deleteFile(name string) error {

return nil
}

func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}

func createDefaultConfig() error {
defaultConfig := Config{
Port: 8080,
Username: "admin",
Password: "123456",
}

configData, err := yaml.Marshal(&defaultConfig)
if err != nil {
return err
}

err = ioutil.WriteFile(configFilePath, configData, 0644)
if err != nil {
return err
}

return nil
}

0 comments on commit 5830579

Please sign in to comment.