gserver

package module
v0.0.0-...-7d9cdc8 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: BSD-2-Clause Imports: 20 Imported by: 0

README

gserver, in Go

gserver is an experimental web server adapted to serve OGDL templates along with static content. It is not optimized and not well tested, as of now.

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
  • Upload files go to /_user/file/$user/$folder/
  • Template functionality extensible through Go plugins

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

There are two routes configured in gserver:

/:user/file/*filepath
/*filepath

The first one goes to a static file handler that translates that path to /_user/file/:user/*filepath. This handler doesn't create or need sessions.

The second goes to a handler that uses the parameter substitution mechanism explained above, creates sessions, processes file uploads and templates.

Any path that has not the form /token/file/* goes to this second route.

The static handler will not return paths with elements that start with a dot. The dynamic handler, in addition, will ignore path with elements starting with an underscore, since these are reserved for variables.

File upload

Any POST request with files attached (multipart) and a field named "UploadFiles" that is handled by the second handler will store those files in /_user/file/$user/$folder/. $user is the current authenticated user and $folder is 'default' or the content of a field named 'folder' in the request.

Without an authenticated user no files will be stored in the server.

TODO: public and private files (ACL, casbin?)

Templates

TODO

Remote functions

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

ogdlrf
  git
    host localhost:1135
  zoekt
    host localhost:1166

TODO: document in ogdl-go how to create servers and clients.

Plugins

Plugins that are present in .conf/plugin are loaded in made available in the request context. Public methods in those plugins can then be accessed in templates. If a plugin database.so is present, a Database object is expected which will be placed in the context so that $database.Method() can be called in the template.

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.

Documentation

Index

Constants

View Source
const (
	FileDir = "file"
	TmpDir  = ".tmp"
)

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 FileHandler_ deprecated

func FileHandler_(srv *Server, host bool) http.Handler

Deprecated: FileHandler returns a handler that processes all paths that exist in the file system starting from the root directory, whether they are static files or templates or markdown.

NOTE This handler needs context information (access to Server{}) NOTE See https://github.com/bpowers/seshcookie TODO serve files with http.ServeContent (handles large files with Range requests)

Types

type Request

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

func ConvertRequest

func ConvertRequest(r *http.Request, w http.ResponseWriter, host bool, 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
	SecureHost     string
	Hosts          []string
	Config         *ogdl.Graph
	HostContexts   map[string]*ogdl.Graph
	Context        *ogdl.Graph
	Root           *fn.FNode
	DocRoot        string
	UploadDir      string
	Sessions       session.Manager
	Plugins        []string
	Login          login
	ContextService contextService
	DomainConfig   domainConfig
	Templates      map[string]*ogdl.Graph
	Multi          bool
}

func New

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

New prepares a Server{} structure initialized with configuration information and a base context that will be the initial context of each request.

func NewMulti

func NewMulti() (*Server, error)

New prepares a Server{} structure initialized with configuration information and a base context that will be the initial context of each request.

func (*Server) DynamicHandler

func (srv *Server) DynamicHandler(host bool) http.HandlerFunc

DynamicHandler ...

NOTE See https://github.com/bpowers/seshcookie TODO serve files with http.ServeContent (handles large files with Range requests)

func (*Server) DynamicHandlerFn

func (srv *Server) DynamicHandlerFn(host bool, fs *fn.FNode) http.HandlerFunc

DynamicHandler ...

NOTE See https://github.com/bpowers/seshcookie TODO serve files with http.ServeContent (handles large files with Range requests)

func (*Server) LoginAdapterHtpasswd

func (srv *Server) LoginAdapterHtpasswd(host bool) 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(secure bool, timeout int, router fr.Router)

func (*Server) StaticFileHandler

func (srv *Server) StaticFileHandler(host, userspace bool) http.HandlerFunc

StaticFileHandler returns a handler that processes static files.

if host is true, the hostname is prepended to the path if userspace is true, the first element of a path is taken as a user

func (*Server) StaticFileHandlerFn

func (srv *Server) StaticFileHandlerFn(host bool, fs *fn.FNode) http.HandlerFunc

StaticFileHandler returns a handler that processes static files.

if host is true, the hostname is prepended to the path

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