aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-05-30 03:26:34 +0700
committerShulhan <m.shulhan@gmail.com>2020-05-30 03:26:34 +0700
commit734ce643ecbc992834a8f78b44904b82b09bc84b (patch)
treed67aec8f6e0fe922e3bb6c7abc91fb8ad30ee9e7 /server.go
parent6b03e4fd9670f865216a773545ea4a0bbf534582 (diff)
downloadkamusku-734ce643ecbc992834a8f78b44904b82b09bc84b.tar.xz
all: rename the module to "kamusku"
Diffstat (limited to 'server.go')
-rw-r--r--server.go51
1 files changed, 43 insertions, 8 deletions
diff --git a/server.go b/server.go
index ef7f930..3e2ebd0 100644
--- a/server.go
+++ b/server.go
@@ -2,18 +2,20 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package kbbi
+package kamusku
import (
"context"
"fmt"
"log"
+ "math/rand"
stdhttp "net/http"
"os"
"strings"
"sync"
"time"
+ "github.com/shuLhan/share/lib/ascii"
"github.com/shuLhan/share/lib/debug"
"github.com/shuLhan/share/lib/http"
)
@@ -21,7 +23,6 @@ import (
const (
envKbbiSandi = "KBBI_SANDI"
envKbbiSurel = "KBBI_SUREL"
- defRootDir = "_www-kbbi"
defListen = ":3394"
emptyResponse = "{}"
)
@@ -36,12 +37,12 @@ type Server struct {
// The client that forward request to official KBBI server.
forwardc *directClient
+ stopped chan bool
+ wg sync.WaitGroup
+
// If offline is true and the word definition is not found on cache,
// the server will not forward request to official KBBI server.
offline bool
-
- wg sync.WaitGroup
- stopped chan bool
}
//
@@ -157,13 +158,22 @@ func (server *Server) dumpCache() {
}
}
-func (server *Server) handleDefinisi(
+//
+// handleAdmin is endpoint to manage dictionary cache on the web.
+//
+func (server *Server) handleAdmin(
httpRes stdhttp.ResponseWriter,
httpReq *stdhttp.Request,
reqBody []byte,
) (resBody []byte, err error) {
- httpRes.Header().Set(headerNameACAO, "*")
+ return resBody, nil
+}
+func (server *Server) handleDefinisi(
+ httpRes stdhttp.ResponseWriter,
+ httpReq *stdhttp.Request,
+ reqBody []byte,
+) (resBody []byte, err error) {
paramKata := httpReq.Form.Get(paramNameKata)
if len(paramKata) == 0 {
return []byte(emptyResponse), nil
@@ -229,5 +239,30 @@ func (server *Server) registerEndpoints() (err error) {
ResponseType: http.ResponseTypeJSON,
Call: server.handleDefinisi,
}
- return server.http.RegisterEndpoint(epDefinisi)
+
+ err = server.http.RegisterEndpoint(epDefinisi)
+ if err != nil {
+ return fmt.Errorf("registerEndpoints %q: %w", pathAPIDefinisi,
+ err)
+ }
+
+ rand.Seed(time.Now().Unix())
+ pathAdmin := string(ascii.Random([]byte(ascii.LettersNumber), 16))
+
+ epAdmin := &http.Endpoint{
+ Method: http.RequestMethodGet,
+ Path: pathAdmin,
+ RequestType: http.RequestTypeQuery,
+ ResponseType: http.ResponseTypeHTML,
+ Call: server.handleAdmin,
+ }
+
+ err = server.http.RegisterEndpoint(epAdmin)
+ if err != nil {
+ return fmt.Errorf("registerEndpoints %q: %w", pathAdmin, err)
+ }
+
+ fmt.Println("administration path:", pathAdmin)
+
+ return nil
}