Serve files for static
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user