From 4dfe7b234ed8f8832e73ca65698457ddc473949f Mon Sep 17 00:00:00 2001 From: Hardwolf Date: Tue, 9 Jun 2020 02:14:17 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20introduce=20go.mod=20fo?= =?UTF-8?q?r=20import=20local=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 27 ++++++ go.sum | 6 ++ main.go | 19 ++-- pkg/admin/admin.go | 5 +- pkg/cookie/cookieManager.go | 27 +++--- pkg/image/imageUploader.go | 10 +-- pkg/login/login.go | 3 +- pkg/post/post.go | 173 +++++++++++++++++------------------- pkg/user/usermanager.go | 2 +- 9 files changed, 151 insertions(+), 121 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..aae427a --- /dev/null +++ b/go.mod @@ -0,0 +1,27 @@ +module github.com/hardw01f/Vulnerability-goapp + +go 1.13 + +require ( + github.com/go-sql-driver/mysql v1.5.0 + github.com/hardw01f/vulnerability-goapp v0.0.0-20200608095627-9a1291f40edf // indirect + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 +) + +replace github.com/hardw01f/Vulnerability-goapp/pkg/admin => ./pkg/admin + +replace github.com/hardw01f/Vulnerability-goapp/pkg/cookei => ./pkg/cookie + +replace github.com/hardw01f/Vulnerability-goapp/pkg/user => ./pkg/user + +replace github.com/hardw01f/Vulnerability-goapp/pkg/image => ./pkg/image + +replace github.com/hardw01f/Vulnerability-goapp/pkg/login => ./pkg/login + +replace github.com/hardw01f/Vulnerability-goapp/pkg/logout => ./pkg/logout + +replace github.com/hardw01f/Vulnerability-goapp/pkg/post => ./pkg/post + +replace github.com/hardw01f/Vulnerability-goapp/pkg/register => ./pkg/register + +replace github.com/hardw01f/Vulnerability-goapp/pkg/search => ./pkg/search diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ed0a29f --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/hardw01f/vulnerability-goapp v0.0.0-20200608095627-9a1291f40edf h1:4jlqfqANv8ThioC+h0Fxz75tm+zl3Alaav7GQ/Er2eQ= +github.com/hardw01f/vulnerability-goapp v0.0.0-20200608095627-9a1291f40edf/go.mod h1:j334qfhZGFHs1bymGREtSfXoiOXwK/wt9i5PYVbDTTI= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 2ab2bfa..0548bdb 100644 --- a/main.go +++ b/main.go @@ -12,15 +12,16 @@ import ( _ "github.com/go-sql-driver/mysql" //login "github.com/Snow-HardWolf/Vulnerability-goapp/pkg/login" - "./pkg/admin" - "./pkg/cookie" - "./pkg/image" - "./pkg/login" - "./pkg/logout" - "./pkg/post" - "./pkg/register" - "./pkg/search" - "./pkg/user" + uploader "github.com/hardw01f/Vulnerability-goapp/pkg/image" + + "github.com/hardw01f/Vulnerability-goapp/pkg/admin" + "github.com/hardw01f/Vulnerability-goapp/pkg/cookie" + "github.com/hardw01f/Vulnerability-goapp/pkg/login" + "github.com/hardw01f/Vulnerability-goapp/pkg/logout" + "github.com/hardw01f/Vulnerability-goapp/pkg/post" + "github.com/hardw01f/Vulnerability-goapp/pkg/register" + "github.com/hardw01f/Vulnerability-goapp/pkg/search" + "github.com/hardw01f/Vulnerability-goapp/pkg/user" ) type Person struct { diff --git a/pkg/admin/admin.go b/pkg/admin/admin.go index 232fa3d..5eb2be1 100644 --- a/pkg/admin/admin.go +++ b/pkg/admin/admin.go @@ -3,7 +3,6 @@ package admin import ( "database/sql" "fmt" - "golang.org/x/xerrors" "html/template" "log" "math/rand" @@ -12,7 +11,9 @@ import ( "strings" "time" - "../cookie" + "golang.org/x/xerrors" + + "github.com/hardw01f/Vulnerability-goapp/pkg/cookie" ) type Lists struct { diff --git a/pkg/cookie/cookieManager.go b/pkg/cookie/cookieManager.go index 86398ad..b6340d1 100644 --- a/pkg/cookie/cookieManager.go +++ b/pkg/cookie/cookieManager.go @@ -67,9 +67,9 @@ func CheckSessionID(r *http.Request) bool { return false } - if sessionID.Value == "" || userID.Value == "" { - return false - } + if sessionID.Value == "" || userID.Value == "" { + return false + } uid, err := strconv.Atoi(userID.Value) if err != nil { @@ -84,11 +84,11 @@ func CheckSessionID(r *http.Request) bool { } -func GetCookieValue(r *http.Request) (SessionID string, UserName string, UserID int, err error){ - sessionID, err := r.Cookie("SessionID") +func GetCookieValue(r *http.Request) (SessionID string, UserName string, UserID int, err error) { + sessionID, err := r.Cookie("SessionID") if err != nil { fmt.Println(err) - return "","",0,err + return "", "", 0, err } userID, err := r.Cookie("UserID") @@ -98,19 +98,18 @@ func GetCookieValue(r *http.Request) (SessionID string, UserName string, UserID uid, err := strconv.Atoi(userID.Value) if err != nil { fmt.Println(err) - return "","",0,err + return "", "", 0, err } - userName, err := r.Cookie("UserName") - if err != nil { - fmt.Println(err) - return "","",0,err - } + userName, err := r.Cookie("UserName") + if err != nil { + fmt.Println(err) + return "", "", 0, err + } - return sessionID.Value, userName.Value, uid, nil + return sessionID.Value, userName.Value, uid, nil } - func CheckCookieOnlyLogin(r *http.Request) (userNameCookie string, sessionIDCookie string, err error) { userName, err := r.Cookie("UserName") if err != nil { diff --git a/pkg/image/imageUploader.go b/pkg/image/imageUploader.go index 9fd8214..38c0e22 100644 --- a/pkg/image/imageUploader.go +++ b/pkg/image/imageUploader.go @@ -8,8 +8,8 @@ import ( "net/http" "os" - "../cookie" - "../user" + "github.com/hardw01f/Vulnerability-goapp/pkg/cookie" + "github.com/hardw01f/Vulnerability-goapp/pkg/user" ) type User struct { @@ -42,7 +42,7 @@ func ShowImageChangePage(w http.ResponseWriter, r *http.Request) { } } -func UpdateDatabase(r *http.Request, imageName string)error{ +func UpdateDatabase(r *http.Request, imageName string) error { _, _, uid, err := cookie.GetCookieValue(r) if err != nil { fmt.Printf("%+v\n", err) @@ -57,7 +57,7 @@ func UpdateDatabase(r *http.Request, imageName string)error{ defer db.Close() _, err = db.Exec("update vulnapp.userdetails set userimage = ? where uid = ?", imageName, uid) - if err != nil { + if err != nil { fmt.Printf("%+v\n", err) return err } @@ -89,7 +89,7 @@ func UploadImage(w http.ResponseWriter, r *http.Request) { io.Copy(f, file) UpdateDatabase(r, handler.Filename) - http.Redirect(w, r, "/profile", 301) + http.Redirect(w, r, "/profile", 301) } } else { http.NotFound(w, nil) diff --git a/pkg/login/login.go b/pkg/login/login.go index d32b4d5..5af0214 100644 --- a/pkg/login/login.go +++ b/pkg/login/login.go @@ -1,13 +1,14 @@ package login import ( - "../cookie" "database/sql" "encoding/base64" "fmt" "log" "net/http" "text/template" + + "github.com/hardw01f/Vulnerability-goapp/pkg/cookie" ) type Person struct { diff --git a/pkg/post/post.go b/pkg/post/post.go index 397d705..c5d9e93 100644 --- a/pkg/post/post.go +++ b/pkg/post/post.go @@ -7,8 +7,8 @@ import ( "reflect" "text/template" - "../cookie" - "../user" + "github.com/hardw01f/Vulnerability-goapp/pkg/cookie" + "github.com/hardw01f/Vulnerability-goapp/pkg/user" ) type User struct { @@ -23,10 +23,10 @@ type User struct { } type Posts struct { - Uids []string - UserPosts []string - Created_at []string - UserImages []string + Uids []string + UserPosts []string + Created_at []string + UserImages []string } func StorePost(uid int, postText string) { @@ -45,92 +45,89 @@ func StorePost(uid int, postText string) { } func GetPost(uid int) ([]string, []string, error) { - db, err := sql.Open("mysql", "root:rootwolf@tcp(mysql)/vulnapp") + db, err := sql.Open("mysql", "root:rootwolf@tcp(mysql)/vulnapp") if err != nil { fmt.Printf("%+v\n", err) - return []string{},[]string{},err + return []string{}, []string{}, err } defer db.Close() - res, err := db.Query("select post,created_at from posts where uid=? order by created_at desc",uid) - if err != nil { - fmt.Printf("%+v\n",err) - return []string{},[]string{},err - } - defer res.Close() - - var posts []string - var create_ats []string + res, err := db.Query("select post,created_at from posts where uid=? order by created_at desc", uid) + if err != nil { + fmt.Printf("%+v\n", err) + return []string{}, []string{}, err + } + defer res.Close() - var post string - var create_at string - for res.Next() { - if err := res.Scan(&post,&create_at); err != nil { - fmt.Println(err) - } - posts = append(posts,post) - create_ats = append(create_ats,create_at) - } + var posts []string + var create_ats []string + var post string + var create_at string + for res.Next() { + if err := res.Scan(&post, &create_at); err != nil { + fmt.Println(err) + } + posts = append(posts, post) + create_ats = append(create_ats, create_at) + } - return posts, create_ats, nil + return posts, create_ats, nil } func GetTimeline() ([]int, []string, []string, error) { - db, err := sql.Open("mysql", "root:rootwolf@tcp(mysql)/vulnapp") + db, err := sql.Open("mysql", "root:rootwolf@tcp(mysql)/vulnapp") if err != nil { fmt.Printf("%+v\n", err) - return []int{},[]string{},[]string{},err + return []int{}, []string{}, []string{}, err } defer db.Close() - res, err := db.Query("select uid,post,created_at from posts order by created_at desc") - if err != nil { - fmt.Printf("%+v\n",err) - return []int{},[]string{},[]string{},err - } - defer res.Close() - - var uids []int - var posts []string - var create_ats []string - - var userid int - var post string - var create_at string - for res.Next() { - if err := res.Scan(&userid,&post,&create_at); err != nil { - fmt.Println(err) - } - uids = append(uids,userid) - posts = append(posts,post) - create_ats = append(create_ats,create_at) - } - - - return uids, posts, create_ats, nil -} + res, err := db.Query("select uid,post,created_at from posts order by created_at desc") + if err != nil { + fmt.Printf("%+v\n", err) + return []int{}, []string{}, []string{}, err + } + defer res.Close() + + var uids []int + var posts []string + var create_ats []string + + var userid int + var post string + var create_at string + for res.Next() { + if err := res.Scan(&userid, &post, &create_at); err != nil { + fmt.Println(err) + } + uids = append(uids, userid) + posts = append(posts, post) + create_ats = append(create_ats, create_at) + } + return uids, posts, create_ats, nil +} func ShowAddPostPage(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { if cookie.CheckSessionID(r) { - _, _, uid, err := cookie.GetCookieValue(r) + _, _, uid, err := cookie.GetCookieValue(r) if err != nil { fmt.Printf("%+v\n", err) http.NotFound(w, nil) return } - posts, creates, err := GetPost(uid) - if err != nil { - fmt.Printf("%+v\n",err) - return - } + posts, creates, err := GetPost(uid) + if err != nil { + fmt.Printf("%+v\n", err) + return + } - p := Posts{UserPosts: posts, Created_at: creates} + p := Posts{UserPosts: posts, Created_at: creates} - fmt.Println(p) + fmt.Println(p) t, _ := template.ParseFiles("./views/post/post.gtpl") t.Execute(w, p) @@ -163,34 +160,34 @@ func ShowAddPostPage(w http.ResponseWriter, r *http.Request) { } func ShowTimeline(w http.ResponseWriter, r *http.Request) { - if r.Method == "GET" { - if cookie.CheckSessionID(r) { - uids, allPosts, allTime, err := GetTimeline() - if err != nil { - fmt.Printf("%+v\n",err) - } + if r.Method == "GET" { + if cookie.CheckSessionID(r) { + uids, allPosts, allTime, err := GetTimeline() + if err != nil { + fmt.Printf("%+v\n", err) + } - var userImages []string + var userImages []string - for _, uid := range uids { - userImage, _, _, _, err := user.GetUserMoreDetails(uid) - if err != nil { - fmt.Printf("%+v\n",err) - } - userImages = append(userImages, userImage) - } + for _, uid := range uids { + userImage, _, _, _, err := user.GetUserMoreDetails(uid) + if err != nil { + fmt.Printf("%+v\n", err) + } + userImages = append(userImages, userImage) + } - p := Posts{UserPosts: allPosts, Created_at: allTime, UserImages: userImages} + p := Posts{UserPosts: allPosts, Created_at: allTime, UserImages: userImages} - fmt.Println(p) + fmt.Println(p) - t, _ := template.ParseFiles("./views/post/timeline.gtpl") - t.Execute(w,p) + t, _ := template.ParseFiles("./views/post/timeline.gtpl") + t.Execute(w, p) - } + } - } else if r.Method == "POST" { - if cookie.CheckSessionID(r) { + } else if r.Method == "POST" { + if cookie.CheckSessionID(r) { _, _, uid, err := cookie.GetCookieValue(r) if err != nil { fmt.Printf("%+v\n", err) @@ -210,9 +207,7 @@ func ShowTimeline(w http.ResponseWriter, r *http.Request) { } - - } else { - http.NotFound(w,nil) - } + } else { + http.NotFound(w, nil) + } } - diff --git a/pkg/user/usermanager.go b/pkg/user/usermanager.go index a1d96ca..bfeca11 100644 --- a/pkg/user/usermanager.go +++ b/pkg/user/usermanager.go @@ -7,7 +7,7 @@ import ( "strconv" "text/template" - "../cookie" + "github.com/hardw01f/Vulnerability-goapp/pkg/cookie" ) type User struct {