gserver2

package module
v0.0.0-...-6b47fe2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: BSD-2-Clause Imports: 33 Imported by: 0

README

gserver, in Go

gserver is an experimental web server adapted to serve OGDL templates along with static content.

To do

  • Put allmost all code in library:
    • config: .conf vs ini
  • Commercial or OSS main.go possible

Features

  • The file extension of (some) files below the document root is optional
  • Trailing slash and index files detection
  • Markdown rendering on the fly.
  • Login, Logout example functionality
  • Multiple hosts

Minimum number of dependencies

No HTTPS support (let a front-end server handle this).

  • securecookies
  • go-chi

Parameter substitution in paths

While resolving a path in the document root, entries of the form _token are used for path elements not directly found in the file system. In that case 'token' will be available later as a variable, set to the unknown path element.

For example:

 /john/blog/1

will be sent to

/_user/blog/_id/index.htm

if that path is present. Two variables will be available in the context:

user=john
id=1

Routes

File upload

Templates

Remote functions

OGDL remote functions (RPC endpoints) can be configured in .conf/config:

Markdown processor ($MD())

TODO: describe the extensions and the \escape inline syntax for processing style and function calls.

Form to context

Form inputs are stored in the request's context and made directly accessible in templates. Input names are taken as simple paths (tokens separated by dots), and the special case where the name ends with ._ogdl is parsed as OGDL before stored in the context.

For example, the content of the following form:

<form>
<input name="obj.name" />
<input name="obj.conf._ogdl"/>

could be something like this:

obj
  name
    "Pepe Delgado"
  conf
    ip
      192.160.1.1
    net
      255.255.255.0

Login, Logout

There is no specific path for login or logout. Any request which does not go to the static file handler will recognize the parameters 'Login' and 'Logout' if present. The default login handler accepts any non-empty username and any password.

The 'redirect' parameter can be used to send the user to a specific page after login. The default behavior is to return to the same page.

Configuration

Where it is

.conf directory *.ini file

context.ogdl

Configurable parameters

  • home directory (default .)
  • default server port

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TplExtensions []string = []string{".htm", ".txt", ".csv", ".json", ".g", ".ogdl", ".xml", ".xlsx", ".svg"}

Functions

func AccessAdapter

func AccessAdapter(config string) func(http.Handler) http.Handler

func CreateUploadDir

func CreateUploadDir()

func DeleteUserCookie

func DeleteUserCookie(w http.ResponseWriter)

func GetACL

func GetACL(user string, srv *Server) string

func GlobalContext

func GlobalContext(srv *Server)

GlobalContext loads the context for template processing

Types

type Request

type Request struct {
	HttpRequest *http.Request
	User        string
	Context     *ogdl.Graph
	Path        string
	File        *fn.FNode
	Mime        string
}

func ConvertRequest

func ConvertRequest(r *http.Request, w http.ResponseWriter, srv *Server) *Request

func (*Request) Get

func (r *Request) Get() error

Get is a direct map from URL to file (binary content + params) TODO: Clean r.Path vs r.File.Path dicotomy

func (*Request) Process

func (r *Request) Process(srv *Server) error

Process processes templates, remaining path fragments and sets mime the type

type Server

type Server struct {
	Host         string
	Hosts        []string
	Config       *ogdl.Graph
	HostContexts map[string]*ogdl.Graph
	Context      *ogdl.Graph
	Root         *fn.FNode
	DocRoot      string
	UploadDir    string
	DefaultUser  string
	UserDb       *sql.DB
	MaxSessions  int
	Multi        bool
	Templates    map[string]*ogdl.Graph
	ContextMu    sync.RWMutex

	Secret []byte // Used as key for encoding persistent user cookies
	// contains filtered or unexported fields
}

func New

func New(host string) (*Server, error)

New prepares a Server initialized from .conf/config.ogdl and .conf/context.ogdl.

func NewMulti

func NewMulti() (*Server, error)

NewMulti prepares a Server for virtual-host mode. Each subdirectory that looks like a hostname gets its own context loaded from <host>/.conf/context.ogdl.

func NewWithConfig

func NewWithConfig(host string, config, context *ogdl.Graph) (*Server, error)

func (*Server) DynamicHandler

func (srv *Server) DynamicHandler(fs *fn.FNode) http.HandlerFunc

DynamicHandler returns a handler that processes dynamic (template/data) requests.

If fs is non-nil it is tried first; on miss the handler falls back to srv.Root. If fs is nil, srv.Root is used and path-level auth is enforced via checkPath.

func (*Server) InitSessions

func (srv *Server) InitSessions()

func (*Server) LoginAdapter

func (srv *Server) LoginAdapter(userdb string) func(http.Handler) http.Handler

LoginAdapter handles "Login" and "Logout"

Login: sets r.Form["user"] to the authenticated user name. Logout: removes the session Other: do nothing

func (*Server) Serve

func (srv *Server) Serve(timeout int, router http.Handler)

func (*Server) Shutdown

func (srv *Server) Shutdown()

func (*Server) StaticFileHandler

func (srv *Server) StaticFileHandler(protect bool, fs *fn.FNode) http.HandlerFunc

StaticFileHandler returns a handler that serves static files.

If protect is true, an authenticated user is required. If fs is non-nil it is used as the file tree; otherwise srv.Root is used. In multi-host mode (srv.Multi) the hostname is prepended to the path.

func (*Server) UserCookie

func (srv *Server) UserCookie() *securecookie.Obj

func (*Server) UserCookieValue

func (srv *Server) UserCookieValue(r *http.Request) string

func (*Server) WatchContext

func (srv *Server) WatchContext(path string)

WatchContext watches the given context.ogdl file and reloads srv.Context whenever the file is written or replaced. Intended to be run as a goroutine.

Directories

Path Synopsis
Gserver is a web server.
Gserver is a web server.

Jump to

Keyboard shortcuts

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