-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
70 lines (58 loc) · 1.28 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
package main
import (
"context"
"embed"
"net/http"
"path/filepath"
"runtime"
"github.com/go-chi/chi"
"go-micro.dev/v4"
"github.com/owncloud/ocis/v2/ocis-pkg/assetsfs"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
ohttp "github.com/owncloud/ocis/v2/ocis-pkg/service/http"
)
//go:embed ui/dist/*
var assets embed.FS
const serviceName = "arcade"
const httpRoot = "/sample/" + serviceName
func main() {
logger := log.NewLogger()
ctx := context.Background()
service, _ := ohttp.NewService(
ohttp.Name(serviceName),
ohttp.Namespace("com.owncloud.sample"),
ohttp.Address("127.0.0.1:0"),
ohttp.Context(ctx),
)
_, filename, _, ok := runtime.Caller(0)
if !ok {
logger.Info().Msg("failed to obtain root")
}
if err := micro.RegisterHandler(service.Server(), Service{
log: logger,
root: filepath.Dir(filename),
}); err != nil {
logger.Fatal().Err(err)
}
if err := service.Run(); err != nil {
logger.Fatal().Err(err)
}
}
type Service struct {
root string
log log.Logger
}
func (s Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux := chi.NewMux()
mux.Route(httpRoot, func(r chi.Router) {
r.Get(
"/ui/*",
http.StripPrefix(
httpRoot,
http.FileServer(
assetsfs.New(assets, s.root, s.log)),
).ServeHTTP,
)
})
mux.ServeHTTP(w, r)
}