server

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 20 Imported by: 0

README

Simple Web Server

Compile HTML and MD template files together into a static html website.

This module uses gofiber and staticweb to create a quick and easy setup for a simple web server.

For extra performance, consider adding the pages.dist directory to a cdn (like cloudflare pages) to serve static html pages.

Installation

# install the go module
go get github.com/tkdeng/simplewebserver

# install dependencies
make

Dependencies

Debian/Ubuntu (Linux)
  sudo apt install libpcre3-dev
Fedora (Linux)
  sudo dnf install pcre-devel
Arch (Linux)
  sudo yum install pcre-dev

Usage


import (
  server "github.com/tkdeng/simplewebserver"
)

func main(){
  // create new server
  app, err := server.New("./app")

  //note: page.dist files will automatically be statically rendered,
  // and take priority over gofiber methods

  // do normal gofiber stuff (optional)
  app.Get("/api", func(c fiber.Ctx) error {
    return c.SendString("Hello, API!")
  })

  //note: page.dist files will automatically be statically rendered,
  // and take priority over gofiber methods
  app.Get("/", func(c fiber.Ctx) error {
    // this will be ignored if index.html exists
    return c.SendString("Hello, World!")
  })

  // listen with openssl (default port: [http: 8080, ssl: 8443])
  err = app.Listen()
}

Inside App Directory

config.yml
title: "Web Server"
app_title: "WebServer"
desc: "A Web Server."

public_uri: "/public/"

port_http: 8080
port_ssl: 8443

origins: [
  "localhost",
  "example.com",
]

proxies: [
  "127.0.0.1",
  "192.168.0.1",
]

DebugMode: no

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = ConfigData{
	Title:    "Web Server",
	AppTitle: "WebServer",
	Desc:     "A Web Server.",

	PortHTTP: 8080,
	PortSSL:  8443,
}

Functions

func GenRsaKey

func GenRsaKey(crtPath string, keyPath string) error

GenRsaKey generates a new ssl certificate and key pair

  • expires: 3 years
  • rsa: 4096
  • x509
  • sha256
  • recommended renewal: once a year

func GenRsaKeyIfNeeded

func GenRsaKeyIfNeeded(crtPath string, keyPath string) error

GenRsaKeyIfNeeded auto detects if the certificates generated by the GenRsaKey method are either

  • not synchronized by date modified
  • are possibly expired (assuming a 1 year renewal)

If it detects this is true, it will automatically regenerate a new certificate

func ListenAutoTLS

func ListenAutoTLS(app *fiber.App, httpPort, sslPort uint16, certPath string, proxy ...[]string) error

ListenAutoTLS will automatically generate a self signed tls certificate if needed and listen to both http and https ports

@httpPort: 80, @sslPort: 443

@certPath: file path to store ssl certificates to (this will generate a my/path.crt and my/path.key file)

@proxy: optional, if only one proxy is specified, the app will only listen to that ip address

func PrintMsg

func PrintMsg(color string, msg string, size int, end bool)

PrintMsg prints to console and auto inserts spaces

func RedirectSSL

func RedirectSSL(httpPort, sslPort uint16) func(c fiber.Ctx) error

RedirectSSL can be added to `app.Use` to auto redirect http to https

@httpPort: 80, @sslPort: 443

func VerifyOrigin

func VerifyOrigin(origin []string, proxy []string, handleErr ...func(c fiber.Ctx, err error) error) func(c fiber.Ctx) error

VerifyOrigin can be added to `app.Use` to enforce that all connections are coming through a specified domain and proxy ip

@origin: list of valid domains

@proxy: list of valid ip proxies

@handleErr: optional, allows you to define a function for handling invalid origins, instead of returning the default http error

Types

type App

type App struct {
	*fiber.App
}

func New

func New(root string, config ...fiber.Config) (App, error)

New loads a new server

func (*App) Listen

func (app *App) Listen() error

Listen to both http and https ports and auto generate a self signed ssl certificate (will also auto renew every year)

by using self signed certs, you can use a proxy like cloudflare and not have to worry about verifying a certificate athority like lets encrypt

type ConfigData

type ConfigData struct {
	Title    string
	AppTitle string
	Desc     string

	PublicURI string

	Origins []string
	Proxies []string

	OriginErrHandler func(c fiber.Ctx, err error) error

	PortHTTP uint16
	PortSSL  uint16

	DebugMode bool

	Root string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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