Implemented configuration file functionality
For site name, port number and data directory
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
SiteName string `json:"site_name"`
|
||||||
|
Port string `json:"port"`
|
||||||
|
DataDir string `json:"data_dir"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(path string) Config {
|
||||||
|
configData := Config{
|
||||||
|
SiteName: "Unnamed Download Listing",
|
||||||
|
Port: "8080",
|
||||||
|
DataDir: "data",
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("No config file found, using defaults. Consider setting up a Site Name at the very least!")
|
||||||
|
return configData
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Overwrite the default configuration with data from 'file'
|
||||||
|
err = json.NewDecoder(file).Decode(&configData)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Config file exists but has errors:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return configData
|
||||||
|
}
|
||||||
@@ -6,13 +6,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"192.168.1.180/odonax/filehost-athome/config"
|
||||||
"192.168.1.180/odonax/filehost-athome/database"
|
"192.168.1.180/odonax/filehost-athome/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
os.MkdirAll("data/files", 0755)
|
cfg := config.Load("config.json")
|
||||||
|
|
||||||
db := database.Open("data/hub.db")
|
os.MkdirAll(cfg.DataDir+"/files", 0755)
|
||||||
|
|
||||||
|
db := database.Open(cfg.DataDir+"/hub.db")
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("templates/home.html")
|
tmpl, err := template.ParseFiles("templates/home.html")
|
||||||
@@ -22,7 +25,7 @@ func main() {
|
|||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
"Name": "Filehost at home",
|
"Name": cfg.SiteName,
|
||||||
"Description": "This is where you'll own your files and share them freely.",
|
"Description": "This is where you'll own your files and share them freely.",
|
||||||
}
|
}
|
||||||
tmpl.Execute(w, data)
|
tmpl.Execute(w, data)
|
||||||
|
|||||||
Reference in New Issue
Block a user