Added signal handling

master
Aadhavan Srinivasan 5 days ago
parent 3d06971e77
commit 3d0c20ae48

@ -7,6 +7,9 @@ import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
@ -72,6 +75,13 @@ var lang_codes []string = []string{
var db *sqlx.DB
func cleanup() {
db.Close()
log.Printf("Shutting down...\n")
return
}
/*
Returns the cached translation from the database, for the given english text. The first parameter
@ -168,13 +178,28 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
func main() {
fmt.Printf("Starting server...")
log.Printf("Starting server...")
var err error
db, err = sqlx.Connect("sqlite3", "../translations.db")
if err != nil {
panic(err)
}
defer db.Close()
// Catch signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs)
go func() {
for sig := range sigs {
log.Printf("Received signal: %s", sig)
switch sig {
case syscall.SIGURG:
log.Printf("Ignoring sigurg")
case syscall.SIGTERM, syscall.SIGINT:
cleanup()
os.Exit(1)
}
}
}()
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":9090", nil))

Loading…
Cancel
Save