Add ability to shutdown the server

This commit is contained in:
spsobole 2024-03-21 12:09:05 -06:00
parent b312ba9cb1
commit 6dbdeafca2
2 changed files with 31 additions and 28 deletions

View File

@ -16,7 +16,6 @@ type Options struct {
kv OptionsProvider
}
// FormValueProvider we only care about something that can provide us the K/V lookup
type FormValueProvider interface {
FormValue(string) string
@ -36,7 +35,6 @@ func (f *Form) Get(k string) (string, bool) {
return v, true
}
type Vars struct {
vars map[string]string
}
@ -59,7 +57,6 @@ func (q *Query) Get(key string) (string, bool) {
return "", false
}
func (o *Options) GetVar(k string) (string, bool) {
return o.kv.Get(k)
}
@ -85,7 +82,6 @@ func (o *Options) GetVarAsSlice(k string, delim string, def []string) []string {
return r
}
// StringValue returns form value as a string
func (o *Options) StringValue(k string) (string, bool) {
return o.kv.Get(k)
@ -121,6 +117,7 @@ func (o *Options) StringSlice(k string, delim string) ([]string,bool) {
}
// TimeValue returns a parsed time value for the specified key,
//
// if the key does not exist the but defaultValue was set it will return the defaultValue
// otherwise it will return the passed in time or an error if parsing failed
func (o *Options) TimeValue(key, timeFormat string, defaultValue *time.Time) time.Time {
@ -141,9 +138,6 @@ func (o *Options) TimeValue(key, timeFormat string, defaultValue *time.Time) tim
return t
}
type CompoundOptions struct {
kvs []OptionsProvider
}

View File

@ -1,6 +1,7 @@
package snap
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
@ -28,6 +29,8 @@ type Server struct {
fs http.FileSystem
auth auth.Authenticator
router *mux.Router
http *http.Server
templates string
cachedTmpl *template.Template
templateFuncs template.FuncMap
@ -320,7 +323,7 @@ func (s *Server) ServeTLS(keyPath string, certPath string) error {
log.Fatalf("%v\n", err)
}
srv := &http.Server{
s.http = &http.Server{
Handler: s.router,
Addr: s.address,
// Good practice: enforce timeouts for servers you create!
@ -328,30 +331,30 @@ func (s *Server) ServeTLS(keyPath string, certPath string) error {
ReadTimeout: 120 * time.Second,
TLSConfig: &tls.Config{},
}
srv.TLSConfig.GetCertificate = kpr.GetCertificateFunc()
return srv.ListenAndServeTLS("", "")
s.http.TLSConfig.GetCertificate = kpr.GetCertificateFunc()
return s.http.ListenAndServeTLS("", "")
}
func (s *Server) ServeTLSRedirect(address string) error {
srv := &http.Server{
s.http = &http.Server{
Addr: address,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 120 * time.Second,
ReadTimeout: 120 * time.Second,
}
return srv.ListenAndServe()
return s.http.ListenAndServe()
}
// Serve serve content forever
// Serve content forever
func (s *Server) Serve() error {
srv := &http.Server{
s.http = &http.Server{
Handler: s.router,
Addr: s.address,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 120 * time.Second,
ReadTimeout: 120 * time.Second,
}
return srv.ListenAndServe()
return s.http.ListenAndServe()
}
func (s *Server) WithStaticFiles(prefix string) *Server {
@ -424,6 +427,12 @@ func (s *Server) Dump() {
fmt.Printf(" Templates: %s\n", s.templates)
}
func (s *Server) Shutdown(ctx context.Context) {
if s.http != nil {
s.http.Shutdown(ctx)
}
}
func New(address string, path string, auth auth.Authenticator) *Server {
s := Server{
router: mux.NewRouter(),