forked from CHESSComputing/CHAPaaS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
132 lines (116 loc) · 3.72 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
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
package main
import (
"crypto/tls"
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"strings"
"github.com/uptrace/bunrouter"
gologin "github.com/dghubble/gologin/v2"
"github.com/dghubble/gologin/v2/github"
"golang.org/x/oauth2"
githubOAuth2 "golang.org/x/oauth2/github"
)
// content is our static web server content.
//go:embed static
var StaticFs embed.FS
// The OAuth parts are based on
// https://github.com/dghubble/gologin
// package where we explid github authentication, see
// https://github.com/dghubble/gologin/blob/main/examples/github
// helper function to get base path
func basePath(s string) string {
if Config.Base != "" {
if strings.HasPrefix(s, "/") {
s = strings.Replace(s, "/", "", 1)
}
if strings.HasPrefix(Config.Base, "/") {
return fmt.Sprintf("%s/%s", Config.Base, s)
}
return fmt.Sprintf("/%s/%s", Config.Base, s)
}
return s
}
// bunrouter implementation of the compatible (with net/http) router handlers
func bunRouter() *bunrouter.CompatRouter {
router := bunrouter.New(
bunrouter.Use(bunrouterLoggingMiddleware),
bunrouter.Use(bunrouterLimitMiddleware),
).Compat()
base := Config.Base
router.GET(base+"/", IndexHandler)
router.GET(base+"/favicon.ico", FaviconHandler)
// server routes
router.GET(base+"/docs", DocsHandler)
router.GET(base+"/login", LoginHandler)
router.GET(base+"/access", AccessHandler)
router.GET(base+"/publish", PublishHandler)
router.GET(base+"/notebook", NotebookHandler)
router.GET(base+"/workflows", WorkflowsHandler)
// chap routes
router.GET(base+"/chap/run", ChapRunHandler)
router.GET(base+"/chap/profile", ChapProfileHandler)
// auth end-points
// github OAuth routes
var arec OAuthRecord
var err error
arec, err = Config.Credentials("github")
if err != nil {
log.Println("WARNING:", err)
}
config := &oauth2.Config{
ClientID: arec.ClientID,
ClientSecret: arec.ClientSecret,
RedirectURL: fmt.Sprintf("http://localhost:%d%s/github/callback", Config.Port, Config.Base),
Endpoint: githubOAuth2.Endpoint,
}
stateConfig := gologin.DebugOnlyCookieConfig
githubLogin := gologinHandler(config, github.StateHandler(stateConfig, github.LoginHandler(config, nil)))
githubCallback := gologinHandler(config, github.StateHandler(
stateConfig,
github.CallbackHandler(config, issueSession("github"), nil),
))
router.Router.GET(base+"/github/login", bunrouter.HTTPHandler(githubLogin))
router.Router.GET(base+"/github/callback", bunrouter.HTTPHandler(githubCallback))
// static handlers
for _, dir := range []string{"js", "css", "images"} {
filesFS, err := fs.Sub(StaticFs, "static/"+dir)
if err != nil {
panic(err)
}
m := fmt.Sprintf("%s/%s", Config.Base, dir)
fileServer := http.FileServer(http.FS(filesFS))
hdlr := http.StripPrefix(m, fileServer)
router.Router.GET(m+"/*path", bunrouter.HTTPHandler(hdlr))
}
return router
}
// Server implements MLaaS server
func Server() {
// initialize server middleware
initLimiter(Config.LimiterPeriod)
// setup server router
router := bunRouter()
// start HTTPs server
if len(Config.DomainNames) > 0 {
server := LetsEncryptServer(Config.DomainNames...)
log.Println("Start HTTPs server with LetsEncrypt", Config.DomainNames)
log.Fatal(server.ListenAndServeTLS("", ""))
} else if Config.ServerCrt != "" && Config.ServerKey != "" {
tlsConfig := &tls.Config{
RootCAs: RootCAs(),
}
server := &http.Server{
Addr: ":https",
TLSConfig: tlsConfig,
Handler: router,
}
log.Printf("Start HTTPs server with %s and %s on :%d", Config.ServerCrt, Config.ServerKey, Config.Port)
log.Fatal(server.ListenAndServeTLS(Config.ServerCrt, Config.ServerKey))
} else {
log.Printf("Start HTTP server on :%d", Config.Port)
http.ListenAndServe(fmt.Sprintf(":%d", Config.Port), router)
}
}