-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add init command to initialize a
hello world
sample API with GoFr (#3)
- Loading branch information
1 parent
008c4b4
commit c1d69fa
Showing
4 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"text/template" | ||
|
||
"gofr.dev/pkg/gofr" | ||
) | ||
|
||
const ( | ||
modContent = `module {{ .Module }} | ||
go 1.22.4 | ||
require gofr.dev v{{ .GofrVersion }} | ||
` | ||
mainContent = `package main | ||
import ( | ||
"gofr.dev/pkg/gofr" | ||
) | ||
func main() { | ||
app := gofr.New() | ||
app.GET("/hello", func(ctx *gofr.Context) (interface{}, error) { | ||
return "Hello World!", nil | ||
}) | ||
app.Run() | ||
} | ||
` | ||
fileMode = 0644 | ||
) | ||
|
||
type modInfo struct { | ||
Module string | ||
GofrVersion string | ||
} | ||
|
||
func Create(ctx *gofr.Context) (interface{}, error) { | ||
name := ctx.Param("name") | ||
gofrVersion := ctx.Param("gofr") | ||
|
||
if gofrVersion == "" { | ||
gofrVersion = "1.17.0" | ||
} | ||
|
||
modFile, err := os.OpenFile("go.mod", os.O_CREATE|os.O_WRONLY, fileMode) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
t, err := template.New("go-mod").Parse(modContent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = t.Execute(modFile, modInfo{Module: name, GofrVersion: gofrVersion}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fmt.Println("Note: Please do go mod tidy to sync the dependencies of your project") | ||
|
||
mainFile, err := os.OpenFile("main.go", os.O_CREATE|os.O_WRONLY, fileMode) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = mainFile.WriteString(mainContent) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return "Successfully initialized project " + name, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters