webserver

package module
v0.0.0-...-3a4c8d2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 24 Imported by: 0

README

Web Server

icon

A Web Server Written In... Any Programming Language?

Develop a website with your favorite programming language. Currently Supported Languages: C, Go, Scala.

This module runs a server with a golang base for handling http. Templates are rendered in htmlc which compiles to elixir.

Notice: This Project Is Still In Beta.

Installation

go get github.com/tkdeng/webserver

Usage

func main(){
  // create new server
  app, err := webserver.New("./app")
  
  // do normal gofiber stuff (optional)
  app.Get("/", func(c fiber.Ctx) error {
    return c.SendString("Hello, World!")
  })

  app.Get("/", func(c fiber.Ctx) error {
    // note: c.Render is not used
    // you can use a secondary template with gofiber
    return webserver.Render(c, "index", htmlc.Map{"args": 1}, "layout")
  })

  // 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
templates

Templates are compiled by the htmlc module.

routes

Route files laid out in a nextjs like format. These files can be written in a variety of different programming languages.

Print @PAGE:<name>;, @LAYOUT:<name>;, and @ARGS:<json|base64>; to specify what page to render.

Here is an example in C.

#include <stdio.h>

int main(){
  printf("@PAGE:%s;\n", "index");
  printf("@ARGS:%s;\n", "{\"args\": 1}");
  printf("@LAYOUT:%s;\n", "layout");

  return 0;
}

YAML and JSON can also be used for simpler templates.

page: index
layout: layout
args: {
  title: "Hello, Yaml!",
}

In Scala, the file name represents a base route, and an object represents a sub path. For example, if the file is named test.scala.

object index { // `/test`
  def main(args: Array[String]) = {
    println("Hello, Scala!")
  }
}

object about { // `/test/about`
  def main(args: Array[String]) = {
    println("About, Scala!")
  }
}

We can also name a directory with a languages extension, to have the folder compiled as a single file.

For example, in go we can name out directory about.go/. Then we can write our go module inside this directory.

In this example, main.go will be compiled with it's go.mod file, and any other files or subdirectories.

package main

import "fmt"

func main() {
  fmt.Println("Hello, Golang!")
}

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 Render

func Render(c fiber.Ctx, name string, args htmlc.Map, layout ...string) error

func RenderPage

func RenderPage(c fiber.Ctx, url string) error

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) (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
}

type PageRoute

type PageRoute struct {
	Page   string
	Layout string
	Args   map[string]any
	// contains filtered or unexported fields
}

Directories

Path Synopsis
template command

Jump to

Keyboard shortcuts

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