Documentation
¶
Overview ¶
Package restfool is a stupidly "foolish" and simple approach of implementing a JSON Restful API.
Initialize the api
confFile := flag.String("c", "conf/api.conf", "path to config ile") flag.Parse() api, err := rest.New(*confFile) if err != nil { log.Fatal(err) }
Add your own handler and go
err = api.AddHandler("Index", "GET", "/", "default page", Index) if err != nil { log.Fatal(err) } err = api.Serve() if err != nil { log.Fatal(err) }
Handler definition
func Index(w http.ResponseWriter, r *http.Request) { qs := rest.ParseQueryStrings(r) message := fmt.Sprintf("Welcome to restfool take a look at https://%s/help", r.Host) msg := rest.Msg{Message: message} rest.EncodeAndSend(w, r, qs, msg) }
Configuration example
[general] # listen supports also IPv6 addresses like :: or ::1 listen = "127.0.0.1" port = "9443" basicauth = false [certs] public = "certs/server.crt" private = "certs/server.key" [tls] # supported minimal ssl/tls version # minversion = ["ssl30", "tls10", "tls11", "tls12"] minversion = "tls12" # used eliptical curves # curveprefs = ["p256","p384","p521","x25519"] curveprefs = ["p256","p384","p521"] # allowed ciphers # ciphers = [ # "TLS_RSA_WITH_RC4_128_SHA", # "TLS_RSA_WITH_3DES_EDE_CBC_SHA", # "TLS_RSA_WITH_AES_128_CBC_SHA", # "TLS_RSA_WITH_AES_256_CBC_SHA", # "TLS_RSA_WITH_AES_128_CBC_SHA256", # "TLS_RSA_WITH_AES_128_GCM_SHA256", # "TLS_RSA_WITH_AES_256_GCM_SHA384", # "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", # "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", # "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", # "TLS_ECDHE_RSA_WITH_RC4_128_SHA", # "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", # "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", # "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", # "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", # "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", # "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", # "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", # "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", # "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", # "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305", # "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305", #] ciphers = [ "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_RSA_WITH_AES_256_GCM_SHA384", ] # if not set equal to false preferserverciphers = true # HTTP Strict Transport Security hsts = true hstsmaxage = 63072000 # cross origin policy [cors] allowcrossorigin = true # corsmethods = ["POST", "GET", "OPTIONS", "PUT", "DELETE"] corsmethods = ["POST", "GET"] allowfrom = "https://localhost:8443" [logging] # type = ["text","json"] type = "text" # loglevel = ["info","error","debug"] loglevel = "debug" # output = ["stdout","logfile"] output = "stdout" # only if output = "logfile" logfile = "mylog.log" [ratelimit] limit = 1500 burst = 300 [[user]] username = "testuser" password = "testpass"
Additional filters ¶
As every useful restful JSON API `?prettify` can be used for pretty print.
Default handlers ¶
By default the throttled.HTTPRateLimiter is used, Basic Auth is supported and CORS Headers are set by the default handlers.
Default routes/path
By default the /help path is added and provides the description interface{} of your added handlers. A nice looking description could look like this:
description := map[string]interface{}{ "Message": "description message", "Post-parameter": map[string]string{ "parameter": "type - description", }, }
Index ¶
- Constants
- Variables
- func Debug(msg string, params interface{})
- func DebugMsg(msg string)
- func EncodeAndSend(w http.ResponseWriter, r *http.Request, qs QueryStrings, msg interface{})
- func Error(msg string, params interface{})
- func ErrorMsg(msg string)
- func Info(msg string, params interface{})
- func InfoMsg(msg string)
- func Log(msg string, params map[string]interface{}, loglevel string)
- func Logger(inner http.Handler, name string) http.Handler
- func SetLogger(newLogger *log.Logger)
- type Certs
- type Config
- type Cors
- type ErrMsg
- type General
- type Logging
- type Msg
- type QueryStrings
- type RateLimit
- type RestAPI
- type Route
- type TlsConf
- type User
Constants ¶
const ( // INFO constant INFO string = "info" // ERROR constant ERROR string = "error" // DEBUG constant DEBUG string = "debug" // LOGSTDOUT constant LOGSTDOUT string = "stdout" // LOGFILE constant LOGFILE string = "logfile" // LOGFORMATJSON constant LOGFORMATJSON string = "json" // LOGFORMATTEXT constant LOGFORMATTEXT string = "text" // SSL30 constant SSL30 string = "ssl30" // TLS10 constant TLS10 string = "tls10" // TLS11 constant TLS11 string = "tls11" // TLS12 constant TLS12 string = "tls12" // CURVEP256 definitio CURVEP256 string = "p256" // CURVEP384 constant CURVEP384 string = "p384" // CURVEP521 constant CURVEP521 string = "p521" // X25519 constant X25519 string = "x25519" )
Variables ¶
var CipherMap = map[string]uint16{ "TLS_RSA_WITH_RC4_128_SHA": tls.TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA": tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA": tls.TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA": tls.TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA256": tls.TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA": tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA": tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305": tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305": tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, }
CipherMap contains all cipers
Functions ¶
func Debug ¶
func Debug(msg string, params interface{})
Debug uses the same syntax as Error function but does not support error type and does not check for errors e.g. Debug("debug", fmt.Errorf("my debug msg")) or using log.Fields / map[string]interface type Debug("debug msg", map[string]interface{}{"val1": "foo", "val2": "bar"}
func EncodeAndSend ¶
func EncodeAndSend(w http.ResponseWriter, r *http.Request, qs QueryStrings, msg interface{})
EncodeAndSend handles some filters, json encodes and outputs msg
func Error ¶
func Error(msg string, params interface{})
Error logs out errors using fields e.g. Error("error msg", fmt.Errorf("my error")) or using log.Fields / map[string]interface type Error("error msg", map[string]interface{}{"val1": "foo", "val2": "bar"}
func Info ¶
func Info(msg string, params interface{})
Info uses the same syntax as Error function but does not support error type and does not check for errors e.g. Info("info", fmt.Errorf("my info msg")) or using log.Fields / map[string]interface type
Info("info msg", map[string]interface{}{ "err": "foo", "someField": "bar" }
Types ¶
type Config ¶
type Config struct { General General `toml:"general"` Certs Certs `toml:"certs"` TLS TlsConf `toml:"tls"` Cors Cors `toml:"cors"` Logging Logging `toml:"logging"` RateLimit RateLimit `toml:"ratelimit"` Users []User `toml:"user"` }
config contains all config information
type ErrMsg ¶
type ErrMsg struct {
Error string `json:"error"`
}
ErrMsg is the standard error message type
type QueryStrings ¶
type QueryStrings struct {
// contains filtered or unexported fields
}
QueryStrings contains all possible query options
func ParseQueryStrings ¶
func ParseQueryStrings(r *http.Request) QueryStrings
ParseQueryStrings parses filters
type RestAPI ¶
RestAPI contains api data
func (*RestAPI) AddHandler ¶
func (a *RestAPI) AddHandler(name string, method string, path string, description interface{}, callback http.HandlerFunc) error
AddHandler adds a new handler to the routing list
func (*RestAPI) AddRoutes ¶
func (a *RestAPI) AddRoutes(router *goji.Mux)
AddRoutes add default handler, routing and ratelimit
func (*RestAPI) InitLogger ¶
func (a *RestAPI) InitLogger()
type Route ¶
type Route struct { Name string Method string Pattern string Description interface{} HandlerFunc http.HandlerFunc }
Route contains all information needed for path routing and help generation