diff options
Diffstat (limited to 'server.go')
| -rw-r--r-- | server.go | 51 |
1 files changed, 43 insertions, 8 deletions
@@ -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 } |
