-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamis.go
112 lines (94 loc) · 2.88 KB
/
amis.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
// Package amisgo provides a web framework for building amis applications
package amisgo
import (
"context"
"net/http"
"strings"
"github.com/zrcoder/amisgo/conf"
"github.com/zrcoder/amisgo/util"
)
// App represents the web application
type App struct {
Conf *conf.Config
mux *http.ServeMux
server *http.Server
}
// New creates an Engine instance with options
func New(opts ...conf.Option) *App {
cfg := conf.Default()
cfg.Apply(opts...)
app := &App{
Conf: cfg,
mux: http.NewServeMux(),
}
if cfg.LocalSdkFS != nil {
app.StaticFS("/__amisgo__sdk/", cfg.LocalSdkFS)
}
return app
}
// Mount registers an amis component at the given path
func (a *App) Mount(path string, component any, middlewares ...func(http.Handler) http.Handler) {
var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.renderComponent(w, component)
})
h = util.WithMiddlewares(h, middlewares...)
a.mux.Handle(path, h)
}
// Redirect sets up a URL redirection
func (a *App) Redirect(src, dst string, code int) {
a.mux.HandleFunc(src, func(w http.ResponseWriter, r *http.Request) {
util.Redirect(w, r, dst, code)
})
}
// Handle registers a handler at the given path
func (a *App) Handle(path string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) {
a.mux.Handle(path, util.WithMiddlewares(handler, middlewares...))
}
// HandleFunc registers a handler function at the given path
func (a *App) HandleFunc(path string, handler http.HandlerFunc, middlewares ...func(http.Handler) http.Handler) {
a.Handle(path, handler, middlewares...)
}
// StaticFS registers a file server for serving static files
func (a *App) StaticFS(prefix string, fs http.FileSystem) {
if prefix == "" || prefix == "/" {
prefix = "/"
}
prefix = "/" + strings.TrimLeft(prefix, "/")
prefix = strings.TrimRight(prefix, "/") + "/"
a.mux.Handle(prefix, http.StripPrefix(prefix, http.FileServer(fs)))
}
// StaticFiles registers a file server for serving static files
func (a *App) StaticFiles(prefix string, root string) {
a.StaticFS(prefix, http.Dir(root))
}
// Run starts the HTTP server
func (a *App) Run(addr ...string) error {
address := ":80"
if len(addr) > 0 && addr[0] != "" {
address = addr[0]
}
a.server = &http.Server{Addr: address, Handler: a}
return a.server.ListenAndServe()
}
// Shutdown gracefully shuts down the server without interrupting any
// active connections.
func (a *App) Shutdown(ctx context.Context) error {
return a.server.Shutdown(ctx)
}
// ServeHTTP implements http.Handler interface
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.mux.ServeHTTP(w, r)
}
// renderComponent renders an amis component
func (a *App) renderComponent(w http.ResponseWriter, component any) {
data := struct {
*conf.Config
AmisJson any
}{
Config: a.Conf,
AmisJson: component,
}
if err := a.Conf.AmisTempl.Execute(w, data); err != nil {
panic(err)
}
}