github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/server/server.go (about)

     1  package server
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"mime/multipart"
     7  	"net/http"
     8  	"time"
     9  
    10  	"github.com/ngocphuongnb/tetua/app/entities"
    11  	"github.com/ngocphuongnb/tetua/app/logger"
    12  )
    13  
    14  type AuthProvider interface {
    15  	Name() string
    16  	Login(Context) error
    17  	Callback(Context) (*entities.User, error)
    18  }
    19  
    20  type AuthConfig struct {
    21  	Action       string
    22  	Value        entities.PermType
    23  	DefaultValue entities.PermType
    24  	Prepare      func(c Context) error
    25  	OwnCheckFN   func(c Context) bool
    26  }
    27  
    28  type Cookie struct {
    29  	Name        string    `json:"name"`
    30  	Value       string    `json:"value"`
    31  	Path        string    `json:"path"`
    32  	Domain      string    `json:"domain"`
    33  	MaxAge      int       `json:"max_age"`
    34  	Expires     time.Time `json:"expires"`
    35  	Secure      bool      `json:"secure"`
    36  	HTTPOnly    bool      `json:"http_only"`
    37  	SameSite    string    `json:"same_site"`
    38  	SessionOnly bool      `json:"session_only"`
    39  }
    40  
    41  type StaticConfig struct {
    42  	Compress      bool          `json:"compress"`
    43  	ByteRange     bool          `json:"byte_range"`
    44  	Browse        bool          `json:"browse"`
    45  	Download      bool          `json:"download"`
    46  	Index         string        `json:"index"`
    47  	CacheDuration time.Duration `json:"cache_duration"` // Default value 10 * time.Second.
    48  	MaxAge        int           `json:"max_age"`        // Default value 0
    49  }
    50  
    51  type Context interface {
    52  	RequestID() string
    53  	Hostname() string
    54  	BaseUrl() string
    55  	RouteName() string
    56  	User() *entities.User
    57  	Post(...*entities.Post) *entities.Post
    58  	Meta(meta ...*entities.Meta) *entities.Meta
    59  	Messages(...*entities.Messages) *entities.Messages
    60  	Logger() logger.Logger
    61  	Json(interface{}) error
    62  	Cookie(*Cookie)
    63  	Cookies(string, ...string) string
    64  	Next() error
    65  	Status(int) Context
    66  	Locals(string, ...interface{}) (val interface{})
    67  	Response() Response
    68  	Method() string
    69  	OriginalURL() string
    70  	Path() string
    71  	IP() string
    72  	Header(string, ...string) string
    73  	Param(string) string
    74  	ParamInt(string, ...int) int
    75  	QueryInt(string, ...int) int
    76  	Query(string, ...string) string
    77  	SendString(string) error
    78  	Send([]byte) error
    79  	Redirect(string) error
    80  	RedirectToRoute(name string, params ...map[string]interface{}) error
    81  	BodyParser(interface{}) error
    82  	Render(func(meta *entities.Meta, wr *bufio.Writer)) error
    83  	Context() context.Context
    84  	File(name string) (*multipart.FileHeader, error)
    85  	WithError(msg string, err error)
    86  }
    87  
    88  type Handler func(c Context) error
    89  
    90  type Server interface {
    91  	Test(*http.Request, ...int) (*http.Response, error)
    92  	Listen(string)
    93  	Static(string, string, ...StaticConfig)
    94  	Register(func(s Server)) Server
    95  	Use(...Handler)
    96  	UsePrefix(string, ...Handler)
    97  	Group(string, ...Handler) Group
    98  	Get(string, Handler, ...*AuthConfig)
    99  	Post(string, Handler, ...*AuthConfig)
   100  	Put(string, Handler, ...*AuthConfig)
   101  	Delete(string, Handler, ...*AuthConfig)
   102  	Patch(string, Handler, ...*AuthConfig)
   103  	Head(string, Handler, ...*AuthConfig)
   104  	Options(string, Handler, ...*AuthConfig)
   105  }
   106  
   107  type Response interface {
   108  	StatusCode() int
   109  	Header(string, ...string) string
   110  }
   111  
   112  type Group interface {
   113  	Use(Handler)
   114  	Group(string, ...Handler) Group
   115  	Get(string, Handler, ...*AuthConfig)
   116  	Post(string, Handler, ...*AuthConfig)
   117  	Put(string, Handler, ...*AuthConfig)
   118  	Delete(string, Handler, ...*AuthConfig)
   119  	Patch(string, Handler, ...*AuthConfig)
   120  	Head(string, Handler, ...*AuthConfig)
   121  	Options(string, Handler, ...*AuthConfig)
   122  }
   123  
   124  type Route interface {
   125  	Path() string
   126  	Method() string
   127  	Name() string
   128  }