123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package bootstrap
- import (
- "context"
- "fmt"
- "net/http"
- "os"
- "time"
- "github.com/vladimirok5959/golang-ctrlc/ctrlc"
- )
- type customHandler func(h http.Handler) http.Handler
- type callbackBeforeAfter func(ctx context.Context, w http.ResponseWriter, r *http.Request, o interface{})
- type callbackServer func(s *http.Server)
- type Opts struct {
- Handle customHandler
- Host string
- Timeout time.Duration
- Path string
- Before callbackBeforeAfter
- After callbackBeforeAfter
- Cbserv callbackServer
- Object interface{}
- }
- type bootstrap struct {
- ctx context.Context
- opts *Opts
- }
- func new(ctx context.Context, opts *Opts) *bootstrap {
- return &bootstrap{ctx: ctx, opts: opts}
- }
- func (this *bootstrap) handler(w http.ResponseWriter, r *http.Request) {
- if this.opts.Before != nil {
- this.opts.Before(this.ctx, w, r, this.opts.Object)
- }
- if r.URL.Path == "/"+this.opts.Path+"/bootstrap.css" {
- w.Header().Set("Cache-Control", "public, max-age=31536000")
- w.Header().Set("Content-Type", "text/css")
- w.Write(resource_bootstrap_css)
- return
- } else if r.URL.Path == "/"+this.opts.Path+"/bootstrap.js" {
- w.Header().Set("Cache-Control", "public, max-age=31536000")
- w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
- w.Write(resource_bootstrap_js)
- return
- } else if r.URL.Path == "/"+this.opts.Path+"/jquery.js" {
- w.Header().Set("Cache-Control", "public, max-age=31536000")
- w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
- w.Write(resource_jquery_js)
- return
- } else if r.URL.Path == "/"+this.opts.Path+"/popper.js" {
- w.Header().Set("Cache-Control", "public, max-age=31536000")
- w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
- w.Write(resource_popper_js)
- return
- }
- if this.opts.After != nil {
- this.opts.After(this.ctx, w, r, this.opts.Object)
- }
- }
- func Start(opts *Opts) {
- if opts == nil {
- fmt.Println("Start: options is not defined")
- os.Exit(1)
- }
- ctrlc.App(
- opts.Timeout,
- func(ctx context.Context, shutdown context.CancelFunc) *[]ctrlc.Iface {
- mux := http.NewServeMux()
- mux.HandleFunc("/", new(ctx, opts).handler)
- var srv *http.Server
- if opts.Handle == nil {
- srv = &http.Server{
- Addr: opts.Host,
- Handler: mux,
- }
- } else {
- srv = &http.Server{
- Addr: opts.Host,
- Handler: opts.Handle(mux),
- }
- }
- if opts.Cbserv != nil {
- opts.Cbserv(srv)
- }
- go func() {
- fmt.Printf("Starting server at http://%s/\n", opts.Host)
- if err := srv.ListenAndServe(); err != nil {
- if err != http.ErrServerClosed {
- fmt.Printf("Web server startup error: %s\n", err.Error())
- shutdown()
- return
- }
- }
- }()
- return &[]ctrlc.Iface{srv}
- },
- )
- }
|