Skip to content

Commit

Permalink
Fixed router setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Jun 4, 2024
1 parent df9dad8 commit af2b62f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.6.2

* fixed router setup

## 0.6.1

* fixed router setup
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func main() {
})

// Start the server. The router and cancel function are optional.
if err := server.Start(nil, nil); err != nil {
if err := server.Start(nil); err != nil {
slog.Error("Error starting Shifu", "error", err)
}
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/shifu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
shifu "github.com/emvi/shifu/pkg"
"github.com/go-chi/chi/v5"
"log/slog"
"net/http"
"os"
"strings"
)
Expand All @@ -29,9 +31,16 @@ func main() {

switch cmd {
case "run":
server := shifu.NewServer(dir, shifu.ServerOptions{})
r := chi.NewRouter()
r.HandleFunc("/debug", func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("HELLO WORLD!"))
})

if err := server.Start(nil, nil); err != nil {
server := shifu.NewServer(dir, shifu.ServerOptions{
Router: r,
})

if err := server.Start(nil); err != nil {
slog.Error("Error starting Shifu", "error", err)
}
case "init":
Expand Down
41 changes: 25 additions & 16 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ type Server struct {
// Content is the CMS content.
Content *cms.CMS

// Router is the router for the server.
// It has a CORS and gzip middleware, as well as a route for robots.txt, the static directory, and any unmatched path (/*) by default.
Router chi.Router

router chi.Router
dir string
funcMap template.FuncMap
}
Expand All @@ -51,15 +48,15 @@ type Server struct {
// The second argument is an optional template.FuncMap that will be merged with Shifu's funcmap.
func NewServer(dir string, options ServerOptions) *Server {
return &Server{
Router: options.Router,
router: options.Router,
dir: dir,
funcMap: options.FuncMap,
}
}

// Start starts the Shifu server.
// The context.CancelFunc is optional and will be called on server shutdown or error if set.
func (server *Server) Start(router chi.Router, cancel context.CancelFunc) error {
func (server *Server) Start(cancel context.CancelFunc) error {
slog.Info("Starting Shifu", "version", version, "directory", server.dir)
server.funcMap = cms.Merge(server.funcMap)
ctx, cancelServer := context.WithCancel(context.Background())
Expand Down Expand Up @@ -112,28 +109,40 @@ func (server *Server) Start(router chi.Router, cancel context.CancelFunc) error
})
analytics.Init()
server.setupRouter(server.dir, server.Content, sm)
<-server.startServer(server.Router, stop)
<-server.startServer(server.router, stop)
return nil
}

func (server *Server) setupRouter(dir string, cms *cms.CMS, sm *sitemap.Sitemap) {
var router chi.Router

if server.Router != nil {
router = server.Router
} else {
router = chi.NewRouter()
}

router := chi.NewRouter()
router.Use(
middleware.Cors(),
middleware.Gzip(),
)

if server.router != nil {
slog.Info("Merging router with Shifu router...")

for _, route := range server.router.Routes() {
for method, handler := range route.Handlers {
if method == "*" {
router.Handle(route.Pattern, handler)
} else {
router.Method(method, route.Pattern, handler)
}
}
}
}

sm.Serve(router)
server.serveRobotsTxt(router)
server.serveStaticDir(router, dir)
router.Handle("/*", http.HandlerFunc(cms.Serve))
server.Router = router
server.router = router

for _, route := range router.Routes() {
slog.Info("Added route", "route", route.Pattern)
}
}

func (server *Server) serveRobotsTxt(router chi.Router) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pkg

const (
version = "0.6.1"
version = "0.6.2"
)

// Version returns the Shifu version number.
Expand Down

0 comments on commit af2b62f

Please sign in to comment.