-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
50 lines (43 loc) · 1.36 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
package resource
import (
"embed"
_ "embed"
"fmt"
"github.com/gorilla/mux"
"github.com/initialcapacity/oauth-starter/pkg/healthsupport"
"github.com/initialcapacity/oauth-starter/pkg/metricssupport"
"github.com/initialcapacity/oauth-starter/pkg/static"
"github.com/initialcapacity/oauth-starter/pkg/websupport"
"io/fs"
"net"
"net/http"
"os"
)
var (
//go:embed resources
Resources embed.FS
)
type App struct {
}
func (a App) LoadHandlers() func(x *mux.Router) {
return func(router *mux.Router) {
router.HandleFunc("/", a.dashboard).Methods("GET")
router.HandleFunc("/health", healthsupport.HandlerFunction)
router.HandleFunc("/metrics", metricssupport.HandlerFunction)
router.Use(metricssupport.Middleware)
s, _ := fs.Sub(static.Resources, "shared/static")
fileServer := http.FileServer(http.FS(s))
router.PathPrefix("/").Handler(http.StripPrefix("/", fileServer))
}
}
func (a *App) dashboard(writer http.ResponseWriter, _ *http.Request) {
_ = websupport.ModelAndView(writer, &Resources, "index", websupport.Model{Map: map[string]any{}})
}
func NewApp(addr string) (*http.Server, net.Listener) {
if found := os.Getenv("PORT"); found != "" {
host, _, _ := net.SplitHostPort(addr)
addr = fmt.Sprintf("%v:%v", host, found)
}
listener, _ := net.Listen("tcp", addr)
return websupport.Create(listener.Addr().String(), App{}.LoadHandlers()), listener
}