Lux - A web library collection based on net/http

Lux

A web library collection based on net/http.

listen and serve

HTTP

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootRouterGroup := app.NewRouterGroup("/")
	rootRouterGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

HTTP TLS

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootRouterGroup := app.NewRouterGroup("/")
	rootRouterGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	if err := app.ListenAndServe1TLS(":8080", "cert.pem", "key.pem"); err != nil {
		panic(err)
	}
}

HTTP AUTO TLS

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootRouterGroup := app.NewRouterGroup("/")
	rootRouterGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	if err := app.ListenAndServeAutoTLS(nil); err != nil {
		panic(err)
	}
}

HTTP2

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootRouterGroup := app.NewRouterGroup("/")
	rootRouterGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

HTTP2 TLS

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootRouterGroup := app.NewRouterGroup("/")
	rootRouterGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	if err := app.ListenAndServe2TLS(":8080", "cert.pem", "key.pem"); err != nil {
		panic(err)
	}
}

HTTP2 AUTO TLS

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootRouterGroup := app.NewRouterGroup("/")
	rootRouterGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	if err := app.ListenAndServe2AutoTLS(nil); err != nil {
		panic(err)
	}
}

Server

set logger

package main

import (
	"os"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/logext"
)

func main() {
	app := lux.New(nil)

	app.SetLogger(logext.New(os.Stderr))

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

By calling logext.New(writer io.Writer), lux server will write log to writer.

set max header bytes

package main

import (
	"github.com/snowmerak/lux"
)

func main() {
	app := lux.New(nil)

	app.SetMaxHeaderBytes(1024)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

set idle timeout

package main

import (
	"time"

	"github.com/snowmerak/lux"
)

func main() {
	app := lux.New(nil)

	app.SetIdleTimeout(time.Second * 10)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

If KeepAlive is enabled, wait connection during IdleTimeout.

set read and write timeout

package main

import (
	"time"

	"github.com/snowmerak/lux"
)

func main() {
	app := lux.New(nil)

	app.SetReadTimeout(time.Second * 5)
	app.SetWriteTimeout(time.Second * 5)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

reply

string

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("reply string")
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

binary

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyBinary([]byte("Hello World!"))
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

file

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyFile("./public/sample.txt")
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

JSON

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyJSON(map[string]string{"hello": "world"})
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

ETC

func (l *LuxContext) Reply(contentType string, body []byte) error

func (l *LuxContext) ReplyProtobuf(data protoreflect.ProtoMessage) error

func (l *LuxContext) ReplyWebP(data []byte) error

func (l *LuxContext) ReplyJPEG(data []byte) error

func (l *LuxContext) ReplyPNG(data []byte) error

func (l *LuxContext) ReplyWebM(data []byte) error

func (l *LuxContext) ReplyMP4(data []byte) error

func (l *LuxContext) ReplyCSV(data []byte) error

func (l *LuxContext) ReplyHTML(data []byte) error

func (l *LuxContext) ReplyXML(data []byte) error

func (l *LuxContext) ReplyExcel(data []byte) error

func (l *LuxContext) ReplyWord(data []byte) error

func (l *LuxContext) ReplyPDF(data []byte) error

func (l *LuxContext) ReplyPowerpoint(data []byte) error

func (l *LuxContext) ReplyZip(data []byte) error 

func (l *LuxContext) ReplyTar(data []byte) error

func (l *LuxContext) ReplyGZIP(data []byte) error

func (l *LuxContext) Reply7Z(data []byte) error

websocket

echo text

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.Websocket("/", func(w *context.WSContext) error {
		recv, err := w.ReadText()
		if err != nil {
			return err
		}
		err = w.WriteText(recv)
		if err != nil {
			return err
		}
		return nil
	})

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

echo binary

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.Websocket("/", func(w *context.WSContext) error {
		recv, err := w.ReadBinary()
		if err != nil {
			return err
		}
		err = w.WriteBinary(recv)
		if err != nil {
			return err
		}
		return nil
	})

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

echo data with op code

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.Websocket("/", func(w *context.WSContext) error {
		recv, op, err := w.ReadData()
		if err != nil {
			return err
		}
		err = w.WriteData(recv, op)
		if err != nil {
			return err
		}
		return nil
	})

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

set status

set status

package main

import (
	"net/http"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		lc.SetStatus(http.StatusNoContent)
		return nil
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

OK

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		lc.SetOK()
		return nil
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

bad request

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		lc.SetBadRequest()
		return nil
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

not found

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		lc.SetNotFound()
		return nil
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

ETC

func (l *LuxContext) SetAccepted()

func (l *LuxContext) SetNoContent()

func (l *LuxContext) SetResetContent()

func (l *LuxContext) SetFound()

func (l *LuxContext) SetUnauthorized()

func (l *LuxContext) SetForbidden()

func (l *LuxContext) SetInternalServerError()

func (l *LuxContext) SetNotImplemented()

func (l *LuxContext) SetServiceUnavailable()

func (l *LuxContext) SetConflict()

func (l *LuxContext) SetUnsupportedMediaType()

set cookie

full option cookie

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		lc.SetCookie("key", "value", 0, "/", "", false, false)
		return nil
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

secure cookie

package main

import (
	"time"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		lc.SetSecureCookie("key", "value", int(time.Hour.Seconds()), "/", "")
		return nil
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

get

get form file

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		file, header, err := lc.GetFormFile("filename")
		if err != nil {
			return err
		}
		defer file.Close()
		return lc.SaveFile(file, "./"+header.Filename)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

get multipart file

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		headers, err := lc.GetMultipartFile("filename", 10<<20)
		if err != nil {
			return err
		}
		return lc.SaveMultipartFile(headers, "./"+headers[0].Filename)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

get form data

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		value := lc.GetFormData("key")
		return lc.ReplyString(value)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

The data from post form.

get url query

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		value := lc.GetURLQuery("key")
		return lc.ReplyString(value)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

http://localhost:8080/?key=value

get path variable

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/:key", func(lc *context.LuxContext) error {
		value := lc.GetPathVariable("key")
		return lc.ReplyString(value)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

http://localhost:8080/value

get body

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		body, err := lc.GetBody()
		if err != nil {
			return err
		}
		return lc.ReplyBinary(body)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

The data from http body.

get body reader

package main

import (
	"io"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		reader := lc.GetBodyReader()
        defer reader.Close()
		_, err := io.Copy(lc.Response, reader)
		return err
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

get cookie

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		value := lc.GetCookie("key")
		return lc.ReplyString(value)
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

get remote address, ip, port

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.POST("/", func(lc *context.LuxContext) error {
		addr := lc.GetRemoteAddress()
		ip := lc.GetRemoteIP()
		port := lc.GetRemotePort()
		return lc.ReplyJSON(map[string]string{
			"address": addr,
			"ip":      ip,
			"port":    port,
		})
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

logext

set stderr

package main

import (
	"os"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/logext"
)

func main() {
	app := lux.New(nil)

	logger := logext.New(os.Stderr)
	app.SetLogger(logger)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

set file

package main

import (
	"os"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/logext"
)

func main() {
	app := lux.New(nil)

	file, err := os.OpenFile("log.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0755)
	if err != nil {
		panic(err)
	}
	logger := logext.New(file)
	app.SetLogger(logger)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

change default logger's buffer size

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/logext"
	"github.com/snowmerak/lux/logext/stdout"
)

func main() {
	app := lux.New(nil)

	bufferSize := 16
	logger := logext.New(stdout.New(bufferSize))
	app.SetLogger(logger)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

middleware

allow static ip

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.AllowStaticIPs(
			"localhost",
			"127.0.0.1",
			"[::1]",
		),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

allowing localhost, 127.0.0.1, [::1].

allow dynamic ip

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	ipMap := map[string]bool{
		"localhost": true,
		"127.0.0.1": true,
		"[::1]":     true,
	}

	app := lux.New(
		nil,
		middleware.AllowDynamicIPs(
			func(remoteIP string) bool {
				return ipMap[remoteIP]
			},
		),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

block static ip

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.BlockStaticIPs(
			"localhost",
			"127.0.0.1",
			"[::1]",
		),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

blocking localhost, 127.0.0.1, [::1].

block dynamic ip

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	ipMap := map[string]bool{
		"localhost": true,
		"127.0.0.1": true,
		"[::1]":     true,
	}

	app := lux.New(
		nil,
		middleware.BlockDynamicIPs(
			func(remoteIP string) bool {
				return ipMap[remoteIP]
			},
		),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

allow and block ports

func AllowStaticPorts(ports ...string) Set

func BlockStaticPorts(ports ...string) Set 

func AllowDynamicPorts(checker func(remotePort string) bool) Set

func BlockDynamicPorts(checker func(remotePort string) bool) Set

authorize

package main

import (
	"net/http"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/", middleware.Auth(func(authorizaionHeader string, tokenCookie *http.Cookie) bool {
		return true
	}))
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("authorized")
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

compress snappy

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("hello!")
	}, nil, middleware.CompressSnappy)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

If Accept-Encoding do not has snappy, ignore this middleware.

compress gzip

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("hello!")
	}, nil, middleware.CompressGzip)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

If Accept-Encoding do not has gzip, ignore this middleware.

compress brotli

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/")
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("hello!")
	}, nil, middleware.CompressBrotli)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

If Accept-Encoding do not has brotli, ignore this middleware.

allow headers

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.SetAllowHeaders("*"),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

allow methods

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.SetAllowMethods("*"),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

allow origins

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.SetAllowOrigins("*"),
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

allow credentials

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.SetAllowCredentials,
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

allow cors

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(
		nil,
		middleware.SetAllowCORS,
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

router

http methods

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)

	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("GET request")
	}, nil)

	rootGroup.POST("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("POST request")
	}, nil)

	rootGroup.PATCH("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("PATCH request")
	}, nil)

	rootGroup.PUT("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("PUT request")
	}, nil)

	rootGroup.DELETE("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("DELETE request")
	}, nil)

	rootGroup.OPTIONS("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("OPTIONS request")
	}, nil)
	
	rootGroup.HEAD("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("HEAD request") // will be ignored
	}, nil)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

preflight

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)

	rootGroup.Preflight(
		[]string{"*"},
		[]string{"*"},
		[]string{"*"},
	)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

statics

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/", nil, middleware.SetAllowCORS)

	rootGroup.Statics("/public", "./public")

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

embed

package main

import (
	"embed"
	"io/fs"

	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/middleware"
)

//go:embed public
var public embed.FS

func main() {
	app := lux.New(nil)

	rootGroup := app.NewRouterGroup("/", nil, middleware.SetAllowCORS)

	publicFS, err := fs.Sub(public, "public")
	if err != nil {
		panic(err)
	}
	rootGroup.Embedded("/", publicFS)

	if err := app.ListenAndServe1(":8080"); err != nil {
		panic(err)
	}
}

swagger

if you want swagger, you must copy swagger/dist folder in your project.

app info

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	app.ShowSwagger("/swagger")

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

Can set swagger info where in Lux.New() method.

set email

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	app.SetInfoEmail("[email protected]")

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	app.ShowSwagger("/swagger")

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

Lux.SetInfoEmail() method set swagger contact email with parameter.

set license

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	app.SetInfoLicense("MIT", "")

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, nil)

	app.ShowSwagger("/swagger")

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

Lux.SetInfoLicense() method set swagger's license with parameters, name and link.

set router swagger

package main

import (
	"github.com/snowmerak/lux"
	"github.com/snowmerak/lux/context"
	"github.com/snowmerak/lux/middleware"
	"github.com/snowmerak/lux/swagger"
)

func main() {
	app := lux.New(&swagger.Info{
		Title:   "Lux API",
		Version: "1.0.0",
	})

	rootGroup := app.NewRouterGroup("/", middleware.SetAllowCORS)
	rootGroup.GET("/", func(lc *context.LuxContext) error {
		return lc.ReplyString("Hello World!")
	}, &swagger.Router{
		Summary:     "Hello World",
		Description: "This is a simple example of a lux router.",
		Produces:    []string{"text/plain"},
		Responses: map[string]swagger.Response{
			"200": {
				Description: "OK",
				Schema: swagger.Schema{
					Type: "string",
				},
			},
		},
	})

	app.ShowSwagger("/swagger")

	if err := app.ListenAndServe2(":8080"); err != nil {
		panic(err)
	}
}

RouterGroup.AddHandler() and GET(), POST(), PATCH(), PUT(), DELETE(), HEAD(), OPTIONS() method get *swagger.Router parameter. And the *swagger.Router be used for swagger.

show swagger

app.ShowSwagger("/swagger")

Lux.ShowSwagger() method build swagger to given path.

Owner
Similar Resources

Useful collection functions for golang, based on generic types

go-collection English | 简体中文 go-collection provides developers with a convenient set of functions for working with common slices, maps, and arrays dat

Jul 21, 2022

IPIP.net officially supported IP database ipdb format parsing library

IPIP.net officially supported IP database ipdb format parsing library

Dec 27, 2022

EasyNet - A light net library with epoll

easyNet - NON BLOCKING IO Examples echo-server package main import ( "fmt" "g

Aug 24, 2022

Go HTTP tunnel is a reverse tunnel based on HTTP/2.

Go HTTP tunnel is a reverse tunnel based on HTTP/2. It enables you to share your localhost when you don't have a public IP.

Dec 28, 2022

A standalone Web Server developed with the standard http library, suport reverse proxy & flexible configuration

A standalone Web Server developed with the standard http library, suport reverse proxy & flexible configuration

paddy 简介 paddy是一款单进程的独立运行的web server,基于golang的标准库net/http实现。 paddy提供以下功能: 直接配置http响应 目录文件服务器 proxy_pass代理 http反向代理 支持请求和响应插件 部署 编译 $ go build ./main/p

Oct 18, 2022

screen sharing for developers https://screego.net/

screen sharing for developers https://screego.net/

screego/server screen sharing for developers Huge thanks to sipgate for sponsoring this project! Intro In the past I've had some problems sharing my s

Jan 1, 2023

ipx provides general purpose extensions to golang's IP functions in net package

ipx ipx is a library which provides a set of extensions on go's standart IP functions in net package. compability with net package ipx is fully compat

May 24, 2021

ConnPool is a thread safe connection pool for net.Conn interface.

ConnPool is a thread safe connection pool for net.Conn interface. It can be used to manage and reuse connections.

Dec 22, 2022

Go net wrappers that enable TCP Fast Open.

tfo-go tfo-go provides a series of wrappers around net.Listen, net.ListenTCP, net.DialContext, net.Dial, net.DialTCP that seamlessly enable TCP Fast O

Nov 7, 2022
Related tags
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http

fasthttp Fast HTTP implementation for Go. Currently fasthttp is successfully used by VertaMedia in a production serving up to 200K rps from more than

Jan 5, 2023
Simple GUI to convert Charles headers to golang's default http client (net/http)

Charles-to-Go Simple GUI to convert Charles headers to golang's default http client (net/http) Usage Compile code to a binary, go build -ldflags -H=wi

Dec 14, 2021
Nuke-Net is a VERY VERY over powered and ridiculous web crawler that is well- very very noisy XD read more here
Nuke-Net is a VERY VERY over powered and ridiculous web crawler that is well- very very noisy XD read more here

Nuke-Net is a VERY VERY over powered and ridiculous web crawler that is well- very very noisy XD read more here

Dec 20, 2021
Fork of Go stdlib's net/http that works with alternative TLS libraries like refraction-networking/utls.

github.com/ooni/oohttp This repository contains a fork of Go's standard library net/http package including patches to allow using this HTTP code with

Sep 29, 2022
Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway
Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway

Package gateway provides a drop-in replacement for net/http's ListenAndServe for use in AWS Lambda & API Gateway, simply swap it out for gateway.Liste

Nov 24, 2022
A Discord ratelimiter for net/http

A Discord ratelimiter intended to be used with net/http clients using time/x/rate.

Nov 6, 2021
go stomp server base on net/http

stompserver go stomp server base on "net/http" base on "net/http" and "golang.org/x/net/websocket" so use one port, you can be WebServer or StompServe

Sep 22, 2022
EasyTCP is a light-weight and less painful TCP server framework written in Go (Golang) based on the standard net package.

EasyTCP is a light-weight TCP framework written in Go (Golang), built with message router. EasyTCP helps you build a TCP server easily fast and less painful.

Jan 7, 2023
Cat Balancer is line based load balancer for net cat nc.
Cat Balancer is line based load balancer for net cat nc.

Cat Balancer Cat Balancer is line based load balancer for net cat nc. Usage cb [-p <producers-port>] [-c <consumers-port>] One Producer to One Consum

Jul 6, 2022