Created
January 17, 2020 07:22
-
-
Save lotusirous/c26d93b87e611fec4f2e74f8c523d069 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
| package main | |
| import ( | |
| "context" | |
| "net/http" | |
| "github.com/go-chi/chi" | |
| "github.com/sirupsen/logrus" | |
| "golang.org/x/sync/errgroup" | |
| ) | |
| // HandleIndex writes cookie to brower | |
| func HandleIndex() http.HandlerFunc { | |
| fn := func(w http.ResponseWriter, r *http.Request) { | |
| w.Header().Set("Cookie", "__session__=1") | |
| } | |
| return http.HandlerFunc(fn) | |
| } | |
| func main() { | |
| ctx := context.Background() | |
| var g errgroup.Group | |
| r := chi.NewRouter() | |
| r.Get("/", HandleIndex()) | |
| s := http.Server{ | |
| Addr: ":3000", | |
| Handler: r, | |
| } | |
| g.Go(func() error { | |
| return s.ListenAndServe() | |
| }) | |
| g.Go(func() error { | |
| <-ctx.Done() | |
| return s.Shutdown(ctx) | |
| }) | |
| if err := g.Wait(); err != nil { | |
| logrus.Debugln("server stopped", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment