2019-06-03 20:30:43 +02:00
|
|
|
// Package web provides web based micro services
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2019-12-05 16:15:35 +02:00
|
|
|
"context"
|
2019-06-03 20:30:43 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-03-11 19:55:39 +02:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2019-06-03 20:30:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service is a web service with service discovery built in
|
|
|
|
type Service interface {
|
|
|
|
Client() *http.Client
|
|
|
|
Init(opts ...Option) error
|
|
|
|
Options() Options
|
|
|
|
Handle(pattern string, handler http.Handler)
|
|
|
|
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
|
|
|
|
Run() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(o *Options)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// For serving
|
|
|
|
DefaultName = "go-web"
|
|
|
|
DefaultVersion = "latest"
|
|
|
|
DefaultId = uuid.New().String()
|
|
|
|
DefaultAddress = ":0"
|
|
|
|
|
|
|
|
// for registration
|
|
|
|
DefaultRegisterTTL = time.Minute
|
|
|
|
DefaultRegisterInterval = time.Second * 30
|
|
|
|
|
|
|
|
// static directory
|
2019-12-05 16:15:35 +02:00
|
|
|
DefaultStaticDir = "html"
|
|
|
|
DefaultRegisterCheck = func(context.Context) error { return nil }
|
2020-03-11 19:55:39 +02:00
|
|
|
|
|
|
|
log = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "web"})
|
2019-06-03 20:30:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewService returns a new web.Service
|
|
|
|
func NewService(opts ...Option) Service {
|
|
|
|
return newService(opts...)
|
|
|
|
}
|