1# Micro Service
2
3[![GoDoc](https://pkg.go.dev/badge/github.com/gin-gonic/gin?status.svg)](https://docs-go.hexacode.org/pkg/gitlab.hexacode.org/go-libs/microservice/)
4
5## Example: ./config.json
6```json
7{
8 "namespace": "api-registration",
9 "server": {
10 "port": 8080
11 },
12 "templates": [
13 {
14 "name": "registration",
15 "path": "/",
16 "file_path": "example/template.html"
17 }
18 ],
19 "apis": [
20 {
21 "name": "registration",
22 "path": "/registration",
23 "method": "POST",
24 "content_type": "application/json",
25 "response_type": "application/json",
26 "callback": {
27 "plugin": "plugin.so",
28 "function": "Registration"
29 },
30 "params": [
31 {
32 "name": "username",
33 "label": "Username",
34 "placeholder": "Enter Username",
35 "type": "username",
36 "validate": "username",
37 "required": true,
38 "min": 5,
39 "max": 32
40 },
41 {
42 "name": "fullname",
43 "label": "Full Name",
44 "placeholder": "Enter Full Name",
45 "type": "name",
46 "validate": "name",
47 "required": true,
48 "min": 5,
49 "max": 32
50 },
51 {
52 "name": "email",
53 "label": "Email Address",
54 "placeholder": "Enter Email Address",
55 "type": "email",
56 "required": true,
57 "min": 5,
58 "max": 32
59 },
60 {
61 "name": "password",
62 "label": "Password",
63 "placeholder": "Enter Password",
64 "type": "password",
65 "required": true,
66 "min": 8,
67 "max": 120
68 },
69 {
70 "name": "confirm_password",
71 "label": "Confirm Password",
72 "placeholder": "Enter Confirm Password",
73 "type": "password",
74 "required": true,
75 "min": 8,
76 "max": 120
77 }
78 ]
79 },
80 {
81 "name": "verification",
82 "path": "/verification/:id/:otp",
83 "method": "GET",
84 "content_type": "application/json",
85 "response_type": "application/json",
86 "callback": {
87 "plugin": "plugin.so",
88 "function": "Verification"
89 },
90 "url_params": [
91 {
92 "name": "id",
93 "type": "textnumber",
94 "required": true
95 },
96 {
97 "name": "otp",
98 "type": "textnumber",
99 "required": true
100 }
101 ]
102 }
103 ]
104}
105```
106
107## Example: main.go
108```go
109package main
110
111import (
112 "fmt"
113 "gitlab.hexacode.org/go-libs/hctypes"
114 "gitlab.hexacode.org/go-libs/microservice/config"
115)
116
117func Registration(ctx *config.ConfigContext) (int, hctypes.Dict, *config.ReturnHTTP) {
118 fmt.Println(ctx.Config.ToDict().ToColoredJson())
119 fmt.Println(ctx.ConfigApi.ToDict().ToColoredJson())
120 fmt.Println(ctx.URLParams.ToColoredJson())
121 fmt.Println(ctx.Params.ToColoredJson())
122 fmt.Println(ctx.Headers.ToColoredJson())
123
124 return 200, hctypes.Dict{
125 "id": 1,
126 "otp": 123456,
127 }, nil
128}
129
130func Verification(ctx *config.ConfigContext) (int, hctypes.Dict, *config.ReturnHTTP) {
131 fmt.Println(ctx.Config.ToDict().ToColoredJson())
132 fmt.Println(ctx.ConfigApi.ToDict().ToColoredJson())
133 fmt.Println(ctx.URLParams.ToColoredJson())
134 fmt.Println(ctx.Params.ToColoredJson())
135 fmt.Println(ctx.Headers.ToColoredJson())
136
137 return 200, hctypes.Dict{
138 "success": true,
139 "message": "OTP verified",
140 }, nil
141}
142```
View as plain text