Skip to content

BonneVoyager/basicserver

Repository files navigation

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
SMTP_USER=...@gmail.com
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

About

basicserver is a preconfigured Iris web server with JWT user authentication and MongoDB storage.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages