mirror of
https://github.com/Kakune55/Pixel.git
synced 2025-05-06 18:29:25 +08:00
添加配置文件功能
This commit is contained in:
parent
4dde2ba1ab
commit
d701f59a4e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/data
|
/data
|
||||||
|
user.json
|
||||||
|
44
config/load.go
Normal file
44
config/load.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
type User struct {
|
||||||
|
Listen string `json:"listen"`
|
||||||
|
}
|
||||||
|
func ReadConfig(filePath string) (User, error) {
|
||||||
|
// 检查文件是否存在
|
||||||
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||||
|
// 文件不存在,创建一个新的JSON文件
|
||||||
|
user := User{Listen: ":9090"}
|
||||||
|
data, err := json.Marshal(user)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
err = os.WriteFile(filePath, data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 读取文件内容
|
||||||
|
data, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
// 解析JSON数据
|
||||||
|
var user User
|
||||||
|
err = json.Unmarshal(data, &user)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
//如何调用
|
||||||
|
//---------------------------------------
|
||||||
|
// filePath := "user.json"
|
||||||
|
// // 读取配置文件
|
||||||
|
// user, err := ReadConfig(filePath)
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println("读取配置文件失败:", err)
|
||||||
|
// }
|
44
main.go
44
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"Pixel/config"
|
||||||
"Pixel/database"
|
"Pixel/database"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -20,7 +21,6 @@ import (
|
|||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
const appinfo string = `
|
const appinfo string = `
|
||||||
|
|
||||||
@ -61,18 +61,24 @@ func init() {
|
|||||||
fmt.Println("数据库初始化完成")
|
fmt.Println("数据库初始化完成")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
filePath := "user.json"
|
||||||
|
// 读取配置文件
|
||||||
|
config, err := config.ReadConfig(filePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("读取配置文件失败:", err)
|
||||||
|
}
|
||||||
|
|
||||||
http.HandleFunc("/info", showimg)
|
http.HandleFunc("/info", showimg)
|
||||||
http.HandleFunc("/info/list", showlist)//
|
http.HandleFunc("/info/list", showlist) //
|
||||||
http.HandleFunc("/upload", upload)//上传图片
|
http.HandleFunc("/upload", upload) //上传图片
|
||||||
http.HandleFunc("/img/",downloadHandler)//图片接口
|
http.HandleFunc("/img/", downloadHandler) //图片接口
|
||||||
http.HandleFunc("/img/mini",displayThumbnailHandler)//缩略图接口
|
http.HandleFunc("/img/mini", displayThumbnailHandler) //缩略图接口
|
||||||
http.HandleFunc("/idlist",arrayHandler)//获取现有图片id
|
http.HandleFunc("/idlist", arrayHandler) //获取现有图片id
|
||||||
http.HandleFunc("/img/del",deleteImagesHandler)//删除相应图片
|
http.HandleFunc("/img/del", deleteImagesHandler) //删除相应图片
|
||||||
http.HandleFunc("/login",login)//登录页
|
http.HandleFunc("/login", login) //登录页
|
||||||
fmt.Println("Web服务器已启动")
|
fmt.Println("Web服务器已启动")
|
||||||
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
err = http.ListenAndServe(config.Listen, nil) //设置监听的端口
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("ListenAndServe: ", err)
|
log.Fatal("ListenAndServe: ", err)
|
||||||
}
|
}
|
||||||
@ -85,7 +91,7 @@ func showimg(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func showlist(w http.ResponseWriter, r *http.Request) {
|
func showlist(w http.ResponseWriter, r *http.Request) {
|
||||||
cookie, _ := r.Cookie("login")
|
cookie, _ := r.Cookie("login")
|
||||||
if cookie == nil{ //未授权禁止访问
|
if cookie == nil { //未授权禁止访问
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
w.Write([]byte(`<html><a href="/login">验证失败 点此登录</a><html>`))
|
w.Write([]byte(`<html><a href="/login">验证失败 点此登录</a><html>`))
|
||||||
return
|
return
|
||||||
@ -95,7 +101,6 @@ func showlist(w http.ResponseWriter, r *http.Request) {
|
|||||||
t.Execute(w, "Hello")
|
t.Execute(w, "Hello")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 处理/upload 逻辑
|
// 处理/upload 逻辑
|
||||||
func upload(w http.ResponseWriter, r *http.Request) {
|
func upload(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("method:", r.Method) // 获取请求的方法
|
fmt.Println("method:", r.Method) // 获取请求的方法
|
||||||
@ -148,12 +153,11 @@ func upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// 存入数据库
|
// 存入数据库
|
||||||
var linkid = RandomString(10)
|
var linkid = RandomString(10)
|
||||||
database.NewFile(linkid,md5sum,ext)
|
database.NewFile(linkid, md5sum, ext)
|
||||||
w.Write([]byte(linkid))
|
w.Write([]byte(linkid))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func RandomString(n int) string {
|
func RandomString(n int) string {
|
||||||
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
bytes := make([]byte, n)
|
bytes := make([]byte, n)
|
||||||
@ -250,7 +254,7 @@ func displayThumbnailHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func arrayHandler(w http.ResponseWriter, r *http.Request) { //获取全部图片ID
|
func arrayHandler(w http.ResponseWriter, r *http.Request) { //获取全部图片ID
|
||||||
cookie, _ := r.Cookie("login")
|
cookie, _ := r.Cookie("login")
|
||||||
if cookie == nil{ //未授权禁止访问
|
if cookie == nil { //未授权禁止访问
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
w.Write([]byte(`<html><a href="/login">验证失败 点此登录</a><html>`))
|
w.Write([]byte(`<html><a href="/login">验证失败 点此登录</a><html>`))
|
||||||
return
|
return
|
||||||
@ -277,7 +281,7 @@ func arrayHandler(w http.ResponseWriter, r *http.Request) { //获取全部图
|
|||||||
|
|
||||||
func deleteImagesHandler(w http.ResponseWriter, r *http.Request) {
|
func deleteImagesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
cookie, _ := r.Cookie("login")
|
cookie, _ := r.Cookie("login")
|
||||||
if cookie == nil{ //未授权禁止访问
|
if cookie == nil { //未授权禁止访问
|
||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
w.Write([]byte(`<html><a href="/login">验证失败 点此登录</a><html>`))
|
w.Write([]byte(`<html><a href="/login">验证失败 点此登录</a><html>`))
|
||||||
return
|
return
|
||||||
@ -311,15 +315,15 @@ func login(w http.ResponseWriter, r *http.Request) {
|
|||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
t, _ := template.ParseFiles("Web/login.html")
|
t, _ := template.ParseFiles("Web/login.html")
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
t.Execute(w,"")
|
t.Execute(w, "")
|
||||||
} else {
|
} else {
|
||||||
userlist,_:= database.QueryUser()
|
userlist, _ := database.QueryUser()
|
||||||
fmt.Println(userlist)
|
fmt.Println(userlist)
|
||||||
if len(userlist) == 0 {
|
if len(userlist) == 0 {
|
||||||
database.NewUser("admin", r.FormValue("passwd"))
|
database.NewUser("admin", r.FormValue("passwd"))
|
||||||
} else {
|
} else {
|
||||||
if !database.CheckUserPasswd("admin", r.FormValue("passwd")) {
|
if !database.CheckUserPasswd("admin", r.FormValue("passwd")) {
|
||||||
http.Redirect(w, r, "/login",http.StatusFound)
|
http.Redirect(w, r, "/login", http.StatusFound)
|
||||||
fmt.Println("密码错误")
|
fmt.Println("密码错误")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -327,6 +331,6 @@ func login(w http.ResponseWriter, r *http.Request) {
|
|||||||
cookie := http.Cookie{Name: "login", Value: "yes"}
|
cookie := http.Cookie{Name: "login", Value: "yes"}
|
||||||
http.SetCookie(w, &cookie)
|
http.SetCookie(w, &cookie)
|
||||||
fmt.Println("密码正确")
|
fmt.Println("密码正确")
|
||||||
http.Redirect(w, r, "/info/list",http.StatusFound)
|
http.Redirect(w, r, "/info/list", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user