httpapp

package
v0.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 3 Imported by: 3

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/RussellLuo/appx"
	"github.com/RussellLuo/kun/pkg/appx/httpapp"
	"github.com/go-chi/chi"
)

type Hi struct {
	router chi.Router
}

func (h *Hi) Router() chi.Router {
	return h.router
}

func (h *Hi) Init(appx.Context) error {
	h.router = chi.NewRouter()
	h.router.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("Got a request for /hi")
	})
	return nil
}

type Bye struct {
	router chi.Router
}

func (b *Bye) Router() chi.Router {
	return b.router
}

func (b *Bye) Init(appx.Context) error {
	b.router = chi.NewRouter()
	b.router.Get("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("Got a request for /bye")
	})
	return nil
}

type Greeter struct {
	router chi.Router
	server *http.Server
}

func (g *Greeter) Router() chi.Router {
	return g.router
}

func (g *Greeter) Init(appx.Context) error {
	g.router = chi.NewRouter()
	g.server = &http.Server{
		Addr:    ":8080",
		Handler: g.router,
	}
	return nil
}

func (g *Greeter) Start(context.Context) error {
	fmt.Println("Starting HTTP server")
	go g.server.ListenAndServe() // nolint:errcheck
	return nil
}

func (g *Greeter) Stop(ctx context.Context) error {
	fmt.Println("Stopping HTTP server")
	return g.server.Shutdown(ctx)
}

func main() {
	r := appx.NewRegistry()

	// Typically located in `func init()` of package hi.
	r.MustRegister(httpapp.New("hi", new(Hi)).MountOn("greeter", "/hi").App)

	// Typically located in `func init()` of package bye.
	r.MustRegister(httpapp.New("bye", new(Bye)).MountOn("greeter", "/bye").App)

	// Typically located in `func init()` of package greeter.
	r.MustRegister(httpapp.New("greeter", new(Greeter)).App)

	// Typically located in `func main()` of package main.
	r.SetOptions(&appx.Options{
		ErrorHandler: func(err error) {
			fmt.Printf("err: %v\n", err)
		},
	})

	// Installs the applications.
	if err := r.Install(context.Background()); err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}
	defer r.Uninstall()

	// Start the greeter.
	startCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := r.Start(startCtx); err != nil {
		fmt.Printf("err: %v\n", err)
		return
	}

	// Make two HTTP requests to demonstrate that our server is running.
	_, _ = http.Get("http://localhost:8080/hi")
	_, _ = http.Get("http://localhost:8080/bye")

	// Stop the greeter.
	stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	r.Stop(stopCtx)

}
Output:

Starting HTTP server
Got a request for /hi
Got a request for /bye
Stopping HTTP server

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MountOn added in v0.4.4

func MountOn(parent, pattern string) func(appx.Standard) appx.Standard

func MountRouter

func MountRouter(parent chi.Router, pattern string, r chi.Router)

Types

type App

type App struct {
	*appx.App
}

func New

func New(name string, instance appx.Instance) *App

func (*App) MountOn

func (a *App) MountOn(parent, pattern string) *App

func (*App) Require

func (a *App) Require(names ...string) *App

func (*App) Use added in v0.4.5

func (a *App) Use(middlewares ...func(appx.Standard) appx.Standard) *App

type ChiRouter

type ChiRouter interface {
	Router() chi.Router
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL