init
This commit is contained in:
commit
fa6a4c650f
93 files changed
+6555
No files matched your search
@@ -0,0 +1,169 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
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/",
|
||||
s.handleObjectRoute,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/history/",
|
||||
s.handleRevisionBrowser,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/view/",
|
||||
s.handleRevisionView,
|
||||
)
|
||||
|
||||
s.mux.HandleFunc(
|
||||
"/r/diff/",
|
||||
s.handleRevisionDiff,
|
||||
)
|
||||
|
||||
s.registerRoutes()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) registerStaticRoutes() {
|
||||
|
||||
staticFS := http.FileServer(
|
||||
http.Dir("./static"),
|
||||
)
|
||||
|
||||
s.mux.Handle(
|
||||
"/static/",
|
||||
http.StripPrefix(
|
||||
"/static/",
|
||||
staticFS,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Server) mustLoadTemplates() {
|
||||
|
||||
funcs := template.FuncMap{
|
||||
|
||||
"parentPath": parentPath,
|
||||
|
||||
"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
|
||||
},
|
||||
}
|
||||
|
||||
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.handleSemanticEditor,
|
||||
SemanticPage: s.handleSemanticRoute,
|
||||
RawView: s.handleRawView,
|
||||
LinkManager: s.handleLinkManager,
|
||||
Health: s.handleHealth,
|
||||
Tree: s.handleTree,
|
||||
},
|
||||
)
|
||||
|
||||
routespkg.RegisterAPIRoutes(
|
||||
s.mux,
|
||||
routespkg.APIHandlers{
|
||||
CreateObject: s.handleCreateObjectAPI,
|
||||
ListObjects: s.handleListObjectsAPI,
|
||||
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,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
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(),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user