-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.go
101 lines (84 loc) · 2.97 KB
/
routing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"database/sql"
"net/http"
"sort"
"github.com/Euvaz/go-log"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
func createMyRender() multitemplate.Renderer {
renderer := multitemplate.NewRenderer()
renderer.AddFromFiles("home", "website/tmpl/layout.tmpl", "website/tmpl/home.tmpl")
renderer.AddFromFiles("projects", "website/tmpl/layout.tmpl", "website/tmpl/projects.tmpl")
renderer.AddFromFiles("contact", "website/tmpl/layout.tmpl", "website/tmpl/contact.tmpl")
return renderer
}
func registerRoutes (router *gin.Engine, db *sql.DB) {
router.HTMLRender = createMyRender()
EMAIL := "6f4baa76-cf90-4803-bc66-19838566253e@simplelogin.com"
GITHUB_USER := "Euvaz"
LINKEDIN_USER := "VirgilLopez"
router.GET("/", func(ctx *gin.Context) {
logger.Info("Handling GET /")
data := gin.H {
"title": "Virgil Lopez",
"tagline": "Aspiring DevOps Engineer.",
"firstname": "Virgil",
"lastname": "Lopez",
"linkedin_user": LINKEDIN_USER,
}
ctx.HTML(http.StatusOK, "home", data)
})
router.GET("/projects", func(ctx *gin.Context) {
logger.Info("Handling GET /projects")
type GitHubRepository struct {
ID int
Owner string
Name string
Preview string
Stargazers_count int
}
rows, err := db.Query("SELECT id, owner, name, preview, stargazers_count FROM github_repositories")
if err != nil {
logger.Fatal(err.Error())
}
defer rows.Close()
github_repos := []GitHubRepository{}
for rows.Next() {
var repo GitHubRepository
err = rows.Scan(&repo.ID, &repo.Owner, &repo.Name, &repo.Preview, &repo.Stargazers_count)
if err != nil {
logger.Fatal(err.Error())
}
github_repos = append(github_repos, repo)
}
if err = rows.Err(); err != nil {
logger.Fatal(err.Error())
}
sort.Slice(github_repos, func(i, j int) bool {
return github_repos[i].Stargazers_count > github_repos[j].Stargazers_count
})
data := gin.H {
"title": "Virgil Lopez",
"tagline": "Projects",
"firstname": "Virgil",
"lastname": "Lopez",
"github_repos": github_repos,
}
ctx.HTML(http.StatusOK, "projects", data)
})
router.GET("/contact", func(ctx *gin.Context) {
logger.Info("Handling GET /contact")
data := gin.H {
"title": "Virgil Lopez",
"tagline": "Contact Me",
"firstname": "Virgil",
"lastname": "Lopez",
"email": EMAIL,
"github_user": GITHUB_USER,
"linkedin_user": LINKEDIN_USER,
}
ctx.HTML(http.StatusOK, "contact", data)
})
}