58 lines
1.0 KiB
Go
Executable File
58 lines
1.0 KiB
Go
Executable File
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"local/router"
|
|
"local/todo-server/config"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
func (s *Server) Routes() error {
|
|
routes := []struct {
|
|
path string
|
|
handler http.HandlerFunc
|
|
}{
|
|
{
|
|
path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard),
|
|
handler: s.phpProxy,
|
|
},
|
|
{
|
|
path: fmt.Sprintf("ajax.php"),
|
|
handler: s.HandleAjax,
|
|
},
|
|
}
|
|
|
|
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) {
|
|
f, err := os.Open(path.Join(config.Public, "index.php"))
|
|
if err != nil {
|
|
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) {
|
|
url, err := url.Parse(config.Public)
|
|
if err != nil {
|
|
log.Println(err)
|
|
} else {
|
|
proxy := httputil.NewSingleHostReverseProxy(url)
|
|
proxy.ServeHTTP(w, r)
|
|
}
|
|
}
|