github.com/yusys-cloud/go-jsonstore-rest@v0.0.0-20230228115429-0a54aa4a27a6/main.go (about)

     1  // Author: yangzq80@gmail.com
     2  // Date: 2021-02-02
     3  //
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"github.com/gin-gonic/gin"
     9  	"github.com/yusys-cloud/go-jsonstore-rest/rest"
    10  	"net/http"
    11  )
    12  
    13  func main() {
    14  
    15  	path := flag.String("path", "./json-db", "--path=./json-db")
    16  
    17  	port := flag.String("port", "9999", "--port=9999")
    18  
    19  	flag.Parse()
    20  
    21  	r := gin.Default()
    22  
    23  	r.Use(DisableCors())
    24  
    25  	rest.NewJsonStoreRest(*path, r)
    26  
    27  	r.Run(":" + *port)
    28  }
    29  
    30  //Needed in order to disable CORS for local development
    31  func DisableCors() gin.HandlerFunc {
    32  	return func(c *gin.Context) {
    33  		method := c.Request.Method
    34  		c.Header("Access-Control-Allow-Origin", "*")
    35  		c.Header("Access-Control-Allow-Methods", "*")
    36  		c.Header("Access-Control-Allow-Headers", "*")
    37  
    38  		if method == "OPTIONS" {
    39  			c.AbortWithStatus(http.StatusNoContent)
    40  		}
    41  		c.Next()
    42  	}
    43  }