todo-server/server/routes.go

189 lines
4.8 KiB
Go
Executable File

package server
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"gogs.inhome.blapointe.com/local/gziphttp"
"gogs.inhome.blapointe.com/local/oauth2/oauth2client"
"gogs.inhome.blapointe.com/local/router"
"gogs.inhome.blapointe.com/local/todo-server/config"
)
func (s *Server) Routes() error {
routes := []struct {
path string
handler http.HandlerFunc
}{
{
path: "/",
handler: s.index,
},
{
path: "/mytinytodo_lang.php",
handler: s.lang,
},
{
path: fmt.Sprintf("/themes/%s%s", router.Wildcard, router.Wildcard),
handler: s.handleDeviceCSS,
},
{
path: fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard),
handler: s.phpProxy,
},
{
path: "/ajax.php",
handler: s.HandleAjax,
},
}
for _, route := range routes {
handler := route.handler
handler = s.gzip(handler)
handler = s.oauth(handler)
if err := s.Add(route.path, handler); err != nil {
return err
}
}
return nil
}
func (s *Server) lang(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `
mytinytodo.lang.init({
confirmDelete: "Are you sure you want to delete the task?",
confirmLeave: "There can be unsaved data. Do you really want to leave?",
actionNoteSave: "save",
actionNoteCancel: "cancel",
error: "Some error occurred (click for details)",
denied: "Access denied",
invalidpass: "Wrong password",
tagfilter: "Tag:",
addList: "Create new list",
addListDefault: "Todo",
renameList: "Rename list",
deleteList: "This will delete current list with all tasks in it.\nAre you sure?",
clearCompleted: "This will delete all completed tasks in the list.\nAre you sure?",
settingsSaved: "Settings saved. Reloading...",
daysMin: ["Su","Mo","Tu","We","Th","Fr","Sa"],
daysLong: ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
monthsLong: ["January","February","March","April","May","June","July","August","September","October","November","December"],
tags: "Tags",
tasks: "Tasks",
f_past: "Overdue",
f_today: "Today and tomorrow",
f_soon: "Soon"
});
`)
}
func (s *Server) toindex(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (s *Server) index(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(path.Join(config.Root, "index.html"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
io.Copy(w, f)
}
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("http://127.0.0.1:64123")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
log.Println("WOULD proxy", url.String(), r.URL.Path)
s.toindex(w, r)
//proxy := httputil.NewSingleHostReverseProxy(url)
//proxy.ServeHTTP(w, r)
}
}
func (s *Server) static(w http.ResponseWriter, r *http.Request) {
if err := s._static(w, r); err != nil {
s.toindex(w, r)
}
}
func (s *Server) _static(w http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodGet {
http.FileServer(s.fileDir).ServeHTTP(w, r)
return nil
}
f, err := s.fileDir.Open(path.Clean(r.URL.Path))
if err != nil {
return err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return err
}
if info.IsDir() {
r.URL.Path = path.Join(r.URL.Path, "index.html")
f, err = s.fileDir.Open(r.URL.Path)
defer f.Close()
if err != nil {
return err
}
}
gziphttp.SetContentTypeIfMedia(w, r)
_, err = io.Copy(w, f)
return err
}
func (s *Server) oauth(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if config.OAuth != "" {
err := oauth2client.Authenticate(config.OAuth, r.Host, w, r)
if err != nil {
log.Println("oauth failure", r.Host, ":", err)
return
}
}
h(w, r)
}
}
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if gziphttp.Can(r) {
gz := gziphttp.New(w)
defer gz.Close()
w = gz
}
if filepath.Ext(r.URL.Path) == ".css" {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
}
h(w, r)
}
}
func (s *Server) handleDeviceCSS(w http.ResponseWriter, r *http.Request) {
if _, ok := r.URL.Query()["pda"]; ok || strings.Contains(r.Header.Get("User-Agent"), "Android") || strings.Contains(r.Header.Get("User-Agent"), "Mobile") {
if path.Base(r.URL.Path) == "print.css" {
r.URL.Path = path.Join(path.Dir(r.URL.Path), "pda.css")
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
return
}
}
s.static(w, r)
}