Explorar el Código

Merge pull request #45 from tehmaze-labs/master

make paths configurable; make the Go server work
Andre d hace 8 años
padre
commit
6398eab0c3
Se han modificado 3 ficheros con 39 adiciones y 17 borrados
  1. 5 0
      server/server.conf.example
  2. 27 14
      server/server.go
  3. 7 3
      server/server.js

+ 5 - 0
server/server.conf.example

@@ -3,6 +3,11 @@
   "delete_key": "",
   "maximum_file_size": 50000000,
 
+  "path": {
+    "i": "../i",
+    "client": "../client"
+  },
+
   "http": {
     "enabled": true,
     "listen": ":80"

+ 27 - 14
server/server.go

@@ -5,6 +5,7 @@ import (
 	"crypto/sha256"
 	"encoding/hex"
 	"encoding/json"
+	"flag"
 	"fmt"
 	"io"
 	"log"
@@ -12,6 +13,7 @@ import (
 	"net/url"
 	"os"
 	"path"
+	"path/filepath"
 	"sync"
 )
 
@@ -21,6 +23,11 @@ type Config struct {
 	DeleteKey   string `json:"delete_key"`
 	MaxFileSize int64  `json:"maximum_file_size"`
 
+	Path struct {
+		I      string `json:"i"`
+		Client string `json:"client"`
+	} `json:"path"`
+
 	Http struct {
 		Enabled bool   `json:"enabled"`
 		Listen  string `json:"listen"`
@@ -53,8 +60,8 @@ type SuccessMessage struct {
 	Delkey string `json:"delkey"`
 }
 
-func readConfig() Config {
-	file, _ := os.Open("server.conf")
+func readConfig(name string) Config {
+	file, _ := os.Open(name)
 	decoder := json.NewDecoder(file)
 	config := Config{}
 	err := decoder.Decode(&config)
@@ -74,6 +81,12 @@ func validateConfig(config Config) {
 	if len(config.DeleteKey) == 0 {
 		log.Fatal("A static delete key must be defined in the configuration!")
 	}
+	if len(config.Path.I) == 0 {
+		config.Path.I = "../i"
+	}
+	if len(config.Path.Client) == 0 {
+		config.Path.Client = "../client"
+	}
 }
 
 func makeDelkey(ident string) string {
@@ -85,11 +98,9 @@ func makeDelkey(ident string) string {
 
 func index(w http.ResponseWriter, r *http.Request) {
 	if r.URL.Path == "/" {
-		http.ServeFile(w, r, "index.html")
-	} else if r.URL.Path == "/config.js" {
-		http.ServeFile(w, r, "config.js")
+		http.ServeFile(w, r, filepath.Join(config.Path.Client, "index.html"))
 	} else {
-		http.NotFound(w, r)
+		http.ServeFile(w, r, filepath.Join(config.Path.Client, r.URL.Path[1:]))
 	}
 }
 
@@ -125,7 +136,7 @@ func upload(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	identPath := path.Join("i", path.Base(ident))
+	identPath := path.Join(config.Path.I, path.Base(ident))
 	if _, err := os.Stat(identPath); err == nil {
 		msg, _ := json.Marshal(&ErrorMessage{Error: "Ident is already taken.", Code: 4})
 		w.Write(msg)
@@ -169,7 +180,7 @@ func delfile(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	identPath := path.Join("i", ident)
+	identPath := path.Join(config.Path.I, ident)
 	if _, err := os.Stat(identPath); os.IsNotExist(err) {
 		msg, _ := json.Marshal(&ErrorMessage{Error: "Ident does not exist.", Code: 9})
 		w.Write(msg)
@@ -216,15 +227,17 @@ func cfInvalidate(ident string, https bool) {
 }
 
 func main() {
-	http.HandleFunc("/", index)
-	http.HandleFunc("/up", upload)
-	http.HandleFunc("/del", delfile)
-	http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
-	http.Handle("/i/", http.StripPrefix("/i", http.FileServer(http.Dir("i"))))
+	configName := flag.String("config", "server.conf", "Configuration file")
+	flag.Parse()
 
-	config = readConfig()
+	config = readConfig(*configName)
 	validateConfig(config)
 
+	http.Handle("/i/", http.StripPrefix("/i", http.FileServer(http.Dir(config.Path.I))))
+	http.HandleFunc("/up", upload)
+	http.HandleFunc("/del", delfile)
+	http.HandleFunc("/", index)
+
 	var wg sync.WaitGroup
 	wg.Add(2)
 

+ 7 - 3
server/server.js

@@ -36,7 +36,7 @@ function handle_upload(req, res) {
 
     busboy.on('file', function(fieldname, file, filename) {
         try {
-            var ftmp = tmp.fileSync({ postfix: '.tmp', dir: '../i/', keep: true });
+            var ftmp = tmp.fileSync({ postfix: '.tmp', dir: req.app.locals.config.path.i, keep: true });
             tmpfname = ftmp.name;
 
             var fstream = fs.createWriteStream('', {fd: ftmp.fd, defaultEncoding: 'binary'});
@@ -157,13 +157,17 @@ function cf_invalidate(ident, config) {
         cf_do_invalidate(ident, 'http', cfconfig);
     if (config.https.enabled)
         cf_do_invalidate(ident, 'https', cfconfig);
+
+    config.path |= {};
+    config.path.i |= "../i";
+    config.path.client |= "../client";
 }
 
 function create_app(config) {
   var app = express();
   app.locals.config = config
-  app.use('', express.static('../client'));
-  app.use('/i', express.static('../i'));
+  app.use('', express.static(config.path.client));
+  app.use('/i', express.static(config.path.i));
   app.post('/up', handle_upload);
   app.get('/del', handle_delete);
   return app