Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A tiny TCP server implementing a basic Buletin Board Service.

```shell
telnet localhost 5555
telnet localhost 2323
```

```text
Expand All @@ -17,13 +17,19 @@ telnet localhost 5555
| |
+-------------------------------------------+
vxn-dev bbs-go service (0.6.1)
telnet localhost 5555
telnet localhost 2323

Enter username (or type 'register'): register
Choose a username: noarche
Choose a password:
Registration successful.
> help
*** Commands:
help --- show this help message
post <message> --- post a message to the board
read --- read recent messages
exit --- quit the session

> test
*** Invalid command

> exit
*** Bye
```

## build and run
Expand Down
63 changes: 6 additions & 57 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,10 @@
package config

import (
"flag"
"os"
"strconv"
)

const (
ENV_DEBUG string = "DEBUG"
ENV_HOST string = "LISTEN_ADDR"
ENV_PORT string = "LISTEN_PORT"
ENV_VERSION string = "PROJECT_VERSION"
)

const (
defaultDebug bool = false
defaultHost string = "localhost"
defaultPort int = 5555
)

var (
Debug bool
Host string
Port int
Version string
Host string
Port int
Version string
Debug bool
MaxMessages = 1000000
MaxReadMessages = 30
)

func init() {
flag.BoolVar(&Debug, "debug", defaultDebug, "a boolean, makes the server's output more verbose")
flag.StringVar(&Host, "host", defaultHost, "a string, a FQDN type of host's name")
flag.IntVar(&Port, "port", defaultPort, "an integer, a port to listen for incoming requests on")

flag.Parse()

//
// Try ENV vars.
//

var err error

// Should be non-empty true, otherwise is false.
if os.Getenv(ENV_DEBUG) != "" {
Debug, err = strconv.ParseBool(os.Getenv(ENV_DEBUG))
if err != nil {
Debug = defaultDebug
}
}

// Should be set to anything non-empty.
if os.Getenv(ENV_PORT) != "" {
Port, err = strconv.Atoi(os.Getenv(ENV_PORT))
if err != nil {
Port = defaultPort
}
}

if os.Getenv(ENV_VERSION) != "" {
Version = os.Getenv(ENV_VERSION)
}
}
32 changes: 18 additions & 14 deletions internal/server/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package server

import (
"fmt"
"os"
"strings"

"go.vxn.dev/bbs-go/internal/config"
"bbs-go/internal/config"
)

var WelcomeMessage = `
+-------------------------------------------+
| __ __ |
| / /_ / /_ _____ ____ ____ |
| / __ \/ __ \/ ___/_____/ __ \/ __ \ |
| / /_/ / /_/ (__ )_____/ /_/ / /_/ / |
| /_.___/_.___/____/ \__, /\____/ |
| /____/ |
| |
+-------------------------------------------+
vxn-dev bbs-go service (` + config.Version + `)
telnet ` + config.Host + ` ` + fmt.Sprintf("%d", config.Port) + `
var WelcomeMessage string

`
func LoadMOTD(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}

msg := string(content)
msg = strings.ReplaceAll(msg, "{{VERSION}}", config.Version)
msg = strings.ReplaceAll(msg, "{{HOST}}", config.Host)
msg = strings.ReplaceAll(msg, "{{PORT}}", fmt.Sprintf("%d", config.Port))

WelcomeMessage = msg
return nil
}
2 changes: 1 addition & 1 deletion internal/server/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

var (
ErrShutdownStarted = errors.New("the server shutdown process has been already started")
ErrShutdownStarted = errors.New("The server shutdown process has been already started")
)
Loading