2018-11-04 19:56:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-29 12:14:41 +00:00
|
|
|
"context"
|
2018-11-04 19:56:06 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-04-05 11:07:19 +00:00
|
|
|
"os"
|
2020-01-29 12:14:41 +00:00
|
|
|
"os/signal"
|
2018-11-04 19:56:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-01-29 12:14:41 +00:00
|
|
|
"github.com/gusaul/grpcox/core"
|
2018-11-04 19:56:06 +00:00
|
|
|
"github.com/gusaul/grpcox/handler"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-04-05 11:07:19 +00:00
|
|
|
// logging conf
|
|
|
|
f, err := os.OpenFile("log/grpcox.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error opening file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
|
|
|
|
// start app
|
2021-02-05 19:26:15 +00:00
|
|
|
addr := "0.0.0.0:6969"
|
|
|
|
if value, ok := os.LookupEnv("BIND_ADDR"); ok {
|
|
|
|
addr = value
|
|
|
|
}
|
2018-11-04 19:56:06 +00:00
|
|
|
muxRouter := mux.NewRouter()
|
|
|
|
handler.Init(muxRouter)
|
2020-01-29 12:14:41 +00:00
|
|
|
var wait time.Duration = time.Second * 15
|
|
|
|
|
2018-11-04 19:56:06 +00:00
|
|
|
srv := &http.Server{
|
2021-02-05 19:26:15 +00:00
|
|
|
Addr: addr,
|
2020-01-29 12:14:41 +00:00
|
|
|
WriteTimeout: time.Second * 15,
|
|
|
|
ReadTimeout: time.Second * 15,
|
|
|
|
IdleTimeout: time.Second * 60,
|
|
|
|
Handler: muxRouter,
|
2018-11-04 19:56:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 19:26:15 +00:00
|
|
|
fmt.Println("Service started on", addr)
|
2020-01-29 12:14:41 +00:00
|
|
|
go func() {
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
|
|
|
|
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
// Block until we receive our signal.
|
|
|
|
<-c
|
|
|
|
|
|
|
|
// Create a deadline to wait for.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), wait)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = removeProtos()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while removing protos: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
log.Println("shutting down")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeProtos will remove all uploaded proto file
|
|
|
|
// this function will be called as the server shutdown gracefully
|
|
|
|
func removeProtos() error {
|
|
|
|
log.Println("removing proto dir from /tmp")
|
|
|
|
err := os.RemoveAll(core.BasePath)
|
|
|
|
return err
|
2018-11-04 19:56:06 +00:00
|
|
|
}
|