goapp

package module
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2022 License: Apache-2.0 Imports: 12 Imported by: 3

README

goapp Build Status GoDoc License

The package is used to initialize an application to simply the creation. Support Go1.12+.

Install

$ go get -u github.com/xgfone/goapp

Usage

package main

import (
	"github.com/xgfone/gconf/v6"
	"github.com/xgfone/go-atexit"
	"github.com/xgfone/go-log"
	"github.com/xgfone/goapp"
	"github.com/xgfone/goapp/router"
	"github.com/xgfone/ship/v5"
)

var opts = []gconf.Opt{
	gconf.StrOpt("opt1", "help doc"),
	gconf.IntOpt("opt2", "help doc"),
}

func main() {
	addr := gconf.NewString("addr", ":80", "The address to listen to.")
	goapp.Init("app", opts...)

	// Initialize the app router.
	app := router.InitRouter(nil)
	app.Logger = log.DefalutLogger
	app.Use(router.Logger(true), router.Recover)
	app.Route("/path1").GET(ship.OkHandler())
	app.Route("/path2").GET(func(c *ship.Context) error { return c.Text(200, "OK") })

	// Start the HTTP server.
	runner := ship.NewRunner(app)
	runner.RegisterOnShutdown(atexit.Execute)
	runner.Start(addr.Get())
}

Build the application above by Makefile like this,

$ make
$ ./app --help
  --addr string
        The address to listen to. (default: ":80")
  --config-file string
        the config file path. (default: "")
  --logfile string
        The file path of the log. The default is stdout. (default: "")
  --loglevel string
        The level of the log, such as debug, info (default: "info")
  --opt1 string
        help doc (default: "")
  --opt2 int
        help doc (default: "0")
  --version bool
        Print the version and exit. (default: "false")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallInit added in v0.21.0

func CallInit() (err error)

CallInit calls the registered initialization functions.

func GetCallStack added in v0.22.0

func GetCallStack(depth int) stack.CallStack

GetCallStack returns the stacks of the caller.

func Init added in v0.7.0

func Init(appName string, opts ...gconf.Opt)

Init is equal to InitApp(appName, gover.Text(), opts...).

func InitApp

func InitApp(appName, version string, opts ...gconf.Opt)

InitApp initializes the application, which is equal to

InitApp2(appName, version, "100M", 100, opts...)

func InitApp2 added in v0.4.0

func InitApp2(appName, version, logfilesize string, logfilenum int, opts ...gconf.Opt)

InitApp2 initializes the application.

  1. Register the log options.
  2. Initialize configuration.
  3. Initialize the logging.
  4. Call the registered initialization functions.

func InitConfig

func InitConfig(app, version string, opts ...gconf.Opt)

InitConfig initializes the configuration, which will set the version, register the options, parse the CLI arguments with "flag", load the "flag", "env" and "file" sources.

func Must added in v0.15.0

func Must(err error, msg string, args ...interface{})

Must logs the error with the msg and the program exits if err is not nil. Or do nothing.

func Panic added in v0.15.0

func Panic(err error, msg string, args ...interface{})

Panic panics with the msg if err is not nil. Or do nothing.

func RegisterInit added in v0.21.0

func RegisterInit(initfuncs ...func() error)

RegisterInit registers the initialization functions.

func Unix

func Unix(sec, nsec int64) time.Time

Unix returns a time with unixstamp in UTC location.

Types

type PanicError added in v0.22.0

type PanicError struct {
	Panic interface{}
	Stack stack.CallStack
}

PanicError is used to represent the panic error.

func NewPanicError added in v0.22.0

func NewPanicError(panic interface{}, depth int) PanicError

NewPanicError returns a new PanicError.

func (PanicError) Error added in v0.22.0

func (pe PanicError) Error() string

Directories

Path Synopsis
Package db supplies some assistant functions about the database.
Package db supplies some assistant functions about the database.
Package exec supplies some convenient execution functions.
Package exec supplies some convenient execution functions.
Package log is used to initialize the logger, and supplies some assistant functions about log.
Package log is used to initialize the logger, and supplies some assistant functions about log.
Package net supplies some assistant functions about net, such as the gateway ip and routes.
Package net supplies some assistant functions about net, such as the gateway ip and routes.
Package router suppies some router assistant functions, such as the router Handlers and Middlewares.
Package router suppies some router assistant functions, such as the router Handlers and Middlewares.
Package validate supplies a struct validator to validate whether the field value of the struct is valid or not.
Package validate supplies a struct validator to validate whether the field value of the struct is valid or not.

Jump to

Keyboard shortcuts

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