-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
93 lines (80 loc) · 2.86 KB
/
server.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
package main
import (
"errors"
"html/template"
"io"
"os"
"github.com/habibitcoin/habibtaro/handler"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/gommon/log"
)
// Implement e.Renderer interface
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tmpl, ok := t.templates[name]
if !ok {
err := errors.New("Template not found -> " + name)
return err
}
return tmpl.ExecuteTemplate(w, "base.html", data)
}
// Define the template registry struct
type TemplateRegistry struct {
templates map[string]*template.Template
}
func main() {
e := echo.New()
templates := make(map[string]*template.Template)
templates["index.html"] = template.Must(template.ParseFiles("templates/index.html", "templates/base.html"))
templates["signup.html"] = template.Must(template.ParseFiles("templates/signup.html", "templates/base.html"))
templates["address.html"] = template.Must(template.ParseFiles("templates/address.html", "templates/base.html"))
templates["asset.html"] = template.Must(template.ParseFiles("templates/asset.html", "templates/base.html"))
templates["proof.html"] = template.Must(template.ParseFiles("templates/proof.html", "templates/base.html"))
templates["user.html"] = template.Must(template.ParseFiles("templates/user.html", "templates/base.html"))
templates["login.html"] = template.Must(template.ParseFiles("templates/login.html", "templates/base.html"))
templates["dashboard.html"] = template.Must(template.ParseFiles("templates/dashboard.html", "templates/base.html"))
templates["post.html"] = template.Must(template.ParseFiles("templates/post.html", "templates/base.html"))
e.Renderer = &TemplateRegistry{
templates: templates,
}
e.Logger.SetLevel(log.ERROR)
e.Use(middleware.Logger())
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
TokenLookup: "cookie:" + echo.HeaderAuthorization,
SigningKey: []byte(handler.Key),
Skipper: func(c echo.Context) bool {
// Skip authentication for requests
if c.Path() != "/dashboard" {
return true
}
return false
},
}))
// Initialize handler
h := &handler.Handler{}
// Routes
e.GET("/", h.Index)
e.GET("/address", h.ViewAddress)
e.GET("/asset/:name", h.FetchAsset)
e.GET("/asset/:name/proof", h.FetchAssetProof)
e.GET("/dashboard", h.Dashboard)
e.GET("/list", h.ListUsers)
e.GET("/:username", h.FetchUser)
e.GET("/signup", h.ViewSignup)
e.GET("/login", h.ViewLogin)
e.PUT("/dashboard", h.UpdateUser)
e.POST("/address", h.Address)
e.POST("/post", h.CheckPost)
e.POST("/signup", h.Signup)
e.POST("/login", h.Login)
e.POST("/posts", h.CreatePost)
e.POST("/dashboard", h.CreateWithdrawal)
e.Static("/static", "static")
e.File("/favicon.ico", "static/images/favicon.ico")
// Start server
port := "1323"
if value, ok := os.LookupEnv("PORT"); ok {
port = value
}
e.Logger.Fatal(e.Start(":" + port))
}