Skip to content

Commit

Permalink
Merge branch 'master' of github.com:go-bootstrap/go-bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
didip committed Dec 30, 2015
2 parents 6283e6b + b4a6404 commit d4c2023
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion blank/handlers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetHome(w http.ResponseWriter, r *http.Request) {
session, _ := cookieStore.Get(r, "$GO_BOOTSTRAP_PROJECT_NAME-session")
currentUser, ok := session.Values["user"].(*dal.UserRow)
if !ok {
http.Redirect(w, r, "/logout", 301)
http.Redirect(w, r, "/logout", 302)
return
}

Expand Down
8 changes: 4 additions & 4 deletions blank/handlers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GetLogin(w http.ResponseWriter, r *http.Request) {

currentUserInterface := session.Values["user"]
if currentUserInterface != nil {
http.Redirect(w, r, "/", 301)
http.Redirect(w, r, "/", 302)
return
}

Expand Down Expand Up @@ -99,7 +99,7 @@ func PostLogin(w http.ResponseWriter, r *http.Request) {
return
}

http.Redirect(w, r, "/", 301)
http.Redirect(w, r, "/", 302)
}

func GetLogout(w http.ResponseWriter, r *http.Request) {
Expand All @@ -112,7 +112,7 @@ func GetLogout(w http.ResponseWriter, r *http.Request) {
delete(session.Values, "user")
session.Save(r, w)

http.Redirect(w, r, "/login", 301)
http.Redirect(w, r, "/login", 302)
}

func PostPutDeleteUsersID(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -167,7 +167,7 @@ func PutUsersID(w http.ResponseWriter, r *http.Request) {
return
}

http.Redirect(w, r, "/", 301)
http.Redirect(w, r, "/", 302)
}

func DeleteUsersID(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 3 additions & 2 deletions blank/middlewares/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
package middlewares

import (
"net/http"

"github.com/gorilla/context"
"github.com/gorilla/sessions"
"github.com/jmoiron/sqlx"
"net/http"
)

func SetDB(db *sqlx.DB) func(http.Handler) http.Handler {
Expand Down Expand Up @@ -36,7 +37,7 @@ func MustLogin(next http.Handler) http.Handler {
userRowInterface := session.Values["user"]

if userRowInterface == nil {
http.Redirect(res, req, "/login", 301)
http.Redirect(res, req, "/login", 302)
return
}

Expand Down
20 changes: 18 additions & 2 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func DefaultPGDSN(dbName string) string {
hostPortSeparator := ":"

if pguser == "" {
u, _ := user.Current()
pguser = u.Username
pguser = GetCurrentUser()
}

isUnixDomainSocket := strings.HasPrefix(pghost, "/")
Expand Down Expand Up @@ -154,3 +153,20 @@ func ExitOnError(err error, msg string) {
log.Fatalf("%s\n%s", msg, err.Error())
}
}

// GetCurrentUser returns the username of the current user.
func GetCurrentUser() string {
currentUser, err := user.Current()

if err == nil {
return currentUser.Username
} else {
username := os.Getenv("USERNAME")

if username == "" {
log.Fatalln("Cannot determine current user's username")
}

return username
}
}
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -56,8 +55,6 @@ func main() {
projectName := dirChunks[len(dirChunks)-1]
dbName := projectName
testDbName := projectName + "-test"
currentUser, _ := user.Current()

blankDir, err := helpers.GetBlankDir()
helpers.ExitOnError(err, "")

Expand Down Expand Up @@ -88,7 +85,7 @@ func main() {
replacers["$GO_BOOTSTRAP_REPO_USER"] = repoUser
replacers["$GO_BOOTSTRAP_PROJECT_NAME"] = projectName
replacers["$GO_BOOTSTRAP_COOKIE_SECRET"] = helpers.RandString(16)
replacers["$GO_BOOTSTRAP_CURRENT_USER"] = currentUser.Username
replacers["$GO_BOOTSTRAP_CURRENT_USER"] = helpers.GetCurrentUser()
replacers["$GO_BOOTSTRAP_PG_DSN"] = helpers.DefaultPGDSN(dbName)
replacers["$GO_BOOTSTRAP_ESCAPED_PG_DSN"] = helpers.BashEscape(helpers.DefaultPGDSN(dbName))
replacers["$GO_BOOTSTRAP_PG_TEST_DSN"] = helpers.DefaultPGDSN(testDbName)
Expand Down

0 comments on commit d4c2023

Please sign in to comment.