wip
This commit is contained in:
@@ -17,7 +17,7 @@ const (
|
||||
defaultKeyFile = "hamlogger.key"
|
||||
applicationName = "hamlogger"
|
||||
|
||||
defaultPort int = 20120
|
||||
defaultPort int = 8081
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -36,6 +36,7 @@ type Config struct {
|
||||
LogPath string `json:"logfile" yaml:"logfile"`
|
||||
RunPath string `json:"runfile" yaml:"runfile"`
|
||||
DataPath string `json:"datadir" yaml:"datadir"`
|
||||
SharePath string `json:"sharedir" yaml:"sharedir"`
|
||||
Daemon bool `json:"daemon" yaml:"daemon"`
|
||||
CertPath string `json:"certPath" yaml:"certPath"`
|
||||
KeyPath string `json:"keyPath" yaml:"keyPath"`
|
||||
@@ -51,6 +52,7 @@ func NewConfig() *Config {
|
||||
Hostname: defaultHostname,
|
||||
Build: defaultBuild,
|
||||
DataPath: datadirPath,
|
||||
SharePath: sharedirPath,
|
||||
Daemon: false,
|
||||
Debug: false,
|
||||
}
|
||||
@@ -121,7 +123,7 @@ func (conf *Config) ReadOpts() error {
|
||||
func (conf *Config) Postproc() error {
|
||||
var err error
|
||||
if conf.X509Cert == "" || conf.X509Key == "" {
|
||||
certBytes, keyBytes, err := aux509.CreateX509SelfSignedCert(conf.Hostname)
|
||||
certBytes, keyBytes, err := aux509.CreateX509Cert(conf.Hostname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,5 +6,7 @@ const (
|
||||
rundirPath = "@srv_rundir@"
|
||||
logdirPath = "@srv_logdir@"
|
||||
datadirPath = "@srv_datadir@"
|
||||
sharedirPath = "@srv_sharedir@"
|
||||
|
||||
)
|
||||
|
||||
|
||||
@@ -7,21 +7,24 @@ import (
|
||||
|
||||
type HandlerConfig struct {
|
||||
Datadir string
|
||||
Sharedir string
|
||||
Database *database.Database
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
log *logger.Logger
|
||||
db *database.Database
|
||||
datadir string
|
||||
log *logger.Logger
|
||||
db *database.Database
|
||||
datadir string
|
||||
sharedir string
|
||||
}
|
||||
|
||||
func NewHandler(conf *HandlerConfig) (*Handler, error) {
|
||||
var err error
|
||||
hand := &Handler{
|
||||
datadir: conf.Datadir,
|
||||
db: conf.Database,
|
||||
log: logger.NewLogger("handler"),
|
||||
datadir: conf.Datadir,
|
||||
sharedir: conf.Sharedir,
|
||||
db: conf.Database,
|
||||
log: logger.NewLogger("handler"),
|
||||
}
|
||||
return hand, err
|
||||
}
|
||||
|
||||
@@ -1,15 +1,47 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"fmt"
|
||||
|
||||
"hamlogger/internal/router"
|
||||
)
|
||||
|
||||
|
||||
type HelloParams struct{}
|
||||
type HelloResult struct{}
|
||||
|
||||
func (hand *Handler) Hello(ctx *router.Context) {
|
||||
func (hand *Handler) GetHello(ctx *router.Context) {
|
||||
var err error
|
||||
res := &HelloResult{}
|
||||
hand.SendResult(ctx, res, err)
|
||||
}
|
||||
|
||||
func (hand *Handler) GetIndex(ctx *router.Context) {
|
||||
|
||||
filename := "index.html"
|
||||
filename = filepath.Join(hand.sharedir, filename)
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
hand.log.Errorf("cannot open file %s: %v", filename, err)
|
||||
return
|
||||
}
|
||||
stat, _ := file.Stat()
|
||||
|
||||
ctx.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
ctx.Writer.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
|
||||
defer file.Close()
|
||||
io.Copy(ctx.Writer, file)
|
||||
//ctx.Writer.Write([]byte(payload))
|
||||
}
|
||||
|
||||
/*
|
||||
func (hand *Handler) SendHTML(ctx *router.Context, filename string) {
|
||||
ctx.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
file, _ := os.Open(filepath.Join(hand.sharedir, filename))
|
||||
defer file.Close()
|
||||
io.Copy(ctx.Writer, file)
|
||||
//ctx.Writer.Write([]byte(payload))
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -13,18 +13,15 @@ type Response struct {
|
||||
}
|
||||
|
||||
func (hand *Handler) SendResult(ctx *router.Context, res any, err error) {
|
||||
var resp *Response
|
||||
resp := &Response{
|
||||
Result: res,
|
||||
}
|
||||
if err != nil {
|
||||
resp = &Response{
|
||||
Error: true,
|
||||
Message: fmt.Sprintf("%v", err),
|
||||
Result: res,
|
||||
}
|
||||
} else {
|
||||
resp = &Response{
|
||||
Error: false,
|
||||
Result: res,
|
||||
}
|
||||
resp.Message = fmt.Sprintf("%v", err)
|
||||
resp.Error = true
|
||||
}
|
||||
ctx.SendJSON(resp)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -77,6 +77,7 @@ func (srv *Server) Build() error {
|
||||
// Create X509 certs
|
||||
handlerConfig := &handler.HandlerConfig{
|
||||
Datadir: srv.conf.DataPath,
|
||||
Sharedir: srv.conf.SharePath,
|
||||
Database: srv.db,
|
||||
}
|
||||
srv.hand, err = handler.NewHandler(handlerConfig)
|
||||
|
||||
@@ -56,7 +56,8 @@ func (svc *Service) Build() error {
|
||||
svc.rout.Use(router.NewLoggingMiddleware(svc.log.Infof))
|
||||
svc.rout.Use(router.NewCorsMiddleware())
|
||||
|
||||
svc.rout.Get(`/api/v1/service/hello`, svc.hand.Hello)
|
||||
svc.rout.Get(`/api/v1/service/hello`, svc.hand.GetHello)
|
||||
svc.rout.Get(`/`, svc.hand.GetIndex)
|
||||
|
||||
svc.rout.NotFound(svc.hand.NotFound)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user