-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (88 loc) · 2.42 KB
/
main.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
101
102
103
104
105
package main
import (
"fmt"
"go-htmx/internal/auth"
"go-htmx/internal/db"
"go-htmx/internal/endpoints"
"go-htmx/internal/user"
"html/template"
"log"
"os"
// trunk-ignore(trufflehog/SQLServer)
"strconv"
echojwt "github.com/labstack/echo-jwt/v4"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func GetDBcredentials() string {
var (
db_host string = os.Getenv("POSTGRES_HOST")
db_port, err = strconv.Atoi(os.Getenv("POSTGRES_PORT"))
db_user string = os.Getenv("POSTGRES_USER")
db_password string = os.Getenv("POSTGRES_PASSWORD")
db_name string = os.Getenv("POSTGRES_DB")
)
if err != nil {
log.Fatalf("Could not determine DB URL: %+v", err)
}
return fmt.Sprintf("host=%s port=%d user=%s "+
// trunk-ignore(trufflehog/SQLServer)
"password=%s dbname=%s sslmode=disable",
db_host, db_port, db_user, db_password, db_name)
}
func main() {
db_url := os.Getenv("DB_URL")
if db_url == "" {
db_url = GetDBcredentials()
}
err := db.NewDatabase(db_url)
if err != nil {
log.Fatalf("Could not init db: %+v", err)
}
u, _ := db.GetUser("test")
if u.Username == "" {
err = db.AddUser(user.LoadTestUser())
if err != nil {
log.Fatalf("Could not add user: %+v", err)
}
}
print(u.Username)
tmpl, err := template.ParseFiles(
"./web/public/login.html",
"./web/public/header.html",
"./web/public/nav.html",
"./web/public/home.html",
"./web/public/help.html",
"./web/public/settings.html",
"./web/public/usermng.html",
"./web/public/signup.html",
)
if err != nil {
log.Fatalf("Could not initialise templates: %+v", err)
}
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Renderer = endpoints.NewTemplateRenderer(tmpl)
e.GET("/", endpoints.HandleIndex)
e.GET("/css/style.css", func(c echo.Context) error {
return c.File("./web/public/css/style.css")
})
e.GET("/login", endpoints.HandleLoginForm)
e.GET("/signup", endpoints.HandleSignup)
e.POST("/login", endpoints.Login)
e.GET("/logout", endpoints.Logout)
app := e.Group("/app")
{
app.Use(echojwt.WithConfig(echojwt.Config{
NewClaimsFunc: auth.Claim,
SigningKey: []byte(auth.GetJWTSecret()),
TokenLookup: "cookie:access-token",
ErrorHandler: auth.JWTErrorChecker,
}))
app.GET("/home", endpoints.HandleHome)
app.GET("/settings", endpoints.HandleSettings)
app.GET("/help", endpoints.HandleHelp)
}
e.Logger.Fatal(e.Start("0.0.0.0:8080"))
}