Created
March 10, 2022 13:47
-
-
Save romanitalian/d1b5da21d3add5a65de6f009ad5d4991 to your computer and use it in GitHub Desktop.
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
// lout | |
log.Debugf("-------- $VAR$: %#v", $VAR$)$END$ | |
// sout | |
fmt.Printf("------- rmn123 $VAR$: %+v\n", $VAR$)$END$ | |
// fout | |
fmt.Println("$VAR$") | |
// ii | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
fmt.Println("$VALUE$") | |
} | |
// ser | |
package main | |
import ( | |
"context" | |
"log" | |
"net/http" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
"github.com/gorilla/mux" | |
) | |
func MainEndpoint(w http.ResponseWriter, r *http.Request) { | |
w.WriteHeader(200) | |
w.Write([]byte("Main page")) | |
} | |
func LoginEndpoint(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("Login page")) | |
} | |
func ApiEndpoint(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("API main page")) | |
} | |
func ApiDocsEndpoint(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("API docs page")) | |
} | |
func main() { | |
router := mux.NewRouter() | |
router.HandleFunc("/", MainEndpoint).Methods("GET") | |
router.HandleFunc("/login", LoginEndpoint) | |
apiRouter := router.PathPrefix("/api").Subrouter() | |
apiRouter.HandleFunc("/", ApiEndpoint).Methods("GET") | |
apiRouter.HandleFunc("/docs", ApiDocsEndpoint).Methods("GET") | |
apiRouter.HandleFunc("/docs/", ApiDocsEndpoint).Methods("GET") | |
srvr := &http.Server{ | |
Addr: ":8080", | |
Handler: router, | |
WriteTimeout: 15 * time.Second, | |
ReadTimeout: 15 * time.Second, | |
} | |
done := make(chan os.Signal, 1) | |
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | |
go func() { | |
err := srvr.ListenAndServe() | |
if err != nil && err != http.ErrServerClosed { | |
log.Fatalf("listen: %s\n", err) | |
} | |
}() | |
log.Print("Server Started") | |
<-done | |
log.Print("Server Stopped") | |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | |
defer func() { | |
cancel() | |
}() | |
if err := srvr.Shutdown(ctx); err != nil { | |
log.Fatalf("Server Shutdown Failed:%+v", err) | |
} | |
log.Print("Server Exited Properly") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment