basicserver is a preconfigured Iris based web server with user authentication (using JWT token) and key/values+files storage (based on MongoDB).
.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:
- POST /register
- POST /signin
- GET /recover
- POST /recover
- POST /change
- DELETE /account
- GET /keepalive
- POST /api/data
- POST /api/file
- GET /api/data
- GET /api/file/{id:string}
- DELETE /api/data
- DELETE /api/file
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().
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
.
MIT