Last active
August 29, 2015 14:16
-
-
Save rocknrollMarc/2702f8de03739420a772 to your computer and use it in GitHub Desktop.
Go webapp with gorilla and Negroni
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 ( | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/codegangsta/negroni" | |
"github.com/gorilla/mux" | |
"github.com/russross/blackfriday" | |
"github.com/yvasiyarov/gorelic" | |
) | |
func main() { | |
// Middleware stack (Negroni) | |
n := negroni.New( | |
negroni.NewRecovery(), | |
negroni.HandlerFunc(MyMiddleware), | |
negroni.NewLogger(), | |
negroni.NewStatic(http.Dir("public")), | |
) | |
n.Run(":8080") | |
agent := gorelic.NewAgent() | |
agent.Verbose = true | |
agent.NewrelicLicense = "ecd905aa235475b2bc6c9b84dec4cffefe51ac094924945" | |
agent.Run() | |
} | |
func MyMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | |
log.Println("Logging on the way there...") | |
if r.URL.Query().Get("password") == "meinefrau011280" { | |
next(rw, r) | |
} else { | |
http.Error(rw, "Not Authorized", 401) | |
} | |
log.Println("Logging on the way back...") | |
} | |
func HomeHandler(rw http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(rw, "Home") | |
} | |
func PostsIndexHandler(rw http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(rw, "posts index") | |
} | |
func PostsCreateHandler(rw http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(rw, "posts create") | |
} | |
func PostsShowHandler(rw http.ResponseWriter, r *http.Request) { | |
id := mux.Vars(r)["id"] | |
fmt.Fprintln(rw, "showing post", id) | |
} | |
func PostUpdateHandler(rw http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(rw, "post update") | |
} | |
func PostDeleteHandler(rw http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(rw, "post delete") | |
} | |
func PostEditHandler(rw http.ResponseWriter, r *http.Request) { | |
fmt.Fprintln(rw, "post edit") | |
} | |
func GenerateMarkdown(rw http.ResponseWriter, r *http.Request) { | |
markdown := blackfriday.MarkdownCommon([]byte(r.FormValue("body"))) | |
rw.Write(markdown) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment