-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverstartup.go
More file actions
59 lines (47 loc) · 1.3 KB
/
serverstartup.go
File metadata and controls
59 lines (47 loc) · 1.3 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/rjtokenring/goms/dbaccess"
"github.com/rjtokenring/goms/serverstub"
"github.com/rjtokenring/goms/stringstxt"
"net/http"
"os"
"os/signal"
"syscall"
)
var serverBinding = ":1323"
var version = "1.0"
func main() {
handlers()
//Init DB before starting web server
dbaccess.InitDb()
e := echo.New()
initGetHandler(e)
e.Logger.Fatal(e.Start(serverBinding))
}
func handlers() {
var signals = make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go shutdownSignalHandlers(signals)
}
func initGetHandler(e *echo.Echo) {
//Base
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Go MS test - version: "+version)
})
e.GET("/txt/reverse/:txtr", func(c echo.Context) error {
var txtToReverse = c.Param("txtr")
txtToReverse = "This is the reversed txt: " + stringstxt.ReverseRunes(txtToReverse)
return c.String(http.StatusOK, txtToReverse)
})
//Server genearted stubs with OpenApi spec
var implementingStubs = serverstub.GoMsServerImpl{}
serverstub.RegisterHandlers(e, &implementingStubs)
}
func shutdownSignalHandlers(signals <-chan os.Signal) {
sig := <-signals
log.Info("Shutting down server.... " + sig.String())
dbaccess.Close()
os.Exit(0)
}