-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
44 lines (36 loc) · 1.01 KB
/
server.go
File metadata and controls
44 lines (36 loc) · 1.01 KB
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
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/paulofelipefeitosa/distributed-union-find/config"
"github.com/paulofelipefeitosa/distributed-union-find/master"
"log"
"net/http"
)
type Component interface {
Build(appConfig config.AppConfig)
GetURL() config.URL
GrabFuncHandler() http.HandlerFunc
InfoFuncHandler() http.HandlerFunc
}
func Serve(conf config.AppConfig) {
var component Component
if conf.IsInitiator() {
log.Print("Server is a Master App\n")
component = &master.ServerMaster{}
component.Build(conf)
} else {
// TODO:
log.Print("Server is a Slave App\n")
}
router := mux.NewRouter()
router.HandleFunc("/grab", component.GrabFuncHandler()).Methods(http.MethodPost)
router.HandleFunc("/info", component.InfoFuncHandler()).Methods(http.MethodGet)
serverURL := component.GetURL()
log.Printf("Server URL %+v\n", serverURL)
s := &http.Server{
Addr: fmt.Sprintf("%s:%d", serverURL.IP, serverURL.Port),
Handler: router,
}
log.Fatal(s.ListenAndServe())
}