first commit
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
commit
c4f28c696d
|
@ -0,0 +1,19 @@
|
|||
MIT License Copyright (c) 2020 Michael Lindman
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next
|
||||
paragraph) shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,153 @@
|
|||
package colour
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Base Colours
|
||||
const (
|
||||
FgBlack = "\x1b[30m"
|
||||
FgRed = "\x1b[31m"
|
||||
FgGreen = "\x1b[32m"
|
||||
FgYellow = "\x1b[33m"
|
||||
FgBlue = "\x1b[34m"
|
||||
FgMagenta = "\x1b[35m"
|
||||
FgCyan = "\x1b[36m"
|
||||
FgWhite = "\x1b[37m"
|
||||
)
|
||||
|
||||
// Text Attributes
|
||||
const (
|
||||
Reset = "\x1b[0m"
|
||||
Bold = "\x1b[1m"
|
||||
Underscore = "\x1b[4m"
|
||||
Reverse = "\x1b[7m"
|
||||
Hidden = "\x1b[8m"
|
||||
)
|
||||
|
||||
// BlackTitle Printf with bold black
|
||||
func BlackTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgBlack+format+Reset, a...)
|
||||
}
|
||||
|
||||
// RedTitle Printf with bold red
|
||||
func RedTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgRed+format+Reset, a...)
|
||||
}
|
||||
|
||||
// GreenTitle Printf with bold green
|
||||
func GreenTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgGreen+format+Reset, a...)
|
||||
}
|
||||
|
||||
// YellowTitle Printf with bold yellow
|
||||
func YellowTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgYellow+format+Reset, a...)
|
||||
}
|
||||
|
||||
// BlueTitle Printf with bold blue
|
||||
func BlueTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgBlue+format+Reset, a...)
|
||||
}
|
||||
|
||||
// MagentaTitle Printf with bold magenta
|
||||
func MagentaTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgMagenta+format+Reset, a...)
|
||||
}
|
||||
|
||||
// CyanTitle Printf with bold cyan
|
||||
func CyanTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgCyan+format+Reset, a...)
|
||||
}
|
||||
|
||||
// WhiteTitle Printf with bold white
|
||||
func WhiteTitle(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, Bold+FgWhite+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Black Printf with the colour black
|
||||
func Black(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgBlack+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Red Printf with the colour red
|
||||
func Red(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgRed+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Green Printf with the colour green
|
||||
func Green(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgGreen+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Yellow Printf with the colour yellow
|
||||
func Yellow(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgYellow+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Blue Printf with the blue
|
||||
func Blue(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgBlue+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Magenta Printf with the magenta
|
||||
func Magenta(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgMagenta+format+Reset, a...)
|
||||
}
|
||||
|
||||
// Cyan Printf with the cyan
|
||||
func Cyan(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgCyan+format+Reset, a...)
|
||||
}
|
||||
|
||||
// White Printf with the white
|
||||
func White(format string, a ...interface{}) (n int, err error) {
|
||||
return fmt.Fprintf(os.Stdout, FgWhite+format+Reset, a...)
|
||||
}
|
||||
|
||||
// String formats string with coloured text
|
||||
func String(format string, value ...string) string {
|
||||
return strings.Join(value, "") + format + Reset
|
||||
}
|
||||
|
||||
// BlackString formats string with black text
|
||||
func BlackString(format string) string {
|
||||
return String(format, FgBlack)
|
||||
}
|
||||
|
||||
// RedString formats string with red text
|
||||
func RedString(format string) string {
|
||||
return String(format, FgRed)
|
||||
}
|
||||
|
||||
// GreenString formats string with green text
|
||||
func GreenString(format string) string {
|
||||
return String(format, FgGreen)
|
||||
}
|
||||
|
||||
// YellowString formats string with yellow text
|
||||
func YellowString(format string) string {
|
||||
return String(format, FgYellow)
|
||||
}
|
||||
|
||||
// BlueString formats string with blue text
|
||||
func BlueString(format string) string {
|
||||
return String(format, FgBlue)
|
||||
}
|
||||
|
||||
// MagentaString formats string with magenta text
|
||||
func MagentaString(format string) string {
|
||||
return String(format, FgMagenta)
|
||||
}
|
||||
|
||||
// CyanString formats string with cyan text
|
||||
func CyanString(format string) string {
|
||||
return String(format, FgCyan)
|
||||
}
|
||||
|
||||
// WhiteString formats string with white text
|
||||
func WhiteString(format string) string {
|
||||
return String(format, FgWhite)
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package colour
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Status colour code statuscodes
|
||||
func Status(statuscode int) (status string) {
|
||||
switch {
|
||||
case statuscode == 200:
|
||||
status = GreenString(strconv.Itoa(statuscode))
|
||||
case statuscode >= 500 && statuscode <= 511:
|
||||
status = RedString(strconv.Itoa(statuscode))
|
||||
case statuscode >= 400 && statuscode <= 451:
|
||||
status = YellowString(strconv.Itoa(statuscode))
|
||||
default:
|
||||
status = MagentaString(strconv.Itoa(statuscode))
|
||||
}
|
||||
return status
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Logger Options
|
||||
type Logger struct {
|
||||
ErrorLog *os.File
|
||||
AccessLog *os.File
|
||||
}
|
||||
|
||||
// New instance of logger
|
||||
func New(path string) (logger *Logger, err error) {
|
||||
errorlog, err := file(path + "error.log")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accesslog, err := file(path + "access.log")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger = &Logger{
|
||||
ErrorLog: errorlog,
|
||||
AccessLog: accesslog,
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return logger, nil
|
||||
}
|
||||
|
||||
// logFile manages error file
|
||||
func file(path string) (*os.File, error) {
|
||||
dir, _ := filepath.Split(path)
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
os.Mkdir(dir, 0775)
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// Error logs errors to file
|
||||
func (l *Logger) Error(e error) {
|
||||
log.SetOutput(io.MultiWriter(os.Stderr, l.ErrorLog))
|
||||
log.Println(e)
|
||||
}
|
||||
|
||||
// Access logs access to file
|
||||
func (l *Logger) Access(s string) {
|
||||
log.SetOutput(l.AccessLog)
|
||||
log.Println(s)
|
||||
}
|
||||
|
||||
// Close log files
|
||||
func (l *Logger) Close() error {
|
||||
if err := l.AccessLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := l.ErrorLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package nettools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// IP returns IP address from requester
|
||||
func IP(r *http.Request) (net.IP, error) {
|
||||
realip := r.Header.Get("x-forwarded-for")
|
||||
if realip != "" {
|
||||
return net.ParseIP(realip), nil
|
||||
}
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return net.ParseIP(ip), nil
|
||||
}
|
||||
|
||||
// Hostname returns hostname
|
||||
func Hostname(ip net.IP) ([]string, error) {
|
||||
names, err := net.LookupAddr(ip.String())
|
||||
unknown := []string{"unknown host"}
|
||||
if err != nil {
|
||||
return unknown, err
|
||||
}
|
||||
if len(names) == 0 {
|
||||
return unknown, fmt.Errorf("unknown hostname for %v", ip.String())
|
||||
}
|
||||
return names, nil
|
||||
}
|
Reference in New Issue