Last active
August 29, 2015 14:23
-
-
Save tomyhero/7cd7e2f1885fac993942 to your computer and use it in GitHub Desktop.
goji db middleware
This file contains hidden or 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 middleware | |
import ( | |
"database/sql" | |
"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/zenazn/goji/web" | |
"net/http" | |
) | |
// TODO slave/master | |
func Database(c *web.C, h http.Handler) http.Handler { | |
fn := func(w http.ResponseWriter, r *http.Request) { | |
con, err := sql.Open("mysql", "root"+":"+""+"@/foo") | |
defer con.Close() | |
if err != nil { | |
// navigate to system error page | |
fmt.Println(err) | |
} | |
// XXX maybe remove this section | |
err = con.Ping() | |
if err != nil { | |
// navigate to system error page | |
fmt.Println(err) | |
} | |
c.Env["db"] = con | |
h.ServeHTTP(w, r) | |
} | |
return http.HandlerFunc(fn) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment