-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserv.go
56 lines (44 loc) · 1.4 KB
/
webserv.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
package main
// Simple static server of Angular compiled dist/project folder.
// A bit improved version.
import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"webserv/common"
"webserv/params"
"webserv/treedata"
)
func main() {
// Initialize database connection
pgx := common.Pgx{}
pgx.Init()
// Init params then serve static folder
args := params.OsArgs(os.Args)
http.Handle("/", http.FileServer(http.Dir(args.StaticDir())))
// Init treedata then serve its controllers & pass db connection
tdata := treedata.TData(&pgx)
http.HandleFunc("/api/postjsonstring", tdata.PostJsonData)
// Catch the Ctrl-C and SIGTERM from kill command
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
signalType := <-ch
signal.Stop(ch)
log.Println("Exit command received. Exiting...")
log.Println("Terminate signal type: ", signalType)
//*********************************************
// Note: here call your app Close() method to
// properly close resources before exiting.
pgx.Con.Close()
log.Println("Postgresql connection closed.")
//*********************************************
os.Exit(0)
}()
log.Printf("\nServing static folder: %s\nListening on port: %s\nPress Ctrl-C to stop server\n", args.StaticDir(), args.Port())
if err := http.ListenAndServe(args.Port(), nil); err != nil {
log.Fatal("Http server fatal panic error: ", err)
}
}