-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathserver.go
55 lines (51 loc) · 1.35 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
package sso
import (
"fmt"
"github.com/viant/toolbox"
"log"
"net/http"
)
// Server represents http server
type Server struct {
service Service
config *Config
serviceRouter *toolbox.ServiceRouter
}
// Start start server
func (s *Server) Start() {
http.HandleFunc("/api/", func(writer http.ResponseWriter, reader *http.Request) {
err := s.serviceRouter.Route(writer, reader)
if err != nil {
http.Error(writer, fmt.Sprintf("%v", err), http.StatusInternalServerError)
return
}
})
for _, route := range s.config.StaticRoutes {
fileServer := http.FileServer(http.Dir(route.Directory))
http.Handle(route.URI, fileServer)
}
fmt.Printf("Started test server on port %v\n", s.config.Port)
log.Fatal(http.ListenAndServe(":"+s.config.Port, nil))
}
// NewServer creates a new http server
func NewServer(config *Config, service Service) (*Server, error) {
serviceRouter := toolbox.NewServiceRouter(
toolbox.ServiceRouting{
HTTPMethod: "POST",
URI: "/api/singup/",
Handler: service.SignUp,
Parameters: []string{"request", "@httpRequest"},
},
toolbox.ServiceRouting{
HTTPMethod: "POST",
URI: "/api/signin/",
Handler: service.SignIn,
Parameters: []string{"request"},
})
var result = &Server{
serviceRouter: serviceRouter,
service: service,
config: config,
}
return result, nil
}