2024-08-17 16:53:58 +00:00
|
|
|
package facade
|
|
|
|
|
|
|
|
import (
|
2024-08-19 15:46:28 +00:00
|
|
|
"log"
|
2024-08-17 16:53:58 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2024-08-20 19:10:21 +00:00
|
|
|
"github.com/nesterow/dal/pkg/adapter"
|
|
|
|
"github.com/nesterow/dal/pkg/handler"
|
2024-08-17 16:53:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SQLiteServer struct {
|
|
|
|
Cwd string
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Init initializes the SQLiteServer struct with the required environment variables.
|
|
|
|
- `SQLITE_DIRECTORY` is the directory where the SQLite database is stored.
|
|
|
|
- `SQLITE_PORT` is the port on which the server listens.
|
|
|
|
*/
|
|
|
|
func (s *SQLiteServer) Init() {
|
|
|
|
s.Cwd = os.Getenv("SQLITE_DIRECTORY")
|
|
|
|
if s.Cwd == "" {
|
|
|
|
panic("env variable `SQLITE_DIRECTORY` is not set")
|
|
|
|
}
|
|
|
|
os.MkdirAll(s.Cwd, os.ModePerm)
|
|
|
|
os.Chdir(s.Cwd)
|
2024-08-21 20:28:41 +00:00
|
|
|
s.Port = os.Getenv("SQLITE_PORT")
|
2024-08-17 16:53:58 +00:00
|
|
|
if s.Port == "" {
|
|
|
|
s.Port = "8118"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
GetAdapter returns a DBAdapter struct with the SQLite3 dialect registered.
|
|
|
|
- The `SQLITE_PRAGMAS` environment variable is expected to be a semicolon separated list of PRAGMA statements.
|
|
|
|
*/
|
|
|
|
func (s *SQLiteServer) GetAdapter() adapter.DBAdapter {
|
|
|
|
adapter.RegisterDialect("sqlite3", adapter.CommonDialect{})
|
|
|
|
db := adapter.DBAdapter{
|
|
|
|
Type: "sqlite3",
|
|
|
|
}
|
|
|
|
db.AfterOpen("PRAGMA journal_mode=WAL")
|
|
|
|
for _, pragma := range strings.Split(os.Getenv("SQLITE_PRAGMAS"), ";") {
|
|
|
|
if pragma == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
db.AfterOpen(pragma)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
GetHanfler returns a http.Handler configured for the SQLiteServer.
|
|
|
|
*/
|
|
|
|
func (s *SQLiteServer) GetHandler() http.Handler {
|
|
|
|
return handler.QueryHandler(s.GetAdapter())
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Serve starts the basic server on the configured port.
|
|
|
|
Use `GetHandler` to get a handler for a custom server.
|
|
|
|
*/
|
|
|
|
func (s *SQLiteServer) Serve() {
|
2024-08-17 17:57:14 +00:00
|
|
|
s.Init()
|
2024-08-19 15:46:28 +00:00
|
|
|
log.Println("Starting server on port " + s.Port)
|
|
|
|
log.Println("Using directory: " + s.Cwd)
|
2024-08-17 16:53:58 +00:00
|
|
|
err := http.ListenAndServe(":"+s.Port, s.GetHandler())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|