diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-08 02:19:18 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-09 01:10:24 +0700 |
| commit | 4e35c509a41cecb207bf6f52e7c9a529fa9f71fb (patch) | |
| tree | 4fda47608b7a226afed77344003ab8e8f1a4788d /lib/http/server_options.go | |
| parent | d309b58f63cfc382e0003cff85ab057fd06d3d23 (diff) | |
| download | pakakeh.go-4e35c509a41cecb207bf6f52e7c9a529fa9f71fb.tar.xz | |
lib/http: rename files for consistency
If the type is in CamelCase the file should be using snake_case.
Diffstat (limited to 'lib/http/server_options.go')
| -rw-r--r-- | lib/http/server_options.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/http/server_options.go b/lib/http/server_options.go new file mode 100644 index 00000000..5ff15fb6 --- /dev/null +++ b/lib/http/server_options.go @@ -0,0 +1,74 @@ +// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package http + +import ( + "io" + "log" + "net/http" + + "git.sr.ht/~shulhan/pakakeh.go/lib/memfs" +) + +// ServerOptions define an options to initialize HTTP server. +type ServerOptions struct { + // Memfs contains the content of file systems to be served in memory. + // The MemFS instance to be served should be already embedded in Go + // file, generated using memfs.MemFS.GoEmbed(). + // Otherwise, it will try to read from file system directly. + // + // See https://pkg.go.dev/git.sr.ht/~shulhan/pakakeh.go/lib/memfs#hdr-Go_embed + Memfs *memfs.MemFS + + // HandleFS inspect each GET request to Memfs. + // Some usage of this handler is to check for authorization on + // specific path, handling redirect, and so on. + // If nil it means all request are allowed. + // See FSHandler for more information. + HandleFS FSHandler + + // Address define listen address, using ip:port format. + // This field is optional, default to ":80". + Address string + + // Conn contains custom HTTP server connection. + // This fields is optional. + Conn *http.Server + + // ErrorWriter define the writer where output from panic in handler + // will be written. Basically this will create new log.Logger and set + // the default Server.ErrorLog. + // This field is optional, but if its set it will be used only if Conn + // is not set by caller. + ErrorWriter io.Writer + + // The options for Cross-Origin Resource Sharing. + CORS CORSOptions + + // If true, server generate index.html automatically if its not + // exist in the directory. + // The index.html contains the list of files inside the requested + // path. + EnableIndexHTML bool +} + +func (opts *ServerOptions) init() { + if len(opts.Address) == 0 { + opts.Address = ":80" + } + + if opts.Conn == nil { + opts.Conn = &http.Server{ + ReadTimeout: defRWTimeout, + WriteTimeout: defRWTimeout, + MaxHeaderBytes: 1 << 20, + } + if opts.ErrorWriter != nil { + opts.Conn.ErrorLog = log.New(opts.ErrorWriter, "", 0) + } + } + + opts.CORS.init() +} |
