Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix golint issues #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"os"
"time"
)
import _ "github.com/go-sql-driver/mysql"

// mysql driver for sql database
import _ "github.com/go-sql-driver/mysql"

var Db *sql.DB

Expand All @@ -17,11 +18,10 @@ func init() {
// To avoid client timeout
Db.SetConnMaxLifetime(time.Second)

if err!= nil {
if err != nil {
log.Print(err)
return
} else
{
} else {
log.Print("Successfully connected to datasource: " + getDatasource())
}
return
Expand All @@ -32,4 +32,3 @@ func getDatasource() (dataSource string) {
")/" + os.Getenv("DB_NAME") + "?parseTime=true"
return
}

56 changes: 29 additions & 27 deletions google_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,52 @@ package main
import (
"context"
"encoding/json"
"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
"github.com/nu7hatch/gouuid"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)

var googleOauthConfig = &oauth2.Config{
RedirectURL: os.Getenv("OAUTH_REDIRECT_URI"),
ClientID: os.Getenv("CLIENT_ID"),
RedirectURL: os.Getenv("OAUTH_REDIRECT_URI"),
ClientID: os.Getenv("CLIENT_ID"),
ClientSecret: os.Getenv("CLIENT_SECRET"),
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
Endpoint: google.Endpoint,
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
Endpoint: google.Endpoint,
}

// GoogleUserInfo struct provides the info of the user
type GoogleUserInfo struct {
Id int `json:"id"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Link url.URL `json:"link"`
Picture url.URL `json:"picture"`
ID int `json:"id"`
Email string `json:"email"`
VerifiedEmail bool `json:"verified_email"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Link url.URL `json:"link"`
Picture url.URL `json:"picture"`
}

const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_token="

const oauthGoogleUrlAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_token="

// GoogleSignUp registers the user
func GoogleSignUp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
u4, _ := uuid.NewV4()
oauthState := u4.String()
cookie := http.Cookie{
Name:"oauthState",
Value:oauthState,
Name: "oauthState",
Value: oauthState,
HttpOnly: true,
Expires: time.Now().Add(365 * 24 * time.Hour),
Path:"/oauth/google",
Expires: time.Now().Add(365 * 24 * time.Hour),
Path: "/oauth/google",
}

http.SetCookie(w, &cookie)
Expand All @@ -56,6 +57,7 @@ func GoogleSignUp(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

// GoogleSignUpCallback returns the data from API and authenticates the user
func GoogleSignUpCallback(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
oauthStateCookie, _ := r.Cookie("oauthState")
oauthState := oauthStateCookie.Value
Expand Down Expand Up @@ -101,11 +103,11 @@ func GoogleSignUpCallback(w http.ResponseWriter, r *http.Request, _ httprouter.P
throwError(err)
session, err := loggedInUser.CreateSession()
cookie := http.Cookie{
Name:"_cookie",
Value: session.Uuid,
Name: "_cookie",
Value: session.Uuid,
HttpOnly: true,
Path:"/",
Path: "/",
}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/", 302)
}
}
60 changes: 32 additions & 28 deletions route_auth.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
package main

import (
"log"
"net/http"

"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
"golang.org/x/crypto/bcrypt"
"log"
"net/http"
)

// Signup is the handler which validates url
func Signup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
checkInvalidRequests(w, r)
generateHTML(w, nil, "layout", "public.navbar", "signup")
}

// SignupAccount creates the user from the form data
func SignupAccount(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
err := r.ParseForm()
throwError(err)

user := data.User{
Username:r.PostFormValue("username"),
Email:r.PostFormValue("email"),
Password: encryptPassword(r.PostFormValue("password")),
HasPassword:true,
Username: r.PostFormValue("username"),
Email: r.PostFormValue("email"),
Password: encryptPassword(r.PostFormValue("password")),
HasPassword: true,
}

err = user.Create()
throwError(err)
http.Redirect(w, r, "/login", http.StatusFound)
}

// Login function handles the login url
func Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
checkInvalidRequests(w,r)
checkInvalidRequests(w, r)
generateHTML(w, nil, "layout", "public.navbar", "login")
}

// Authenticate handler authenticates the input form data
func Authenticate(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
err := r.ParseForm()
throwError(err)
Expand All @@ -49,40 +54,39 @@ func Authenticate(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log.Print(err)
http.Redirect(w, r, "/login", http.StatusFound)
return
} else {
session, err := user.CreateSession()
throwError(err)
}
session, err := user.CreateSession()
throwError(err)

cookie := http.Cookie{
Name: "_cookie",
Value: session.Uuid,
HttpOnly: true,
}
http.SetCookie(w,&cookie)

log.Print("User successfully logged in")
http.Redirect(w, r, "/", http.StatusFound)
cookie := http.Cookie{
Name: "_cookie",
Value: session.Uuid,
HttpOnly: true,
}
http.SetCookie(w, &cookie)

log.Print("User successfully logged in")
http.Redirect(w, r, "/", http.StatusFound)
}

// Logout closes the current session of the user
func Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
cookie, err := r.Cookie("_cookie")
if err != http.ErrNoCookie {
sess := data.Session{Uuid:cookie.Value}
sess := data.Session{Uuid: cookie.Value}
err = sess.DeleteByUUID()
if err != nil {
log.Print(err)
return
} else {
cookie := http.Cookie{
Name: "_cookie",
MaxAge: -1,
}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/", http.StatusFound)
}
cookie := http.Cookie{
Name: "_cookie",
MaxAge: -1,
}
http.SetCookie(w, &cookie)
http.Redirect(w, r, "/", http.StatusFound)
} else {
log.Print("Invalid request")
http.Redirect(w, r, "/", http.StatusFound)
}
}
}
29 changes: 16 additions & 13 deletions route_main.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package main

import (
"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
"log"
"net/http"

"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
)

// ThreadsInfoPrivate represents a list of private threads
type ThreadsInfoPrivate struct {
ThreadList []ThreadInfoPublic
User data.User
User data.User
}

// ThreadsInfoPublic represents a list of public threads
type ThreadsInfoPublic struct {
ThreadList []ThreadInfoPublic
}

// Index function creates index of thread
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
threads, err := data.Threads()
if err != nil {
log.Print(err)
return
}
sess, err := session(w, r)
loggedInUser, err := sess.User()
if err != nil {
data := ThreadsInfoPublic{CreateThreadList(threads)}
generateHTML(w, data, "layout", "public.navbar", "index")
} else {
sess, err := session(w, r)
loggedInUser, err := sess.User()
if err != nil {
data := ThreadsInfoPublic{CreateThreadList(threads)}
generateHTML(w, data, "layout","public.navbar", "index")
} else {
data := ThreadsInfoPrivate{CreateThreadList(threads), loggedInUser}
generateHTML(w, data, "layout", "private.navbar","index")
}
data := ThreadsInfoPrivate{CreateThreadList(threads), loggedInUser}
generateHTML(w, data, "layout", "private.navbar", "index")
}
}
}
24 changes: 15 additions & 9 deletions route_post.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package main

import (
"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
"strconv"

"github.com/anuragdhingra/lets-chat/data"
"github.com/julienschmidt/httprouter"
)

// PostInfoPublic organises public post data
type PostInfoPublic struct {
Post data.Post
Post data.Post
CreatedBy data.User
}

// CreatePost functions creates post from form data
func CreatePost(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
sess, err := session(w, r)
if err != nil {
Expand All @@ -22,24 +27,25 @@ func CreatePost(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
err = r.ParseForm()
throwError(err)
postBody := r.PostFormValue("body")
threadIdString := r.PostFormValue("id")
threadId, err := strconv.Atoi(threadIdString)
threadIDString := r.PostFormValue("id")
threadID, err := strconv.Atoi(threadIdString)
throwError(err)

postRequest := data.PostRequest{postBody, user.Id, threadId}
_, err = postRequest.CreatePost()
throwError(err)
http.Redirect(w, r, "/threads/" + threadIdString, http.StatusFound)
http.Redirect(w, r, "/threads/"+threadIdString, http.StatusFound)
}
}

// CreatePostList function creates a list of posts of User
func CreatePostList(posts []data.Post) (postListPublic []PostInfoPublic) {
for _, post := range posts {
postUserId := post.UserId
postUserID := post.UserId
user, err := data.UserById(postUserId)
throwError(err)
postInfoPublic := PostInfoPublic{post,user}
postInfoPublic := PostInfoPublic{post, user}
postListPublic = append(postListPublic, postInfoPublic)
}
return
}
}
Loading