implemented HTTP 1.0/1.1 server
This commit is contained in:
@@ -1,23 +1,21 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// TODO: add example config file
|
||||
type Config struct {
|
||||
Port uint16 `toml:"port"`
|
||||
Ip IP `toml:"ip"`
|
||||
Addr string `toml:"address"`
|
||||
}
|
||||
|
||||
func ReadConfig(path string) (*Config, error) {
|
||||
var config = new(Config)
|
||||
var config = DefaultConfig()
|
||||
tomlMeta, err := toml.DecodeFile(path, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -33,8 +31,7 @@ func ReadConfig(path string) (*Config, error) {
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Port: 8080,
|
||||
Ip: IP{net.IPv4(127, 0, 0, 1)},
|
||||
Addr: "127.0.0.1:8080",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,20 +59,3 @@ func WriteConfig(config *Config, path string) (err error) {
|
||||
enc.Indent = " " // 4
|
||||
return enc.Encode(config)
|
||||
}
|
||||
|
||||
type IP struct {
|
||||
net.IP
|
||||
}
|
||||
|
||||
var _ encoding.TextUnmarshaler = (*IP)(nil)
|
||||
var _ encoding.TextMarshaler = IP{}
|
||||
|
||||
func (a *IP) UnmarshalText(text []byte) error {
|
||||
s := string(text)
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
return errors.New("can't parse '" + s + "' as IP address")
|
||||
}
|
||||
a.IP = ip
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user