Serve files for static

master
bel 2020-01-20 16:06:21 -07:00
parent 2bb3bee83c
commit 097ca9b8c0
5 changed files with 85 additions and 12 deletions

View File

@ -13,6 +13,7 @@ var (
StoreAddr string StoreAddr string
StoreUser string StoreUser string
StorePass string StorePass string
Root string
MyTinyTodo string MyTinyTodo string
) )
@ -32,6 +33,7 @@ func Refresh() {
as.Append(args.STRING, "storeuser", "user of store", "") as.Append(args.STRING, "storeuser", "user of store", "")
as.Append(args.STRING, "storepass", "pass 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, "mtt", "url of php server", "http://localhost:38808")
as.Append(args.STRING, "root", "root of static files", "./public")
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
} }
@ -41,5 +43,6 @@ func Refresh() {
StoreAddr = as.Get("storeaddr").GetString() StoreAddr = as.Get("storeaddr").GetString()
StoreUser = as.Get("storeuser").GetString() StoreUser = as.Get("storeuser").GetString()
StorePass = as.Get("storepass").GetString() StorePass = as.Get("storepass").GetString()
Root = as.Get("root").GetString()
MyTinyTodo = as.Get("mtt").GetString() MyTinyTodo = as.Get("mtt").GetString()
} }

View File

@ -2,15 +2,13 @@ package server
import ( import (
"fmt" "fmt"
"io"
"local/router" "local/router"
"local/todo-server/config" "local/todo-server/config"
"log" "log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"os" "path/filepath"
"path"
) )
func (s *Server) Routes() error { func (s *Server) Routes() error {
@ -18,6 +16,10 @@ func (s *Server) Routes() error {
path string path string
handler http.HandlerFunc handler http.HandlerFunc
}{ }{
{
path: "/",
handler: s.index,
},
{ {
path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard), path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard),
handler: s.phpProxy, handler: s.phpProxy,
@ -26,6 +28,64 @@ func (s *Server) Routes() error {
path: fmt.Sprintf("ajax.php"), path: fmt.Sprintf("ajax.php"),
handler: s.HandleAjax, 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 { for _, route := range routes {
@ -37,16 +97,17 @@ func (s *Server) Routes() error {
} }
func (s *Server) index(w http.ResponseWriter, r *http.Request) { func (s *Server) index(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(path.Join(config.MyTinyTodo, "index.php")) r.URL.Path = "/index.php"
if err != nil { http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
defer f.Close()
io.Copy(w, f)
} }
func (s *Server) phpProxy(w http.ResponseWriter, r *http.Request) { 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) url, err := url.Parse(config.MyTinyTodo)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -55,3 +116,7 @@ func (s *Server) phpProxy(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r) proxy.ServeHTTP(w, r)
} }
} }
func (s *Server) static(w http.ResponseWriter, r *http.Request) {
s.fileServer.ServeHTTP(w, r)
}

View File

@ -2,12 +2,15 @@ package server
import ( import (
"local/router" "local/router"
"local/todo-server/config"
"local/todo-server/server/ajax" "local/todo-server/server/ajax"
"net/http"
) )
type Server struct { type Server struct {
*ajax.Ajax *ajax.Ajax
*router.Router *router.Router
fileServer http.Handler
} }
func New() *Server { func New() *Server {
@ -15,8 +18,10 @@ func New() *Server {
if err != nil { if err != nil {
panic(err) panic(err)
} }
fileServer := http.FileServer(http.Dir(config.Root))
return &Server{ return &Server{
Ajax: ajax, Ajax: ajax,
Router: router.New(), Router: router.New(),
fileServer: fileServer,
} }
} }

0
source_to_run_loaded.sh Normal file → Executable file
View File

0
testdata/migrate.sh vendored Normal file → Executable file
View File