123 lines
2.2 KiB
Go
Executable File
123 lines
2.2 KiB
Go
Executable File
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"local/router"
|
|
"local/todo-server/config"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"path/filepath"
|
|
)
|
|
|
|
func (s *Server) Routes() error {
|
|
routes := []struct {
|
|
path string
|
|
handler http.HandlerFunc
|
|
}{
|
|
{
|
|
path: "/",
|
|
handler: s.index,
|
|
},
|
|
{
|
|
path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard),
|
|
handler: s.phpProxy,
|
|
},
|
|
{
|
|
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 {
|
|
if err := s.Add(route.path, route.handler); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) index(w http.ResponseWriter, r *http.Request) {
|
|
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)
|
|
} else {
|
|
proxy := httputil.NewSingleHostReverseProxy(url)
|
|
proxy.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
func (s *Server) static(w http.ResponseWriter, r *http.Request) {
|
|
s.fileServer.ServeHTTP(w, r)
|
|
}
|