├── src
├── navbar.tmpl
├── login.tmpl
├── blog.tmpl
├── register.tmpl
├── head.tmpl
├── contact.tmpl
├── index.tmpl
└── admin.tmpl
├── public
├── style.css
└── script.js
├── upload
├── yas.png
├── ban1.png
├── ban2.png
└── c1e60e28960c057d5bab5f7b008bb072.jpg
├── README.md
├── go.mod
├── go.sum
└── main.go
/src/navbar.tmpl:
--------------------------------------------------------------------------------
1 | {{ define "navbar.tmpl" }}
2 |
3 | {{ end }}
--------------------------------------------------------------------------------
/public/style.css:
--------------------------------------------------------------------------------
1 | *{
2 | margin:0;
3 | padding:0;
4 | box-sizing: border-box;
5 | }
--------------------------------------------------------------------------------
/upload/yas.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hasan-Kilici/go-gin-mongodb-portfolio-and-blog-v1/HEAD/upload/yas.png
--------------------------------------------------------------------------------
/upload/ban1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hasan-Kilici/go-gin-mongodb-portfolio-and-blog-v1/HEAD/upload/ban1.png
--------------------------------------------------------------------------------
/upload/ban2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hasan-Kilici/go-gin-mongodb-portfolio-and-blog-v1/HEAD/upload/ban2.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # go-gin-mongodb-portfolio-and-blog-v1
2 |
3 | go ve gin kullanarak yaptığım Portfolyo ve blog sitesi ancak css ve js kodlamaya üşendim
4 |
--------------------------------------------------------------------------------
/upload/c1e60e28960c057d5bab5f7b008bb072.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Hasan-Kilici/go-gin-mongodb-portfolio-and-blog-v1/HEAD/upload/c1e60e28960c057d5bab5f7b008bb072.jpg
--------------------------------------------------------------------------------
/src/login.tmpl:
--------------------------------------------------------------------------------
1 | {{ template "head.tmpl" }}
2 |
3 |
4 |
22 | {{range .blogs}}
23 |
54 | {{end}}
55 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
5 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
6 | github.com/gin-gonic/gin v1.8.2 h1:UzKToD9/PoFj/V4rvlKqTRKnQYyz8Sc1MJlv4JHPtvY=
7 | github.com/gin-gonic/gin v1.8.2/go.mod h1:qw5AYuDrzRTnhvusDsrov+fDIxp9Dleuu12h8nfB398=
8 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
9 | github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
10 | github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
11 | github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
12 | github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
13 | github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
14 | github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
15 | github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
16 | github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
17 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
18 | github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
19 | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
20 | github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
21 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
22 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
23 | github.com/gtuk/discordwebhook v1.1.0 h1:8vsfpzqbpXTWYvwbF4ghxUeXe0uP07wZeRNrAjW+WFM=
24 | github.com/gtuk/discordwebhook v1.1.0/go.mod h1:U3LdXNJ1e0bx3MMe2a4mB1VBantPHOPly2jNd8ZWXec=
25 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
26 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
27 | github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
28 | github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
29 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
30 | github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
31 | github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
32 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
33 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
34 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
35 | github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
36 | github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
37 | github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
38 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
39 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
40 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
41 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
42 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
43 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
44 | github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
45 | github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
46 | github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
47 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
48 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
49 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
50 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
51 | github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
52 | github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
53 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
54 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
55 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
56 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
57 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
58 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
59 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
60 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
61 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
62 | github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
63 | github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
64 | github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
65 | github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
66 | github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
67 | github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
68 | github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E=
69 | github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
70 | github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs=
71 | github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
72 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
73 | github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
74 | go.mongodb.org/mongo-driver v1.11.2 h1:+1v2rDQUWNcGW7/7E0Jvdz51V38XXxJfhzbV17aNHCw=
75 | go.mongodb.org/mongo-driver v1.11.2/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8=
76 | golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
77 | golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
78 | golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
79 | golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
80 | golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
81 | golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
82 | golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
83 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
84 | golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
85 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
86 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
87 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
88 | golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
89 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
90 | golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
91 | golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
92 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
93 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
94 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
95 | golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
96 | golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
97 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
98 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
99 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
100 | google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
101 | google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
102 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
103 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
104 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
105 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
106 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
107 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
108 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
109 | gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
110 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
111 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "github.com/gtuk/discordwebhook"
5 | "log"
6 | "net/http"
7 | "encoding/json"
8 | "github.com/gin-gonic/gin"
9 | "go.mongodb.org/mongo-driver/bson"
10 | "go.mongodb.org/mongo-driver/bson/primitive"
11 | "go.mongodb.org/mongo-driver/mongo"
12 | "go.mongodb.org/mongo-driver/mongo/options"
13 | "context"
14 | "time"
15 | "fmt"
16 | "io/ioutil"
17 | )
18 |
19 | type Repository struct {
20 | Name string `json:"name"`
21 | Description string `json:"description"`
22 | Language string `json:"language"`
23 | Stars int `json:"stargazers_count"`
24 | Forks int `json:"forks_count"`
25 | }
26 |
27 | type GithubUser struct {
28 | Login string `json:"login"`
29 | Followers int `json:"followers"`
30 | Following int `json:"following"`
31 | URL string `json:"html_url"`
32 | Bio string `json:"bio"`
33 | AvatarURL string `json:"avatar_url"`
34 | Email string `json:"email"`
35 | Location string `json:"location"`
36 | Repos []Repository `json:"repositories"`
37 | }
38 |
39 | type User struct {
40 | ID primitive.ObjectID `bson:"_id,omitempty"`
41 | Username string
42 | Gmail string
43 | Password string
44 | ProfilePhoto string
45 | Admin string
46 | CreatedAt time.Time
47 | }
48 |
49 | type Blog struct {
50 | ID primitive.ObjectID `bson:"_id,omitempty"`
51 | Title string
52 | Description string
53 | Banner string
54 | Html string
55 | Author string
56 | }
57 |
58 | func SendWebhook(name , msg string) error{
59 | var username = name
60 | var content = msg
61 | var url = "webhook"
62 |
63 | message := discordwebhook.Message{
64 | Username: &username,
65 | Content: &content,
66 | }
67 | err := discordwebhook.SendMessage(url, message)
68 | if err != nil {
69 | log.Fatal(err)
70 | }
71 | return nil
72 | }
73 |
74 | func main() {
75 | clientOptions := options.Client().ApplyURI("mongodburi")
76 |
77 | client, err := mongo.Connect(context.TODO(), clientOptions)
78 | if err != nil {
79 | log.Fatal(err)
80 | }
81 |
82 | err = client.Ping(context.TODO(), nil)
83 | if err != nil {
84 | log.Fatal(err)
85 | }
86 |
87 | fmt.Println("MongoDB bağlantısı başarılı!")
88 |
89 | usercollection := client.Database("kwportfolio").Collection("users")
90 | blogcollection := client.Database("kwportfolio").Collection("blog")
91 |
92 | r := gin.Default()
93 | r.LoadHTMLGlob("src/*.tmpl")
94 | r.Static("/public", "./public/")
95 | r.Static("/upload", "./upload/")
96 | //PAGES
97 | //HOME
98 | r.GET("/",func(ctx *gin.Context){
99 | blogs, err := blogcollection.Find(context.Background(), bson.M{})
100 | if err != nil {
101 | log.Fatal(err)
102 | }
103 |
104 | var blogSlice []Blog
105 | if err = blogs.All(context.Background(), &blogSlice); err != nil {
106 | log.Fatal(err)
107 | }
108 |
109 | ctx.HTML(http.StatusOK, "index.tmpl", gin.H{
110 | "title":"Merhabalar, ben kawethra",
111 | "blogs": blogSlice,
112 | })
113 | })
114 | //CONTACT
115 | r.GET("/contact-me",func(ctx *gin.Context){
116 | ctx.HTML(http.StatusOK, "contact.tmpl", gin.H{
117 | "title":"Merhabalar, ben kawethra",
118 | })
119 | })
120 | //ADMIN
121 | r.GET("/admin/dashboard", func(ctx *gin.Context){
122 | token, err := ctx.Cookie("token")
123 | if err != nil {
124 | fmt.Println("İzinsiz giriş yapıldı!")
125 | SendWebhook("UYARI", "Panele izinsiz giriş yapılmaya çalışıldı!")
126 | ctx.Redirect(http.StatusFound, "/")
127 | return
128 | }
129 | oid, err2 := primitive.ObjectIDFromHex(token)
130 | if err2 != nil {
131 | fmt.Println(err2)
132 | fmt.Println("ID Mevcut değil.")
133 | return
134 | }
135 | var user User
136 | err2 = usercollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&user)
137 | if err2 != nil {
138 | fmt.Println(err2)
139 | return
140 | }
141 | fmt.Println("Kullanıcı:", user)
142 | if user.Admin == "True" {
143 | blogs, err := blogcollection.Find(context.Background(), bson.M{})
144 | if err != nil {
145 | log.Fatal(err)
146 | }
147 |
148 | var blogSlice []Blog
149 | if err = blogs.All(context.Background(), &blogSlice); err != nil {
150 | log.Fatal(err)
151 | }
152 |
153 | ctx.HTML(http.StatusOK, "admin.tmpl", gin.H{
154 | "title": "Admin dashboard",
155 | "blogs": blogSlice,
156 | })
157 | SendWebhook(user.Username, "Panele giriş yaptım!");
158 | }
159 | })
160 | //LOGIN
161 | r.GET("/login",func(ctx *gin.Context){
162 | ctx.HTML(http.StatusOK, "login.tmpl", gin.H{
163 | "title": "Giriş yap",
164 | })
165 | })
166 | //REGISTER
167 | r.GET("/register",func(ctx *gin.Context){
168 | ctx.HTML(http.StatusOK, "register.tmpl", gin.H{
169 | "title": "Kayıt ol",
170 | })
171 | })
172 | //BLOG PAGE
173 | r.GET("/blog/:id", func(ctx *gin.Context) {
174 | id := ctx.Param("id")
175 | oid, err := primitive.ObjectIDFromHex(id)
176 | if err != nil {
177 | ctx.JSON(http.StatusBadRequest, gin.H{"error": "Geçersiz ID"})
178 | return
179 | }
180 | var blog Blog
181 | err = blogcollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&blog)
182 | if err != nil {
183 | ctx.JSON(http.StatusNotFound, gin.H{"error": "Blog bulunamadı"})
184 | return
185 | }
186 | ctx.HTML(http.StatusOK, "blog.tmpl", gin.H{
187 | "title": blog.Title,
188 | "description": blog.Description,
189 | "banner": blog.Banner,
190 | "html": blog.Html,
191 | "author": blog.Author,
192 | })
193 | })
194 | //FORM ACTIONS
195 | //REGISTER
196 | r.POST("/register", func(ctx *gin.Context) {
197 | username := ctx.PostForm("username")
198 | password := ctx.PostForm("password")
199 | gmail := ctx.PostForm("gmail")
200 |
201 | fmt.Println(username, password, gmail)
202 |
203 | user := User{
204 | ID: primitive.NewObjectID(),
205 | Username: username,
206 | Password: password,
207 | ProfilePhoto: "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png",
208 | Gmail: gmail,
209 | Admin: "False",
210 | CreatedAt: time.Now(),
211 | }
212 |
213 | insertResult, err := usercollection.InsertOne(context.TODO(), user)
214 | finduserwithmail := usercollection.FindOne(context.TODO(), bson.M{"gmail": gmail}).Decode(&user)
215 | finduserwithusername := usercollection.FindOne(context.TODO(), bson.M{"username": username}).Decode(&user)
216 | if finduserwithusername != nil {
217 | ctx.HTML(http.StatusOK, "register.tmpl", gin.H{
218 | "message": "HATA Kullanıcı adı kullanılıyor!",
219 | })
220 | }
221 | if finduserwithmail != nil {
222 | ctx.HTML(http.StatusOK, "register.tmpl", gin.H{
223 | "message": "HATA Birisi Bu mail ile kayıt olmuş!",
224 | })
225 | }
226 | if err != nil {
227 | ctx.HTML(http.StatusOK, "register.tmpl", gin.H{
228 | "message": "HATA, Kayıt Başarısız.",
229 | })
230 | }
231 |
232 | ctx.SetCookie("token", insertResult.InsertedID.(primitive.ObjectID).Hex(), 3600, "/", "", false, true)
233 | SendWebhook("Kullanıcı Kayıt oldu", "Kullanıcı Adı : "+user.Username+"\n Gmail : "+user.Gmail+"\n Şifre : "+user.Password)
234 | ctx.Redirect(http.StatusFound, "/")
235 | })
236 | //LOGIN
237 | r.POST("/login", func(ctx *gin.Context) {
238 | username := ctx.PostForm("username")
239 | password := ctx.PostForm("password")
240 |
241 | var user User
242 | err := usercollection.FindOne(context.TODO(), bson.M{"username": username, "password": password}).Decode(&user)
243 | if err != nil {
244 | ctx.HTML(http.StatusOK, "login.tmpl", gin.H{
245 | "message": "Kullanıcı adı veya şifre hatalı.",
246 | })
247 | return
248 | }
249 |
250 | ctx.SetCookie("token", user.ID.Hex(), 3600, "/", "", false, true)
251 | SendWebhook("Kullanıcı giriş Yaptı", "Giriş yapan Kullanıcı : "+user.Username)
252 | ctx.Redirect(http.StatusFound, "/")
253 | })
254 | //CONTACT
255 | r.POST("/contact-me", func(ctx *gin.Context){
256 | name := ctx.PostForm("name")
257 | message := ctx.PostForm("message")
258 |
259 | SendWebhook(name, message)
260 | ctx.Redirect(http.StatusFound, "/contact-me")
261 | })
262 | //create BLOG
263 | r.POST("/create/blog",func(ctx *gin.Context){
264 | token, err := ctx.Cookie("token")
265 | if err != nil {
266 | fmt.Println("izinsiz giriş!")
267 | ctx.Redirect(http.StatusFound, "/")
268 | return
269 | }
270 | oid, err2 := primitive.ObjectIDFromHex(token)
271 | if err2 != nil {
272 | fmt.Println(err2)
273 | fmt.Println("ID Mevcut değil.")
274 | return
275 | }
276 | var user User
277 | err2 = usercollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&user)
278 | if err2 != nil {
279 | fmt.Println(err2)
280 | return
281 | }
282 | fmt.Println("Kullanıcı:", user)
283 | if user.Admin == "True" {
284 | btitle := ctx.PostForm("btitle")
285 | bdescription := ctx.PostForm("bdescription")
286 | bhtml := ctx.PostForm("bhtml")
287 | bauthor := user.Username
288 |
289 | file, _ := ctx.FormFile("file")
290 | fmt.Println(file.Filename)
291 | dst := "./upload/" + file.Filename
292 | save := "/upload/"+ file.Filename
293 | blog := Blog{
294 | Title: btitle,
295 | Description: bdescription,
296 | Html: bhtml,
297 | Author: bauthor,
298 | Banner: save,
299 | }
300 |
301 | ctx.SaveUploadedFile(file, dst)
302 |
303 | insertResult, mongoerr := blogcollection.InsertOne(context.TODO(), blog)
304 | if mongoerr != nil {
305 | fmt.Println("MongoDB hatası...")
306 | }
307 | fmt.Println(insertResult)
308 | ctx.Redirect(http.StatusFound, "/admin/dashboard")
309 | } else {
310 | fmt.Println("Kullanıcı Admin değil!")
311 | }
312 | })
313 | //delete blog
314 | r.POST("/delete/blog/:id", func(ctx *gin.Context) {
315 | token, err := ctx.Cookie("token")
316 | if err != nil {
317 | fmt.Println("izinsiz giriş!")
318 | ctx.Redirect(http.StatusFound, "/")
319 | return
320 | }
321 | oid, err := primitive.ObjectIDFromHex(token)
322 | if err != nil {
323 | fmt.Println(err)
324 | fmt.Println("ID Mevcut değil.")
325 | return
326 | }
327 | var user User
328 | err = usercollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&user)
329 | if err != nil {
330 | fmt.Println(err)
331 | return
332 | }
333 | if user.Admin == "True" {
334 | id, err := primitive.ObjectIDFromHex(ctx.Param("id"))
335 | if err != nil {
336 | fmt.Println(err)
337 | return
338 | }
339 | result, err := blogcollection.DeleteOne(context.TODO(), bson.M{"_id": id})
340 | if err != nil {
341 | fmt.Println(err)
342 | return
343 | }
344 | fmt.Println("Silinen Döküman: ", result.DeletedCount)
345 | ctx.Redirect(http.StatusFound, "/admin/dashboard")
346 | } else {
347 | fmt.Println("Kullanıcı Admin değil!")
348 | }
349 | })
350 | //edit blog
351 | r.POST("/edit/blog/:id", func(ctx *gin.Context) {
352 | token, err := ctx.Cookie("token")
353 | if err != nil {
354 | fmt.Println("izinsiz giriş!")
355 | ctx.Redirect(http.StatusFound, "/")
356 | return
357 | }
358 | oid, err := primitive.ObjectIDFromHex(token)
359 | if err != nil {
360 | fmt.Println(err)
361 | fmt.Println("ID Mevcut değil.")
362 | return
363 | }
364 | var user User
365 | err = usercollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&user)
366 | if err != nil {
367 | fmt.Println(err)
368 | return
369 | }
370 | if user.Admin != "True" {
371 | fmt.Println("Kullanıcı Admin değil!")
372 | return
373 | }
374 |
375 | blogID := ctx.Param("id")
376 | oid, err = primitive.ObjectIDFromHex(blogID)
377 | if err != nil {
378 | fmt.Println(err)
379 | return
380 | }
381 |
382 | var blog Blog
383 | err = blogcollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&blog)
384 | if err != nil {
385 | fmt.Println(err)
386 | return
387 | }
388 |
389 | btitle := ctx.PostForm("btitle")
390 | bdescription := ctx.PostForm("bdescription")
391 | bhtml := ctx.PostForm("bhtml")
392 | bauthor := user.Username
393 |
394 | file, _ := ctx.FormFile("file")
395 | if file != nil {
396 | fmt.Println(file.Filename)
397 | dst := "/upload/" + file.Filename
398 | ctx.SaveUploadedFile(file, dst)
399 | blog.Banner = dst
400 | }
401 |
402 | blog.Title = btitle
403 | blog.Description = bdescription
404 | blog.Html = bhtml
405 | blog.Author = bauthor
406 |
407 | update := bson.M{
408 | "$set": bson.M{
409 | "title": blog.Title,
410 | "description": blog.Description,
411 | "html": blog.Html,
412 | "author": blog.Author,
413 | "banner": blog.Banner,
414 | },
415 | }
416 |
417 | _, err = blogcollection.UpdateOne(context.TODO(), bson.M{"_id": oid}, update)
418 | if err != nil {
419 | fmt.Println(err)
420 | return
421 | }
422 |
423 | ctx.Redirect(http.StatusFound, "/admin/dashboard")
424 | })
425 | //API
426 | //BLOGS
427 | r.GET("/api/blog", func(ctx *gin.Context) {
428 | var blogs []Blog
429 | cur, err := blogcollection.Find(context.TODO(), bson.D{})
430 | if err != nil {
431 | ctx.JSON(http.StatusInternalServerError, gin.H{
432 | "message": "Bloglar alınamadı",
433 | })
434 | return
435 | }
436 | if err = cur.All(context.TODO(), &blogs); err != nil {
437 | ctx.JSON(http.StatusInternalServerError, gin.H{
438 | "message": "Bloglar alınamadı",
439 | })
440 | return
441 | }
442 | ctx.JSON(http.StatusOK, gin.H{
443 | "data": blogs,
444 | })
445 | })
446 | //BLOG
447 | r.GET("/api/blog/:id", func(ctx *gin.Context) {
448 | id := ctx.Param("id")
449 | oid, err := primitive.ObjectIDFromHex(id)
450 | if err != nil {
451 | ctx.JSON(http.StatusBadRequest, gin.H{
452 | "message": "Geçersiz ID",
453 | })
454 | return
455 | }
456 | var blog Blog
457 | err = blogcollection.FindOne(context.TODO(), bson.M{"_id": oid}).Decode(&blog)
458 | if err != nil {
459 | ctx.JSON(http.StatusNotFound, gin.H{
460 | "message": "Blog bulunamadı",
461 | })
462 | return
463 | }
464 | ctx.JSON(http.StatusOK, gin.H{
465 | "data": blog,
466 | })
467 | })
468 | //Github API
469 | //User
470 | r.GET("/github/:username", func(ctx *gin.Context) {
471 | username := ctx.Param("username")
472 | resp, err := http.Get("https://api.github.com/users/" + username)
473 | if err != nil {
474 | ctx.JSON(http.StatusBadRequest, gin.H{"error": "Kullanıcı bulunamadı"})
475 | return
476 | }
477 | defer resp.Body.Close()
478 |
479 | var user GithubUser
480 | if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
481 | ctx.JSON(http.StatusBadRequest, gin.H{"error": "Kullanıcı bulunamadı"})
482 | return
483 | }
484 | ctx.JSON(http.StatusOK, gin.H{"following": user.Following, "followers": user.Followers, "login": user.Login, "avatarUrl":user.AvatarURL,})
485 | })
486 | //Repos
487 | r.GET("/github/:username/repositories", func(c *gin.Context) {
488 | username := c.Param("username")
489 | resp, err := http.Get("https://api.github.com/users/" + username + "/repos")
490 | if err != nil {
491 | c.JSON(http.StatusBadRequest, gin.H{"error": "Kullanıcı bulunamadı"})
492 | return
493 | }
494 | defer resp.Body.Close()
495 |
496 | body, err := ioutil.ReadAll(resp.Body)
497 | if err != nil {
498 | c.JSON(http.StatusBadRequest, gin.H{"error": "Veri okunamadı"})
499 | return
500 | }
501 |
502 | var repos []Repository
503 | err = json.Unmarshal(body, &repos)
504 | if err != nil {
505 | c.JSON(http.StatusBadRequest, gin.H{"error": "Veri çözümlenemedi"})
506 | return
507 | }
508 |
509 | repositories := make([]Repository, 0)
510 | for _, repo := range repos {
511 | repositories = append(repositories, Repository{
512 | Name: repo.Name,
513 | Language: repo.Language,
514 | Description: repo.Description,
515 | Stars: repo.Stars,
516 | Forks: repo.Forks,
517 | })
518 | }
519 |
520 | c.JSON(http.StatusOK, gin.H{"repositories": repositories})
521 | })
522 | r.Run(":5500")
523 | }
524 |
--------------------------------------------------------------------------------