0
0
Fork 0

use two routers instead of one

This change splits the areas up a bit and makes it easier to, maybe,
make two applications out of it.
This commit is contained in:
Gibheer 2015-01-08 21:33:56 +01:00
parent 574b05887a
commit ea99d621bd
3 changed files with 42 additions and 23 deletions

25
main.go
View File

@ -11,7 +11,6 @@ import (
"strings"
"time"
"github.com/julienschmidt/httprouter"
"gopkg.in/yaml.v2"
)
@ -23,7 +22,8 @@ type (
TemplatePath string // path to the templates
}
Middleware struct {
router *httprouter.Router
admin *Admin
site *Site
}
)
@ -35,16 +35,14 @@ var (
func (l Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
w.Header()["Content-Type"] = []string{"text/html"}
l.session(w, r)
log.Print(r.URL.Path, ";", time.Since(start))
}
func (l *Middleware) session(w http.ResponseWriter, r *http.Request) {
// catch admin panel sessions
if strings.HasPrefix(r.URL.Path, admin_url_prefix) {
if !check_valid_session(w, r) { return }
l.admin.ServeHTTP(w, r)
} else {
l.site.ServeHTTP(w, r)
}
l.router.ServeHTTP(w, r)
log.Print(r.URL.Path, ";", time.Since(start))
}
func check_valid_session(w http.ResponseWriter, r *http.Request) bool {
@ -76,10 +74,7 @@ func main() {
}
defer DB.Close()
router := httprouter.New()
Routes(router)
AdminRoutes(router)
start_server(config, router)
start_server(config)
}
// Reads the config from config_path and returns the settings.
@ -98,7 +93,7 @@ func readConfig(config_path string) (*Config, error) {
}
// start the server with access log
func start_server(config *Config, router *httprouter.Router) {
middleware := Middleware{router}
func start_server(config *Config) {
middleware := Middleware{NewAdmin(), NewSite()}
log.Print(http.ListenAndServe(fmt.Sprintf("%v:%v", config.Address, config.Port), middleware))
}

19
site.go
View File

@ -6,11 +6,20 @@ import (
"github.com/julienschmidt/httprouter"
)
// map URLs to their functions
func Routes(router *httprouter.Router) {
router.GET("/", RouteIndex)
router.GET("/post", RoutePosts)
router.GET("/post/:id", RoutePostsWithID)
type Site struct {
router *httprouter.Router
}
func NewSite() *Site {
site := &Site{httprouter.New()}
site.router.GET("/", RouteIndex)
site.router.GET("/post", RoutePosts)
site.router.GET("/post/:id", RoutePostsWithID)
return site
}
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
// the function for route /

View File

@ -6,10 +6,25 @@ import (
"github.com/julienschmidt/httprouter"
)
func AdminRoutes(router *httprouter.Router) {
type Admin struct {
router *httprouter.Router
}
func NewAdmin() *Admin {
admin := &Admin{httprouter.New()}
p := "/admin/" // prefix
router.GET(p + "login", AdminLogin)
router.GET(p + "foo", AdminFoo)
admin.router.GET(p, AdminRoot)
admin.router.GET(p + "login", AdminLogin)
admin.router.GET(p + "foo", AdminFoo)
return admin
}
func (a *Admin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.router.ServeHTTP(w, r)
}
func AdminRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "landed on root!")
}
// Login the user into the admin panel