From 097ca9b8c00e12398587b5d689b334f2b187a941 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 20 Jan 2020 16:06:21 -0700 Subject: [PATCH] Serve files for static --- config/config.go | 3 ++ server/routes.go | 85 ++++++++++++++++++++++++++++++++++++----- server/server.go | 9 ++++- source_to_run_loaded.sh | 0 testdata/migrate.sh | 0 5 files changed, 85 insertions(+), 12 deletions(-) mode change 100644 => 100755 source_to_run_loaded.sh mode change 100644 => 100755 testdata/migrate.sh diff --git a/config/config.go b/config/config.go index c690ce5..292cc39 100755 --- a/config/config.go +++ b/config/config.go @@ -13,6 +13,7 @@ var ( StoreAddr string StoreUser string StorePass string + Root string MyTinyTodo string ) @@ -32,6 +33,7 @@ func Refresh() { as.Append(args.STRING, "storeuser", "user of store", "") as.Append(args.STRING, "storepass", "pass of store", "") as.Append(args.STRING, "mtt", "url of php server", "http://localhost:38808") + as.Append(args.STRING, "root", "root of static files", "./public") if err := as.Parse(); err != nil { panic(err) } @@ -41,5 +43,6 @@ func Refresh() { StoreAddr = as.Get("storeaddr").GetString() StoreUser = as.Get("storeuser").GetString() StorePass = as.Get("storepass").GetString() + Root = as.Get("root").GetString() MyTinyTodo = as.Get("mtt").GetString() } diff --git a/server/routes.go b/server/routes.go index f04741c..be791fd 100755 --- a/server/routes.go +++ b/server/routes.go @@ -2,15 +2,13 @@ package server import ( "fmt" - "io" "local/router" "local/todo-server/config" "log" "net/http" "net/http/httputil" "net/url" - "os" - "path" + "path/filepath" ) func (s *Server) Routes() error { @@ -18,6 +16,10 @@ func (s *Server) Routes() error { path string handler http.HandlerFunc }{ + { + path: "/", + handler: s.index, + }, { path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard), handler: s.phpProxy, @@ -26,6 +28,64 @@ func (s *Server) Routes() error { path: fmt.Sprintf("ajax.php"), handler: s.HandleAjax, }, + /* + { + path: "db/index.html", + handler: s.static, + }, + { + path: "jquery/index.html", + handler: s.static, + }, + { + path: "lang/index.html", + handler: s.static, + }, + { + path: "themes/index.html", + handler: s.static, + }, + { + path: "tmp/index.html", + handler: s.static, + }, + { + path: "jquery/jquery-1.4.4.min.js", + handler: s.static, + }, + { + path: "jquery/jquery-ui-1.8.7.custom.min.js", + handler: s.static, + }, + { + path: "jquery/jquery.autocomplete-1.1.js", + handler: s.static, + }, + { + path: "mytinytodo.js", + handler: s.static, + }, + { + path: "mytinytodo_ajax_storage.js", + handler: s.static, + }, + { + path: "testdata/mytinytodo2/themes/default/pda.css", + handler: s.static, + }, + { + path: "testdata/mytinytodo2/themes/default/print.css", + handler: s.static, + }, + { + path: "testdata/mytinytodo2/themes/default/style.css", + handler: s.static, + }, + { + path: "testdata/mytinytodo2/themes/default/style_rtl.css", + handler: s.static, + }, + */ } for _, route := range routes { @@ -37,16 +97,17 @@ func (s *Server) Routes() error { } func (s *Server) index(w http.ResponseWriter, r *http.Request) { - f, err := os.Open(path.Join(config.MyTinyTodo, "index.php")) - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - defer f.Close() - io.Copy(w, f) + r.URL.Path = "/index.php" + http.Redirect(w, r, r.URL.String(), http.StatusSeeOther) } func (s *Server) phpProxy(w http.ResponseWriter, r *http.Request) { + switch filepath.Ext(r.URL.Path) { + case ".php": + default: + s.static(w, r) + return + } url, err := url.Parse(config.MyTinyTodo) if err != nil { log.Println(err) @@ -55,3 +116,7 @@ func (s *Server) phpProxy(w http.ResponseWriter, r *http.Request) { proxy.ServeHTTP(w, r) } } + +func (s *Server) static(w http.ResponseWriter, r *http.Request) { + s.fileServer.ServeHTTP(w, r) +} diff --git a/server/server.go b/server/server.go index fd2aac4..9f35c88 100755 --- a/server/server.go +++ b/server/server.go @@ -2,12 +2,15 @@ package server import ( "local/router" + "local/todo-server/config" "local/todo-server/server/ajax" + "net/http" ) type Server struct { *ajax.Ajax *router.Router + fileServer http.Handler } func New() *Server { @@ -15,8 +18,10 @@ func New() *Server { if err != nil { panic(err) } + fileServer := http.FileServer(http.Dir(config.Root)) return &Server{ - Ajax: ajax, - Router: router.New(), + Ajax: ajax, + Router: router.New(), + fileServer: fileServer, } } diff --git a/source_to_run_loaded.sh b/source_to_run_loaded.sh old mode 100644 new mode 100755 diff --git a/testdata/migrate.sh b/testdata/migrate.sh old mode 100644 new mode 100755