initial import

This commit is contained in:
Олег Бородин
2024-01-16 09:02:47 +02:00
commit e18bc7beef
61 changed files with 17123 additions and 0 deletions

3
internal/config/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*~
path.go
user.go

107
internal/config/config.go Normal file
View File

@@ -0,0 +1,107 @@
package config
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/go-yaml/yaml"
)
const (
defaultPort int = 8080
defaultHostname = "localhost"
)
var (
defaultBuild = "NODATE"
)
type ServiceConfig struct {
PortNum int `json:"port" yaml:"port"`
}
type Config struct {
Service ServiceConfig `json:"service" yaml:"service"`
Hostname string `json:"hostname" yaml:"hostname"`
Debug bool `json:"debug" yaml:"debug"`
Build string `json:"build" yaml:"build"`
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"`
}
func NewConfig() *Config {
conf := &Config{
Service: ServiceConfig{
PortNum: defaultPort,
},
Debug: false,
Hostname: defaultHostname,
Build: defaultBuild,
SharePath: sharedirPath,
DataPath: datadirPath,
}
exeName := filepath.Base(os.Args[0])
conf.LogPath = filepath.Join(logdirPath, fmt.Sprintf("%s.log", exeName))
conf.RunPath = filepath.Join(rundirPath, fmt.Sprintf("%s.pid", exeName))
return conf
}
func (conf *Config) ReadFile() error {
var err error
exeName := filepath.Base(os.Args[0])
configPath := filepath.Join(confdirPath, fmt.Sprintf("%s.yaml", exeName))
confBytes, err := os.ReadFile(configPath)
if err != nil {
return err
}
err = yaml.Unmarshal(confBytes, conf)
if err != nil {
return err
}
return err
}
func (conf *Config) ReadEnv() error {
var err error
return err
}
func (conf *Config) ReadOpts() error {
var err error
exeName := filepath.Base(os.Args[0])
flag.IntVar(&conf.Service.PortNum, "port", conf.Service.PortNum, "listen port")
flag.BoolVar(&conf.Daemon, "daemon", conf.Daemon, "run as daemon")
flag.BoolVar(&conf.Debug, "debug", conf.Debug, "on debug mode")
help := func() {
fmt.Println("")
fmt.Printf("Usage: %s [option]\n", exeName)
fmt.Println("")
fmt.Println("Options:")
flag.PrintDefaults()
fmt.Println("")
}
flag.Usage = help
flag.Parse()
return err
}
func (conf *Config) Yaml() (string, error) {
var err error
var res string
yamlBytes, err := yaml.Marshal(conf)
if err != nil {
return res, err
}
res = string(yamlBytes)
return res, err
}

View File

@@ -0,0 +1,11 @@
package config
const (
confdirPath = "@srv_confdir@"
rundirPath = "@srv_rundir@"
logdirPath = "@srv_logdir@"
sharedirPath = "@srv_sharedir@"
datadirPath = "@srv_datadir@"
)

View File

@@ -0,0 +1,25 @@
package handler
import (
"webserv/pkg/logger"
"webserv/internal/logic"
)
type HandlerConfig struct {
Logic *logic.Logic
}
type Handler struct {
log *logger.Logger
lg *logic.Logic
}
func NewHandler(conf *HandlerConfig) (*Handler, error) {
var err error
hand := &Handler{
log: logger.NewLogger("handler"),
lg: conf.Logic,
}
return hand, err
}

50
internal/handler/user.go Normal file
View File

@@ -0,0 +1,50 @@
package handler
import (
"webserv/pkg/auxtool/auxhttp"
"webserv/internal/logic"
"github.com/gin-gonic/gin"
)
// https://github.com/swaggo/swag
// CreateUser godoc
// @Summary Create user
// @Description Create user for service
// @Router /user/create [POST]
// @Param username formData string true "User name"
// @Param grants formData []string true "Grants list"
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} logic.CreateUserResult
// @Tags user
func (hand *Handler) CreateUser(gctx *gin.Context) {
var err error
req := &logic.CreateUserRequest{}
err = gctx.ShouldBind(req)
if err != nil {
hand.log.Error("CreateUser parse incoming parameters error:", err)
auxhttp.SendError(gctx, err)
return
}
ctx := gctx.Request.Context()
res, err := hand.lg.CreateUser(ctx, req)
auxhttp.SendResult(gctx, res)
}
func (hand *Handler) DeleteUser(gctx *gin.Context) {
var err error
req := &logic.DeleteUserRequest{}
err = gctx.ShouldBind(req)
if err != nil {
hand.log.Error("DeleteUser parse incoming parameters error:", err)
auxhttp.SendError(gctx, err)
return
}
ctx := gctx.Request.Context()
res, err := hand.lg.DeleteUser(ctx, req)
auxhttp.SendResult(gctx, res)
}

17
internal/logic/logic.go Normal file
View File

@@ -0,0 +1,17 @@
package logic
import (
"webserv/pkg/logger"
)
type LogicConfig struct {}
type Logic struct {
log *logger.Logger
}
func NewLogic(conf *LogicConfig) (*Logic, error) {
var err error
lg := Logic{}
lg.log = logger.NewLogger("logic")
return &lg, err
}

42
internal/logic/user.go Normal file
View File

@@ -0,0 +1,42 @@
package logic
import (
"context"
yaml "sigs.k8s.io/yaml"
)
type CreateUserRequest struct {
Username string `json:"username" form:"username"`
Grants []string `json:"grants" form:"grants"`
}
type CreateUserResult struct {
Username string `json:"username" form:"username"`
Grants []string `json:"grants" form:"grants"`
}
func (lg *Logic) CreateUser(ctx context.Context, params *CreateUserRequest) (*CreateUserResult, error) {
var err error
res := &CreateUserResult{
Username: params.Username,
Grants: params.Grants,
}
reqYaml, err := yaml.Marshal(params)
if err != nil {
return res, err
}
lg.log.Debugf("Create user request: \n%s\n", string(reqYaml))
return res, err
}
type DeleteUserRequest struct {
Username string `json:"username" form:"username"`
}
type DeleteUserResult struct {}
func (lg *Logic) DeleteUser(ctx context.Context, params *DeleteUserRequest) (*DeleteUserResult, error) {
var err error
res := &DeleteUserResult{}
return res, err
}

280
internal/server/server.go Normal file
View File

@@ -0,0 +1,280 @@
package server
import (
"os"
"os/signal"
"os/user"
"path/filepath"
"strconv"
"syscall"
"fmt"
"webserv/internal/config"
"webserv/internal/logic"
"webserv/internal/handler"
"webserv/internal/service"
"webserv/pkg/auxtool/aux509"
"webserv/pkg/logger"
"webserv/pkg/swaginfo"
"github.com/swaggo/swag"
)
type Server struct {
conf *config.Config
svc *service.Service
lg *logic.Logic
hand *handler.Handler
log *logger.Logger
x509cert []byte
x509key []byte
x509caCert []byte
x509caKey []byte
}
func NewServer() (*Server, error) {
var err error
srv := &Server{}
srv.log = logger.NewLogger("server")
return srv, err
}
func (srv *Server) Service() *service.Service {
return srv.svc
}
func (srv *Server) Configure() error {
var err error
srv.conf = config.NewConfig()
err = srv.conf.ReadFile()
if err != nil {
srv.log.Warningf("Cannot load config file, error: %v", err)
//return err
}
err = srv.conf.ReadEnv()
if err != nil {
return err
}
err = srv.conf.ReadOpts()
if err != nil {
return err
}
err = srv.WriteSwaginfo()
if err != nil {
return err
}
srv.log.Info("Service configuration completed")
return err
}
func (srv *Server) Build() error {
var err error
srv.log.Infof("Build server")
confData, err := srv.conf.Yaml()
if err != nil {
return err
}
srv.log.Infof("Current configuration is:\n%s\n", confData)
// Creating X509 certs
srv.x509caCert, srv.x509caKey, err = aux509.CreateX509CACert(srv.conf.Hostname)
if err != nil {
return err
}
srv.x509cert, srv.x509key, err = aux509.CreateX509Cert(srv.conf.Hostname, srv.x509caKey)
if err != nil {
return err
}
// Creating logic
logicConfig := &logic.LogicConfig{}
srv.lg, err = logic.NewLogic(logicConfig)
if err != nil {
return err
}
// Creating handler
handlerConfig := &handler.HandlerConfig{
Logic: srv.lg,
}
srv.hand, err = handler.NewHandler(handlerConfig)
if err != nil {
return err
}
// Creating service
serviceConfig := &service.ServiceConfig{
AssetsDir: srv.conf.SharePath,
PortNum: srv.conf.Service.PortNum,
Hostname: srv.conf.Hostname,
Handler: srv.hand,
X509Cert: srv.x509cert,
X509Key: srv.x509key,
}
srv.svc, err = service.NewService(serviceConfig)
if err != nil {
return err
}
// Build service
err = srv.svc.Build()
if err != nil {
return err
}
return err
}
func (srv *Server) Run() error {
var err error
currUser, err := user.Current()
if err != nil {
return err
}
srv.log.Infof("Running server as user %s", currUser.Username)
sigs := make(chan os.Signal, 1)
svcDone := make(chan error, 1)
// Running service
startService := func(svc *service.Service, done chan error) {
err = svc.Run()
if err != nil {
srv.log.Errorf("Service error: %v", err)
done <- err
}
}
go startService(srv.svc, svcDone)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var signal os.Signal
select {
case signal = <-sigs:
srv.log.Infof("Services stopped by signal: %v", signal)
srv.svc.Stop()
case err = <-svcDone:
srv.log.Infof("Service stopped by service error: %v", err)
srv.svc.Stop()
}
return err
}
func (srv *Server) WriteSwaginfo() error {
var err error
srv.log.Info("Writing swagger info")
swaggerUIDir := filepath.Join(srv.conf.SharePath, "swagger")
err = os.MkdirAll(swaggerUIDir, 0755)
if err != nil {
return err
}
swagInfo := swaginfo.SwaggerInfo
swagInfo.Host = fmt.Sprintf("%s:%d", srv.conf.Hostname, srv.conf.Service.PortNum)
swagInfo.InfoInstanceName = "server"
swag.Register(swagInfo.InstanceName(), swagInfo)
swagDoc := swagInfo.ReadDoc()
swaggerFilePath := filepath.Join(swaggerUIDir, "swagger.json")
err = os.WriteFile(swaggerFilePath, []byte(swagDoc), 0644)
if err != nil {
return err
}
srv.log.Infof("Swagger info wrote to %s", swaggerFilePath)
return err
}
func (srv *Server) PseudoFork() error {
const successExit int = 0
var keyEnv string = "IMX0LTSELMRF8K"
var err error
_, isChild := os.LookupEnv(keyEnv)
switch {
case !isChild:
os.Setenv(keyEnv, "TRUE")
procAttr := syscall.ProcAttr{}
cwd, err := os.Getwd()
if err != nil {
return err
}
var sysFiles = make([]uintptr, 3)
sysFiles[0] = uintptr(syscall.Stdin)
sysFiles[1] = uintptr(syscall.Stdout)
sysFiles[2] = uintptr(syscall.Stderr)
procAttr.Files = sysFiles
procAttr.Env = os.Environ()
procAttr.Dir = cwd
_, err = syscall.ForkExec(os.Args[0], os.Args, &procAttr)
if err != nil {
return err
}
os.Exit(successExit)
case isChild:
_, err = syscall.Setsid()
if err != nil {
return err
}
}
os.Unsetenv(keyEnv)
return err
}
func (srv *Server) Daemonize() error {
var err error
if srv.conf.Daemon {
// Restart process process
err = srv.PseudoFork()
if err != nil {
return err
}
// Redirect stdin
nullFile, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
if err != nil {
return err
}
err = syscall.Dup2(int(nullFile.Fd()), int(os.Stdin.Fd()))
if err != nil {
return err
}
// Redirect stderr and stout
logdir := filepath.Dir(srv.conf.LogPath)
err = os.MkdirAll(logdir, 0750)
if err != nil {
return err
}
logFile, err := os.OpenFile(srv.conf.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
return err
}
err = syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
if err != nil {
return err
}
err = syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
if err != nil {
return err
}
// Write process ID
rundir := filepath.Dir(srv.conf.RunPath)
err = os.MkdirAll(rundir, 0750)
if err != nil {
return err
}
pidFile, err := os.OpenFile(srv.conf.RunPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
return err
}
defer pidFile.Close()
currPid := os.Getpid()
_, err = pidFile.WriteString(strconv.Itoa(currPid))
if err != nil {
return err
}
}
return err
}

152
internal/service/service.go Normal file
View File

@@ -0,0 +1,152 @@
package service
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net"
"time"
"path/filepath"
"webserv/internal/handler"
"webserv/pkg/auxtool/auxgin"
"webserv/pkg/logger"
"github.com/gin-gonic/gin"
)
const (
httpTimeout = 360
)
type ServiceConfig struct {
AssetsDir string
PortNum int
Hostname string
X509Cert []byte
X509Key []byte
Handler *handler.Handler
}
type Service struct {
assetsDir string
hand *handler.Handler
hsrv *http.Server
log *logger.Logger
engine *gin.Engine
listen net.Listener
portnum int
hostname string
x509cert []byte
x509key []byte
}
func NewService(conf *ServiceConfig) (*Service, error) {
var err error
svc := &Service{
assetsDir: conf.AssetsDir,
hand: conf.Handler,
portnum: conf.PortNum,
hostname: conf.Hostname,
x509cert: conf.X509Cert,
x509key: conf.X509Key,
}
svc.log = logger.NewLogger("service")
return svc, err
}
func (svc *Service) Build() error {
var err error
svc.log.Debugf("Build service")
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
svc.engine = gin.New()
svc.engine.Use(gin.Recovery())
svc.engine.Use(auxgin.CorsMiddleware())
svc.engine.Use(auxgin.LogMiddleware())
apiGroup := svc.engine.Group("api")
{
auxGroup := apiGroup.Group("v1")
{
sessionGroup := auxGroup.Group("user")
sessionGroup.POST("create", svc.hand.CreateUser)
sessionGroup.POST("delete", svc.hand.DeleteUser)
}
docsGroup := apiGroup.Group("docs")
swaggerUIPath := filepath.Join(svc.assetsDir, "swagger")
docsGroup.Static("", swaggerUIPath)
}
noRouteFunc := func(gctx *gin.Context) {
gctx.Status(http.StatusNotFound)
return
}
svc.engine.NoRoute(noRouteFunc)
const useTLS = false
if useTLS {
tlsCert, err := tls.X509KeyPair(svc.x509cert, svc.x509key)
if err != nil {
return err
}
tlsConfig := tls.Config{
Certificates: []tls.Certificate{tlsCert},
ClientAuth: tls.NoClientCert,
InsecureSkipVerify: true,
}
listenAddress := fmt.Sprintf(":%d", svc.portnum)
svc.listen, err = tls.Listen("tcp", listenAddress, &tlsConfig)
if err != nil {
return err
}
} else {
listenAddress := fmt.Sprintf(":%d", svc.portnum)
svc.listen, err = net.Listen("tcp", listenAddress)
if err != nil {
return err
}
}
svc.hsrv = &http.Server{
Handler: svc.engine,
}
return err
}
func (svc *Service) Router() *gin.Engine {
return svc.engine
}
func (svc *Service) Handler() *handler.Handler {
return svc.hand
}
func (svc *Service) Run() error {
var err error
for _, route := range svc.engine.Routes() {
svc.log.Debugf("The route is registered: %s %s", route.Method, route.Path)
}
err = svc.hsrv.Serve(svc.listen)
if err != nil {
return err
}
return err
}
func (svc *Service) Stop() {
svc.log.Infof("Stopping service")
if svc.hsrv != nil {
downWaiting := 5 * time.Second
ctx, _ := context.WithTimeout(context.Background(), downWaiting)
svc.hsrv.Shutdown(ctx)
}
}