Minor optimizations

pull/42/head
Fenny 2020-02-05 02:53:07 +01:00
parent 931093ed76
commit bd2e034c49
7 changed files with 30 additions and 27 deletions

2
go.mod
View File

@ -4,5 +4,7 @@ go 1.11
require (
github.com/json-iterator/go v1.1.9
github.com/klauspost/compress v1.9.8 // indirect
github.com/klauspost/cpuid v1.2.3 // indirect
github.com/valyala/fasthttp v1.9.0
)

3
go.sum
View File

@ -5,8 +5,11 @@ github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGn
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs=
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.9.8 h1:VMAMUUOh+gaxKTMk+zqbjsSjsIcUcL/LF4o63i82QyA=
github.com/klauspost/compress v1.9.8/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=

View File

@ -9,6 +9,7 @@ package fiber
import (
"fmt"
"log"
"net"
"os"
"os/exec"
@ -32,7 +33,7 @@ func (r *Fiber) Listen(address interface{}, tls ...string) {
}
host = val
default:
panic("Host must be an INT port or STRING address")
log.Fatal("Listen: Host must be an INT port or STRING address")
}
// Create fasthttp server
server := &fasthttp.Server{
@ -75,18 +76,18 @@ func (r *Fiber) Listen(address interface{}, tls ...string) {
ln, err := net.Listen("tcp4", host)
if err != nil {
panic(err)
log.Fatal("Listen: ", err)
}
// enable TLS/HTTPS
if len(tls) > 1 {
if err := server.ServeTLS(ln, tls[0], tls[1]); err != nil {
panic(err)
log.Fatal("Listen: ", err)
}
}
if err := server.Serve(ln); err != nil {
panic(err)
log.Fatal("Listen: ", err)
}
}
@ -103,13 +104,13 @@ func (r *Fiber) prefork(server *fasthttp.Server, host string, tls ...string) {
childs[i].Stdout = os.Stdout
childs[i].Stderr = os.Stderr
if err := childs[i].Start(); err != nil {
panic(err)
log.Fatal("Listen-prefork: ", err)
}
}
for _, child := range childs {
if err := child.Wait(); err != nil {
panic(err)
log.Fatal("Listen-prefork: ", err)
}
}
@ -122,17 +123,17 @@ func (r *Fiber) prefork(server *fasthttp.Server, host string, tls ...string) {
ln, err := reuseport.Listen("tcp4", host)
if err != nil {
panic(err)
log.Fatal("Listen-prefork: ", err)
}
// enable TLS/HTTPS
if len(tls) > 1 {
if err := server.ServeTLS(ln, tls[0], tls[1]); err != nil {
panic(err)
log.Fatal("Listen-prefork: ", err)
}
}
if err := server.Serve(ln); err != nil {
panic(err)
log.Fatal("Listen-prefork: ", err)
}
}

View File

@ -10,7 +10,6 @@ package fiber
import (
"encoding/base64"
"fmt"
"log"
"mime"
"mime/multipart"
"strings"
@ -21,9 +20,8 @@ import (
// Accepts : https://gofiber.github.io/fiber/#/context?id=accepts
func (ctx *Ctx) Accepts(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
return ""
}
h := ctx.Get(fasthttp.HeaderAccept)
if h == "" {
return offers[0]
@ -60,7 +58,7 @@ func (ctx *Ctx) Accepts(offers ...string) string {
// AcceptsCharsets : https://gofiber.github.io/fiber/#/context?id=acceptscharsets
func (ctx *Ctx) AcceptsCharsets(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
return ""
}
h := ctx.Get(fasthttp.HeaderAcceptCharset)
@ -86,7 +84,7 @@ func (ctx *Ctx) AcceptsCharsets(offers ...string) string {
// AcceptsEncodings : https://gofiber.github.io/fiber/#/context?id=acceptsencodings
func (ctx *Ctx) AcceptsEncodings(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
return ""
}
h := ctx.Get(fasthttp.HeaderAcceptEncoding)
@ -112,9 +110,8 @@ func (ctx *Ctx) AcceptsEncodings(offers ...string) string {
// AcceptsLanguages : https://gofiber.github.io/fiber/#/context?id=acceptslanguages
func (ctx *Ctx) AcceptsLanguages(offers ...string) string {
if len(offers) == 0 {
panic("You must provide atleast one content type string")
return ""
}
h := ctx.Get(fasthttp.HeaderAcceptLanguage)
if h == "" {
return offers[0]
@ -356,10 +353,8 @@ func (ctx *Ctx) Route() *Route {
}
// SaveFile : https://gofiber.github.io/fiber/#/context?id=secure
func (ctx *Ctx) SaveFile(fh *multipart.FileHeader, path string) {
if err := fasthttp.SaveMultipartFile(fh, path); err != nil {
log.Fatal(err)
}
func (ctx *Ctx) SaveFile(fh *multipart.FileHeader, path string) error {
return fasthttp.SaveMultipartFile(fh, path)
}
// Secure : https://gofiber.github.io/fiber/#/context?id=secure
@ -392,5 +387,5 @@ func (ctx *Ctx) Xhr() bool {
// XHR : https://gofiber.github.io/fiber/#/context?id=xhr
func (ctx *Ctx) XHR() bool {
return ctx.Get("X-Requested-With") == "XMLHttpRequest"
return ctx.Get(fasthttp.HeaderXRequestedWith) == "XMLHttpRequest"
}

View File

@ -100,7 +100,7 @@ func (ctx *Ctx) Cookie(key, value string, options ...interface{}) {
cook.SetSameSite(sameSite)
}
default:
panic("Invalid cookie options")
log.Println("Cookie: Invalid &Cookie{} struct")
}
}
@ -144,7 +144,7 @@ func (ctx *Ctx) Format(args ...interface{}) {
ctx.SendString("<p>" + body + "</p>")
case "json":
if err := ctx.JSON(body); err != nil {
log.Fatal(err)
log.Println("Format: error serializing json ", err)
}
default:
ctx.SendString(body)

View File

@ -8,6 +8,7 @@
package fiber
import (
"log"
"regexp"
"strings"
@ -61,12 +62,12 @@ func (r *Fiber) register(method string, args ...interface{}) {
path = args[0].(string)
handler = args[1].(func(*Ctx))
if path[0] != '/' && path[0] != '*' {
panic("Invalid path, must begin with slash '/' or wildcard '*'")
log.Fatal("Router: Invalid path, must begin with slash '/' or wildcard '*'")
}
}
if midware && strings.Contains(path, "/:") {
panic("You cannot use :params in Use()")
log.Fatal("Router: You cannot use :params in Use()")
}
// If Use() path == "/", match anything aka *
@ -92,7 +93,7 @@ func (r *Fiber) register(method string, args ...interface{}) {
// We have parametes, so we need to compile regex from the path
regex, err := getRegex(path)
if err != nil {
panic("Invalid url pattern: " + path)
log.Fatal("Router: Invalid url pattern: " + path)
}
// Add regex + params to route

View File

@ -8,6 +8,7 @@
package fiber
import (
"log"
"path/filepath"
"strings"
)
@ -38,7 +39,7 @@ func (r *Fiber) Static(args ...string) {
// Lets get all files from root
files, _, err := getFiles(root)
if err != nil {
panic(err)
log.Fatal("Static: ", err)
}
// ./static/compiled => static/compiled