-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (139 loc) · 3.34 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"embed"
"flag"
"io/fs"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"time"
"github.com/yaitoo/xun"
"github.com/yaitoo/xun/ext/htmx"
)
//go:embed app/components
//go:embed app/public
//go:embed app/layouts
//go:embed app/pages
//go:embed app/text
var fsys embed.FS
func main() {
var dev bool
flag.BoolVar(&dev, "dev", false, "it is development environment")
flag.Parse()
var opts []xun.Option
if dev {
// use local filesystem in development, and watch files to reload automatically
opts = []xun.Option{xun.WithFsys(os.DirFS("./app")), xun.WithWatch()}
} else {
// use embed resources in production environment
views, _ := fs.Sub(fsys, "app")
opts = []xun.Option{xun.WithFsys(views)}
}
opts = append(opts, xun.WithInterceptor(htmx.New()))
app := xun.New(opts...)
app.Use(func(next xun.HandleFunc) xun.HandleFunc {
return func(c *xun.Context) error {
n := time.Now()
defer func() {
duration := time.Since(n)
log.Println(c.Routing.Pattern, duration)
}()
return next(c)
}
})
app.Get("/{$}", func(c *xun.Context) error {
return c.View(map[string]string{
"Name": "go-xun",
})
})
app.Get("/user/{id}", func(c *xun.Context) error {
id := c.Request().PathValue("id")
user := getUserById(id)
return c.View(user)
})
app.Get("/sitemap.xml", func(c *xun.Context) error {
return c.View(struct {
LastMod time.Time
}{
LastMod: time.Now(),
}, "text/sitemap.xml")
})
admin := app.Group("/admin")
admin.Use(func(next xun.HandleFunc) xun.HandleFunc {
return func(c *xun.Context) error {
s, err := c.Request().Cookie("session")
if err != nil || s == nil || s.Value == "" {
c.Redirect("/login?return=" + c.Request().URL.String())
return xun.ErrCancelled
}
c.Set("session", s.Value)
return next(c)
}
})
admin.Get("/{$}", func(c *xun.Context) error {
return c.View(User{
Name: c.Get("session").(string),
})
})
app.Post("/login", func(c *xun.Context) error {
it, err := xun.BindForm[Login](c.Request())
if err != nil {
c.WriteStatus(http.StatusBadRequest)
return xun.ErrCancelled
}
if !it.Validate(c.AcceptLanguage()...) {
c.WriteStatus(http.StatusBadRequest)
return c.View(it)
}
if it.Data.Email != "[email protected]" || it.Data.Password != "123" {
htmx.WriteHeader(c, htmx.HxTrigger, htmx.HxHeader[string]{
"showMessage": "Email or password is incorrect",
})
c.WriteStatus(http.StatusBadRequest)
return c.View(it)
}
cookie := http.Cookie{
Name: "session",
Value: it.Data.Email,
Path: "/",
MaxAge: 3600,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
}
http.SetCookie(c.Writer(), &cookie)
ref, _ := url.Parse(c.RequestReferer())
c.Redirect(ref.Query().Get("return"))
return nil
})
app.Start()
defer app.Close()
if dev {
slog.Default().Info("xun-admin is running in development")
} else {
slog.Default().Info("xun-admin is running in production")
}
err := http.ListenAndServe(":80", http.DefaultServeMux)
if err != nil {
panic(err)
}
}
func getUserById(id string) User {
return User{
ID: id,
Name: "Yaitoo",
}
}
func checkToken(token string) bool {
return true
}
type Login struct {
Email string `form:"email" validate:"required,email"`
Password string `form:"password" validate:"required"`
}
type User struct {
ID string
Name string
}