forked from sensible-contract/satotx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (57 loc) · 1.54 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"github.com/gin-contrib/cors"
"log"
"net/http"
"os"
"os/signal"
"satotx/controller"
"syscall"
"time"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)
var (
// 0.0.0.0:8000
listen_address = os.Getenv("LISTEN")
)
func main() {
router := gin.Default()
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "OPTIONS"},
AllowHeaders: []string{"Content-Type,Origin,Content-Encoding"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}))
router.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithDecompressFn(gzip.DefaultDecompressHandle)))
router.GET("/", controller.Satotx)
router.POST("/utxo/:txid/:index", controller.SignUtxo)
router.POST("/utxo-spend-by/:txid/:index/:byTxid", controller.SignUtxoSpendBy)
router.POST("/utxo-spend-by-utxo/:txid/:index/:byTxid/:byTxindex", controller.SignUtxoSpendByUtxo)
log.Printf("LISTEN: %s", listen_address)
svr := &http.Server{
Addr: listen_address,
Handler: router,
}
go func() {
err := svr.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalln(err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
timeout := time.Duration(5) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := svr.Shutdown(ctx); err != nil {
log.Fatalln(err)
}
}