Created
December 10, 2014 19:59
-
-
Save pkieltyka/3afcfd34bd45f29fe372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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