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
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()
}

View File

@ -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)
}

View File

@ -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,
}
}

0
source_to_run_loaded.sh Normal file → Executable file
View File

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