Categorygithub.com/bonnevoyager/basicserver
modulepackage
1.4.1
Repository: https://github.com/bonnevoyager/basicserver.git
Documentation: pkg.go.dev

# README

basicserver

What is this

basicserver is a preconfigured Iris based web server with user authentication (using JWT token) and key/values+files storage (based on MongoDB).

Examples usage

.env

MONGO=mongodb://127.0.0.1:27017/project
PORT=8081
SECRET=mySecretKey
LOG_LEVEL=info
SMTP_URL=smtp.gmail.com
SMTP_PORT=587
[email protected]
SMTP_PASS=...

main.go

package main

import (
	"os"

	"github.com/bonnevoyager/basicserver"
	"github.com/joho/godotenv"
)

type MyApp struct{}

func getSettings() *basicserver.Settings {
	godotenv.Load()

	secret := os.Getenv("SECRET")
	mongoString := os.Getenv("MONGO")
	serverPort := os.Getenv("PORT")
	logLevel := os.Getenv("LOG_LEVEL")

	smtpURL := os.Getenv("SMTP_URL")
	smtpPort, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
	smtpUser := os.Getenv("SMTP_USER")
	smtpPass := os.Getenv("SMTP_PASS")

	SMTPSettings := &basicserver.SMTPSettings{
		URL:  smtpURL,
		Port: smtpPort,
		User: smtpUser,
		Pass: smtpPass,
	}

	return &basicserver.Settings{
		Secret:      []byte(secret),
		MongoString: mongoString,
		ServerPort:  serverPort,
		LogLevel:    logLevel,
		SMTP:        *SMTPSettings,
	}
}

func main() {
	settings := getSettings()

	myApp := &MyApp{}

	app := basicserver.CreateApp(settings)

	api := app.Iris.Party("/api")
	api.Use(app.RequireAuth())
	{
		api.Get("/profile", myApp.ServeProfileGet(app))
		api.Post("/profile", myApp.ServeProfilePost(app))
	}

	app.Init()
	app.Start(settings.ServerPort)
}

In order to use basicserver, you need to at least provide MongoString and ServerPort configuration options to basicserver.CreateApp(settings).

Preconfigured routes are:

You can add additional routes as in the example above, by adding more handlers.

In case you need user authorization, you can use app.RequireAuth().

Godoc link.

Testing

Since basicserver needs MongoDB connection, a running instance of MongoDB Server should be running.

Test configuration tries to connect to default mongodb://127.0.0.1:27017/test.

License

MIT

# Functions

CreateApp returns BasicApp.

# Structs

BasicApp contains following fields: `Coll.Users` - MongoDB "users" collection `Coll.State` - MongoDB "states" collection `Coll.File` - MongoDB "files" collection `Db` - MongoDB named database `Iris` - iris.Default() instance `Settings` - Settings passed as an argument .
Settings values are used by BasicApp.
SMTPSettings values are used by BasicApp to send emails.
State is an user's state data entity: `ID` user uid `Data` user data .
User is an user data entity: `ID` user uid `Email` user email `Password` encrypted password `RecoveryCode` optional recovery code for password reset `LastLoginAt` time at which last login happened .