package server import ( "fmt" "html/template" "log" "net/http" "strings" "time" mw "pageswiki/internal/server/middleware" routespkg "pageswiki/internal/server/routes" "pageswiki/internal/store" ) type Server struct { mux *http.ServeMux templates *template.Template store *store.Store } func New( st *store.Store, ) *Server { s := &Server{ mux: http.NewServeMux(), store: st, } s.mustLoadTemplates() s.registerStaticRoutes() s.mux.HandleFunc( "/o/e/", s.handleObjectEdit, ) s.mux.HandleFunc( "/o/r/", s.handleObjectRevisionHistory, ) s.mux.HandleFunc( "/o/", func(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.URL.Path, "/o/uuid:") { s.handleObjectView(w, r) return } s.handleObjectRoute(w, r) }, ) s.mux.HandleFunc( "/r/history/", s.handleRevisionBrowser, ) s.mux.HandleFunc( "/r/view/", s.handleRevisionView, ) s.mux.HandleFunc( "/r/diff/", s.handleRevisionDiff, ) s.registerRoutes() s.startGCWorker() return s } func (s *Server) registerStaticRoutes() { staticFS := http.FileServer( http.Dir("./static"), ) s.mux.Handle( "/static/", http.StripPrefix( "/static/", staticFS, ), ) themeFS := http.FileServer( http.Dir("/data/themes"), ) s.mux.Handle( "/-/theme/", http.StripPrefix( "/-/theme/", themeFS, ), ) } func (s *Server) mustLoadTemplates() { funcs := template.FuncMap{ "parentPath": parentPath, "T": s.T, "sub": func(a, b int) int { return a - b }, "add": func(a, b int) int { return a + b }, "until": func(n int) []int { out := make([]int, n) for i := 0; i < n; i++ { out[i] = i } return out }, "formatBytes": func(n int) string { if n < 1024 { return fmt.Sprintf("%d B", n) } if n < 1024*1024 { return fmt.Sprintf("%.1f KiB", float64(n)/1024.0) } return fmt.Sprintf("%.1f MiB", float64(n)/(1024.0*1024.0)) }, "formatUnix": func(ts int64) string { if ts <= 0 { return "-" } return time. Unix(ts, 0). In(s.getLocation()). Format("2006-01-02 15:04") }, } tmpl := template.New("").Funcs(funcs) parsed, err := tmpl.ParseGlob( "templates/**/*.gohtml", ) if err != nil { panic(err) } s.templates = parsed } func (s *Server) registerRoutes() { routespkg.RegisterUIRoutes( s.mux, routespkg.UIHandlers{ Home: s.handleHome, NewPage: s.handleNewPage, SemanticEditor: s.handleEditorRoute, SemanticPage: s.handleSemanticRoute, RawView: s.handleRawView, LinkManager: s.handleLinkManager, Health: s.handleHealth, Tree: s.handleTree, Browser: s.handleBrowser, Preview: s.handlePreview, PlainView: s.handlePlainView, MarkdownView: s.handleMarkdownView, CompileMarkdown: s.handleCompileMarkdown, CompileDiagram: s.handleCompileDiagram, Config: s.handleConfig, }, ) routespkg.RegisterAPIRoutes( s.mux, routespkg.APIHandlers{ CreateObject: s.handleCreateObjectAPI, ListObjects: s.handleListObjectsAPI, DeleteObject: s.handleDeleteObjectAPI, SaveRevision: s.handleSaveRevisionAPI, ListRevisions: s.handleRevisionListAPI, RestoreRevision: s.handleRestoreRevisionAPI, RenderMarkdown: s.handleMarkdownRenderAPI, AddTag: s.handleTagAddAPI, BindPath: s.handleBindPathAPI, RenamePath: s.handleRenamePath, RenamePreview: s.handleRenamePreview, RemovePath: s.handleRemovePathAPI, ZombieList: s.handleZombieListAPI, ZombiePurge: s.handleZombiePurgeAPI, }, ) } func (s *Server) handler() http.Handler { var h http.Handler = s.mux h = mw.NormalizePath(h) h = mw.RequestID(h) h = mw.Logging(h) h = mw.Recovery(h) return h } func (s *Server) Run(addr string) error { log.Printf( "listen %s", addr, ) return http.ListenAndServe( addr, s.handler(), ) }