summaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'server.go')
-rw-r--r--server.go55
1 files changed, 14 insertions, 41 deletions
diff --git a/server.go b/server.go
index 673c211..40ba88d 100644
--- a/server.go
+++ b/server.go
@@ -9,23 +9,17 @@ import (
"encoding/json"
"fmt"
"log"
- "math/rand"
- "net/http"
"os"
"strings"
"sync"
"time"
"git.sr.ht/~shulhan/kamusku"
- "github.com/shuLhan/share/lib/ascii"
- "github.com/shuLhan/share/lib/debug"
- libhttp "github.com/shuLhan/share/lib/http"
- "github.com/shuLhan/share/lib/memfs"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
+ libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
)
-//
// Server for kamusku with caching and spell checking functionalities.
-//
type Server struct {
httpd *libhttp.Server
kamus *dictionary
@@ -41,22 +35,18 @@ type Server struct {
offline bool
}
-//
// NewServer create and initialize the server with optional path to dictionary
// storage.
-//
func NewServer(dictionaryStorage string) (server *Server, err error) {
+ var logp = `NewServer`
+
address := defListen
port := os.Getenv(envPort)
if len(port) > 0 {
address = ":" + port
}
- httpdOpts := &libhttp.ServerOptions{
- Options: memfs.Options{
- Root: "_www",
- Development: debug.Value >= 2,
- },
+ var httpdOpts = libhttp.ServerOptions{
Memfs: memfsWWW,
Address: address,
}
@@ -67,22 +57,22 @@ func NewServer(dictionaryStorage string) (server *Server, err error) {
server.kamus, err = newDictionary(dictionaryStorage)
if err != nil {
- return nil, fmt.Errorf("NewServer: %w", err)
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
server.httpd, err = libhttp.NewServer(httpdOpts)
if err != nil {
- return nil, fmt.Errorf("NewServer: %w", err)
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
server.kbbic, err = kamusku.NewKbbiClient()
if err != nil {
- return nil, fmt.Errorf("NewServer: %w", err)
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
err = server.registerEndpoints()
if err != nil {
- return nil, fmt.Errorf("NewServer: %w", err)
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
}
if !server.kbbic.IsAuthenticated() {
@@ -99,9 +89,7 @@ func NewServer(dictionaryStorage string) (server *Server, err error) {
return server, nil
}
-//
// Start the HTTP server.
-//
func (server *Server) Start() (err error) {
go server.dumpCacheJob()
@@ -110,9 +98,7 @@ func (server *Server) Start() (err error) {
return server.httpd.Start()
}
-//
// Shutdown the HTTP server and save the cache for future use.
-//
func (server *Server) Shutdown() (err error) {
err = server.httpd.Shutdown(context.TODO())
@@ -124,9 +110,7 @@ func (server *Server) Shutdown() (err error) {
return err
}
-//
// dumpCacheJob periodically save the cache or when the server stopped.
-//
func (server *Server) dumpCacheJob() {
ticker := time.NewTicker(1 * time.Hour)
@@ -142,9 +126,7 @@ func (server *Server) dumpCacheJob() {
}
}
-//
// dumpCache to storage to be loaded later.
-//
func (server *Server) dumpCache() {
if !server.kamus.isChanging() {
return
@@ -155,19 +137,13 @@ func (server *Server) dumpCache() {
}
}
-//
// handleAdmin is endpoint to manage dictionary cache on the web.
-//
-func (server *Server) handleAdmin(
- _ http.ResponseWriter, _ *http.Request, _ []byte,
-) (resBody []byte, err error) {
+func (server *Server) handleAdmin(_ *libhttp.EndpointRequest) (resBody []byte, err error) {
return resBody, nil
}
-func (server *Server) handleDefinisi(
- _ http.ResponseWriter, httpReq *http.Request, _ []byte,
-) (resBody []byte, err error) {
- paramKata := httpReq.Form.Get(paramNameKata)
+func (server *Server) handleDefinisi(epr *libhttp.EndpointRequest) (resBody []byte, err error) {
+ paramKata := epr.HTTPRequest.Form.Get(paramNameKata)
if len(paramKata) == 0 {
return []byte(jsonEmptyObject), nil
}
@@ -215,11 +191,9 @@ func (server *Server) handleDefinisi(
return json.Marshal(res)
}
-//
// registerEndpoints register the API endpoints.
-//
func (server *Server) registerEndpoints() (err error) {
- epDefinisi := &libhttp.Endpoint{
+ var epDefinisi = libhttp.Endpoint{
Method: libhttp.RequestMethodGet,
Path: pathAPIDefinisi,
RequestType: libhttp.RequestTypeQuery,
@@ -233,10 +207,9 @@ func (server *Server) registerEndpoints() (err error) {
err)
}
- rand.Seed(time.Now().Unix())
pathAdmin := string(ascii.Random([]byte(ascii.LettersNumber), 16))
- epAdmin := &libhttp.Endpoint{
+ var epAdmin = libhttp.Endpoint{
Method: libhttp.RequestMethodGet,
Path: pathAdmin,
RequestType: libhttp.RequestTypeQuery,