Created
          November 12, 2018 08:35 
        
      - 
      
- 
        Save syfun/f75908989b0fd2d96825a08d0b844159 to your computer and use it in GitHub Desktop. 
    gin-solf-shutdown.go
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package http | |
| import ( | |
| "context" | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "os" | |
| "os/signal" | |
| "time" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| func Run(addr string) { | |
| router := gin.Default() | |
| // Set a lower memory limit for multipart forms (default is 32 MiB) | |
| // router.MaxMultipartMemory = 8 << 20 // 8 MiB | |
| router.POST("/upload", func(c *gin.Context) { | |
| // single file | |
| file, _ := c.FormFile("file") | |
| log.Println(file.Filename) | |
| // Upload the file to specific dst. | |
| c.SaveUploadedFile(file, file.Filename) | |
| c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) | |
| }) | |
| s := &http.Server{Addr: addr, Handler: router} | |
| go func() { | |
| if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { | |
| log.Fatalf("listen: %s\n", err) | |
| } | |
| }() | |
| defer func() { | |
| s.Shutdown(context.Background()) | |
| time.Sleep(time.Microsecond * 500) | |
| }() | |
| interrupt := make(chan os.Signal) | |
| signal.Notify(interrupt, os.Interrupt) | |
| <-interrupt | |
| log.Println("Server exiting") | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment