dynamic data directory

This commit is contained in:
ari melody 2024-11-10 00:37:01 +00:00
parent d0b392f6a0
commit 5284b8a7cc
Signed by: ari
GPG key ID: CF99829C92678188
3 changed files with 24 additions and 2 deletions

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"arimelody-web/global"
"bufio" "bufio"
"encoding/base64" "encoding/base64"
"errors" "errors"
@ -15,6 +16,7 @@ func HandleImageUpload(data *string, directory string, filename string) (string,
header := split[0] header := split[0]
imageData, err := base64.StdEncoding.DecodeString(split[1]) imageData, err := base64.StdEncoding.DecodeString(split[1])
ext, _ := strings.CutPrefix(header, "data:image/") ext, _ := strings.CutPrefix(header, "data:image/")
directory = filepath.Join(global.DATA_DIR, directory)
switch ext { switch ext {
case "png": case "png":

View file

@ -3,6 +3,7 @@ package global
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -34,7 +35,7 @@ var Args = func() map[string]string {
return args return args
}() }()
var HTTP_DOMAIN = func() string { var HTTP_DOMAIN = func() string {
domain := os.Getenv("ARIMELODY_HTTP_DOMAIN") domain := os.Getenv("ARIMELODY_HTTP_DOMAIN")
if domain == "" { if domain == "" {
return "https://arimelody.me" return "https://arimelody.me"
@ -42,4 +43,23 @@ var HTTP_DOMAIN = func() string {
return domain return domain
}() }()
var DATA_DIR = func() string {
dir, err := filepath.Abs(os.Getenv("ARIMELODY_DATA_DIR"))
if err != nil {
fmt.Printf("FATAL: Failed to get working directory: %s\n", err.Error())
os.Exit(1)
}
if dir != "" {
os.MkdirAll(dir, os.ModePerm)
} else {
var err error
dir, err = os.Getwd()
if err != nil {
fmt.Printf("FATAL: Failed to get working directory: %s\n", err.Error())
os.Exit(1)
}
}
return dir
}()
var DB *sqlx.DB var DB *sqlx.DB

View file

@ -72,7 +72,7 @@ func createServeMux() *http.ServeMux {
mux.Handle("/admin/", http.StripPrefix("/admin", admin.Handler())) mux.Handle("/admin/", http.StripPrefix("/admin", admin.Handler()))
mux.Handle("/api/", http.StripPrefix("/api", api.Handler())) mux.Handle("/api/", http.StripPrefix("/api", api.Handler()))
mux.Handle("/music/", http.StripPrefix("/music", view.MusicHandler())) mux.Handle("/music/", http.StripPrefix("/music", view.MusicHandler()))
mux.Handle("/uploads/", http.StripPrefix("/uploads", staticHandler("uploads"))) mux.Handle("/uploads/", http.StripPrefix("/uploads", staticHandler(filepath.Join(global.DATA_DIR, "uploads"))))
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" || r.URL.Path == "/index.html" { if r.URL.Path == "/" || r.URL.Path == "/index.html" {
err := templates.Pages["index"].Execute(w, nil) err := templates.Pages["index"].Execute(w, nil)