Skip to content

Instantly share code, notes, and snippets.

@pkieltyka
Created December 10, 2014 19:59
Show Gist options
  • Save pkieltyka/3afcfd34bd45f29fe372 to your computer and use it in GitHub Desktop.
Save pkieltyka/3afcfd34bd45f29fe372 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net/http"
"syscall"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/web/middleware"
"github.com/zenazn/goji/graceful"
)
func main() {
app := web.New()
// App router
app.Use(middleware.RequestID)
app.Use(middleware.Logger)
app.Use(middleware.Recoverer)
app.Get("/", indexHandler)
app.Get("/hello", indexHandler)
// Admin router
admin := web.New()
// app.Handle("/admin/*", admin)
app.Handle("/admin*", admin)
admin.Use(middleware.SubRouter)
admin.Get("/", adminIndexHandler)
admin.Get("/secret", adminSecretHandler)
admin.Post("/", newAdminThingHandler)
admin.Post("/etc", etcAdminHandler)
// Start it up
graceful.AddSignal(syscall.SIGINT, syscall.SIGTERM)
err := graceful.ListenAndServe(":3333", app)
if err != nil {
log.Fatal(err)
}
graceful.Wait()
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("hi."))
}
func adminIndexHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("Admin status."))
}
func adminSecretHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("sssh, admin secrets."))
}
func newAdminThingHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("thanks for the post to /admin"))
}
func etcAdminHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("thanks for the post to /admin/etc"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment