Graceful shutdown on CTRL-C
This commit is contained in:
parent
8f6aa65a10
commit
e583839bfe
1
db/db.go
1
db/db.go
|
@ -60,6 +60,7 @@ func Open(path string) (*DB, error) {
|
|||
}
|
||||
|
||||
func (db *DB) Close() error {
|
||||
log.Println("Closing database...")
|
||||
err := db.ctx.Close()
|
||||
Db = nil
|
||||
return err
|
||||
|
|
1
main.go
1
main.go
|
@ -55,4 +55,5 @@ func main() {
|
|||
fmt.Printf("Failed to start listen: ", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Returning from main...")
|
||||
}
|
||||
|
|
35
net/net.go
35
net/net.go
|
@ -2,7 +2,12 @@ package net
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.fuwafuwa.moe/x3/ngfshare/config"
|
||||
"git.fuwafuwa.moe/x3/ngfshare/controller"
|
||||
|
@ -10,7 +15,7 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func Start(conf config.Config) error {
|
||||
func setupRoutes() *mux.Router {
|
||||
r := mux.NewRouter()
|
||||
|
||||
authedR := r.PathPrefix("/api").Methods("POST").Subrouter()
|
||||
|
@ -26,11 +31,31 @@ func Start(conf config.Config) error {
|
|||
r.HandleFunc("/login", controller.WebLogin).Methods("POST")
|
||||
r.HandleFunc("/logout", controller.WebLogout).Methods("POST")
|
||||
|
||||
http.Handle("/", r)
|
||||
return r
|
||||
}
|
||||
|
||||
func Start(conf config.Config) error {
|
||||
|
||||
lstStr := fmt.Sprintf("%s:%d", conf.Address, conf.Port)
|
||||
fmt.Println("Listening on", lstStr)
|
||||
http.ListenAndServe(lstStr, nil)
|
||||
srv := &http.Server{
|
||||
Addr: lstStr,
|
||||
Handler: setupRoutes(),
|
||||
}
|
||||
|
||||
return nil
|
||||
done := make(chan os.Signal, 1)
|
||||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
||||
log.Printf("HTTP ListenAndServe error: %v\n", err)
|
||||
}
|
||||
}()
|
||||
log.Println("Listening on", lstStr)
|
||||
|
||||
<-done
|
||||
fmt.Println("")
|
||||
log.Println("Stopping HTTP server")
|
||||
|
||||
err := srv.Shutdown(context.Background())
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue